Skip to content

Commit be20ce8

Browse files
hystsgradio-pr-bot
andauthored
Fix UI freeze when one event makes multiple components of the same type visible (#13521)
* Fix UI freeze when multiple components become visible Visibility updates in update_state() rebuilt the entire component tree with traverse() and reassigned this.root. When one event made two or more components of the same type visible, the concurrent rebuilds (handle_data applies outputs in parallel) trapped Svelte's async reactivity in a non-terminating update cycle, freezing the page and eventually crashing the tab. Update the target node's visibility in place and load any missing components in place via load_components(), mirroring what render_previously_invisible_children() already does. Also make load_components() store the loading component promise instead of the whole get_component() return value so awaiting node.component yields the module. Regression introduced in #12839, which kept forms mounted (hidden via CSS) instead of marking them invisible, so the racing rebuilds started happening under an already-mounted parent. * add changeset * Propagate runtime when lazily mounting components load_components now copies the loaded component's runtime alongside its component, matching the node-creation path in create_node, so a custom component that needs a separate Svelte runtime mounts correctly when it is made visible after the initial load. * Move slider repro into invisible_textbox demo Per review feedback, drop the separate two_sliders testcase app and add the sliders as a new tab in the existing run.py, so the E2E suite does not need to spin up another Gradio app. Also extend the test to hide the sliders again after showing them, confirming the page does not freeze. --------- Co-authored-by: gradio-pr-bot <[email protected]>
1 parent c440779 commit be20ce8

4 files changed

Lines changed: 47 additions & 18 deletions

File tree

‎.changeset/empty-tables-suffer.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@gradio/core": patch
3+
"gradio": patch
4+
---
5+
6+
fix:Fix UI freeze when one event makes multiple components of the same type visible

‎demo/invisible_textbox/run.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,18 @@ def show():
3232
show_third_message.click(lambda: gr.Accordion(visible=True), outputs=acc)
3333
hide_third_message = gr.Button("Hide Accordion")
3434
hide_third_message.click(lambda: gr.Accordion(visible=False), outputs=acc)
35+
with gr.Tab("Sliders Tab"):
36+
slider1 = gr.Slider(0, 1, value=0, visible=False, elem_id="slider-1")
37+
slider2 = gr.Slider(0, 1, value=0, visible=False, elem_id="slider-2")
38+
show_sliders_btn = gr.Button("Show Sliders")
39+
hide_sliders_btn = gr.Button("Hide Sliders")
40+
show_sliders_btn.click(
41+
lambda: (gr.Slider(visible=True), gr.Slider(visible=True)),
42+
outputs=[slider1, slider2],
43+
)
44+
hide_sliders_btn.click(
45+
lambda: (gr.Slider(visible=False), gr.Slider(visible=False)),
46+
outputs=[slider1, slider2],
47+
)
3548
if __name__ == "__main__":
3649
demo.launch()

‎js/core/src/init.svelte.ts‎

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -445,12 +445,15 @@ export class AppTree {
445445
let already_updated_visibility = false;
446446
if (check_visibility && !node?.component) {
447447
await tick();
448-
this.root = this.traverse(this.root!, [
449-
//@ts-ignore
450-
(n) => set_visibility_for_updated_node(n, id, new_state.visible),
451-
//@ts-ignore
452-
(n) => handle_visibility(n, this.#config.api_url)
453-
]);
448+
// Update the node in place. Rebuilding the tree with traverse() here
449+
// replaces every node's children array, and two rebuilds racing in one
450+
// event (two outputs made visible together) freeze Svelte's reactivity.
451+
const updated_node = find_node_by_id(this.root!, id);
452+
if (updated_node && "visible" in new_state) {
453+
updated_node.props.shared_props.visible =
454+
new_state.visible as SharedProps["visible"];
455+
}
456+
load_components(this.root!, this.#config.api_url);
454457
await tick();
455458
already_updated_visibility = true;
456459
}
@@ -656,7 +659,9 @@ function has_hidden_descendants(
656659

657660
function load_components(node: ProcessedComponentMeta, api_url: string): void {
658661
if (node.props.shared_props.visible && !node.component) {
659-
node.component = get_component(node.type, node.component_class_id, api_url);
662+
const loaded = get_component(node.type, node.component_class_id, api_url);
663+
node.component = loaded.component;
664+
node.runtime = loaded.runtime;
660665
}
661666
node.children.forEach((child) => load_components(child, api_url));
662667
}
@@ -793,17 +798,6 @@ function handle_visibility(
793798
}
794799
}
795800

796-
function set_visibility_for_updated_node(
797-
node: ProcessedComponentMeta,
798-
id: number,
799-
visible: boolean
800-
): ProcessedComponentMeta {
801-
if (node.id == id) {
802-
node.props.shared_props.visible = visible;
803-
}
804-
return node;
805-
}
806-
807801
function _untrack(
808802
node: ProcessedComponentMeta,
809803
components_to_register: Set<number>

‎js/spa/test/invisible_textbox.spec.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,22 @@ test("Making accordion visible does not show all children automatically", async
7272
await expect(page.locator("#hidden-number")).toHaveCount(0);
7373
});
7474

75+
test("Making two components of the same type visible in one event does not freeze the page", async ({
76+
page
77+
}) => {
78+
await page.getByRole("tab", { name: "Sliders Tab" }).click();
79+
80+
await page.click('text="Show Sliders"');
81+
82+
await expect(page.locator("#slider-1")).toBeVisible();
83+
await expect(page.locator("#slider-2")).toBeVisible();
84+
85+
await page.click('text="Hide Sliders"');
86+
87+
await expect(page.locator("#slider-1")).not.toBeVisible();
88+
await expect(page.locator("#slider-2")).not.toBeVisible();
89+
});
90+
7591
test("Hiding accordion retains textbox value when accordion is shown again", async ({
7692
page
7793
}) => {

0 commit comments

Comments
 (0)