Skip to content

Commit

Permalink
Fix UI freeze on rapid generators (#7055)
Browse files Browse the repository at this point in the history
* changes

* add changeset

* changes

---------

Co-authored-by: Ali Abid <aliabid@Alis-MacBook-Pro.local>
Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
  • Loading branch information
3 people committed Jan 18, 2024
1 parent 64c65d8 commit 3c3cf86
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changeset/forty-pans-add.md
@@ -0,0 +1,6 @@
---
"@gradio/client": patch
"gradio": patch
---

fix:Fix UI freeze on rapid generators
14 changes: 10 additions & 4 deletions client/js/src/client.ts
Expand Up @@ -290,6 +290,7 @@ export function api_factory(
let pending_stream_messages: Record<string, any[]> = {}; // Event messages may be received by the SSE stream before the initial data POST request is complete. To resolve this race condition, we store the messages in a dictionary and process them when the POST request is complete.
let event_stream: EventSource | null = null;
const event_callbacks: Record<string, () => Promise<void>> = {};
const unclosed_events: Set<string> = new Set();
let config: Config;
let api_map: Record<string, number> = {};

Expand Down Expand Up @@ -902,9 +903,6 @@ export function api_factory(
) {
if (event_callbacks[event_id]) {
delete event_callbacks[event_id];
if (Object.keys(event_callbacks).length === 0) {
close_stream();
}
}
}
} catch (e) {
Expand All @@ -928,6 +926,7 @@ export function api_factory(
delete pending_stream_messages[event_id];
}
event_callbacks[event_id] = callback;
unclosed_events.add(event_id);
if (!stream_open) {
open_stream();
}
Expand Down Expand Up @@ -1042,7 +1041,14 @@ export function api_factory(
)
);
} else if (event_callbacks[event_id]) {
await event_callbacks[event_id](_data);
if (_data.msg === "process_completed") {
unclosed_events.delete(event_id);
if (unclosed_events.size === 0) {
close_stream();
}
}
let fn = event_callbacks[event_id];
window.setTimeout(fn, 0, _data); // need to do this to put the event on the end of the event loop, so the browser can refresh between callbacks and not freeze in case of quick generations. See https://github.com/gradio-app/gradio/pull/7055
} else {
if (!pending_stream_messages[event_id]) {
pending_stream_messages[event_id] = [];
Expand Down

0 comments on commit 3c3cf86

Please sign in to comment.