Skip to content

Commit 7fc0f51

Browse files
Fix reload mode by implementing close on the client (#8548)
* Fix reload mode * add changeset * reload test * add changeset --------- Co-authored-by: gradio-pr-bot <[email protected]>
1 parent 88de38e commit 7fc0f51

File tree

3 files changed

+89
-2
lines changed

3 files changed

+89
-2
lines changed

.changeset/red-rice-build.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@gradio/client": patch
3+
"gradio": patch
4+
---
5+
6+
fix:Fix reload mode by implementing `close` on the client

client/js/src/client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {
3131
parse_and_set_cookies
3232
} from "./helpers/init_helpers";
3333
import { check_space_status } from "./helpers/spaces";
34-
import { open_stream, readable_stream } from "./utils/stream";
34+
import { open_stream, readable_stream, close_stream } from "./utils/stream";
3535
import { API_INFO_ERROR_MSG, CONFIG_ERROR_MSG } from "./constants";
3636

3737
export class Client {
@@ -209,7 +209,7 @@ export class Client {
209209
}
210210

211211
close(): void {
212-
this.heartbeat_event?.close();
212+
close_stream(this.stream_status, this.abort_controller);
213213
}
214214

215215
static async duplicate(
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { test, expect } from "@playwright/test";
2+
import { spawnSync } from "node:child_process";
3+
import { launch_app_background, kill_process } from "./utils";
4+
import { join } from "path";
5+
6+
let _process;
7+
8+
const demo_file = "chat_demo.py";
9+
10+
test.beforeAll(() => {
11+
const demo = `
12+
import gradio as gr
13+
14+
def greet(msg, history):
15+
return "Hello"
16+
17+
demo = gr.ChatInterface(fn=greet)
18+
19+
if __name__ == "__main__":
20+
demo.launch()
21+
`;
22+
spawnSync(`echo '${demo}' > ${join(process.cwd(), demo_file)}`, {
23+
shell: true,
24+
stdio: "pipe",
25+
env: {
26+
...process.env,
27+
PYTHONUNBUFFERED: "true"
28+
}
29+
});
30+
});
31+
32+
test.afterAll(() => {
33+
if (_process) kill_process(_process);
34+
spawnSync(`rm ${join(process.cwd(), demo_file)}`, {
35+
shell: true,
36+
stdio: "pipe",
37+
env: {
38+
...process.env,
39+
PYTHONUNBUFFERED: "true"
40+
}
41+
});
42+
});
43+
44+
test("gradio dev mode correctly reloads a stateful ChatInterface demo", async ({
45+
page
46+
}) => {
47+
test.setTimeout(20 * 1000);
48+
49+
try {
50+
const port = 7890;
51+
const { _process: server_process } = await launch_app_background(
52+
`GRADIO_SERVER_PORT=${port} gradio ${join(process.cwd(), demo_file)}`,
53+
process.cwd()
54+
);
55+
_process = server_process;
56+
console.log("Connected to port", port);
57+
const demo = `
58+
import gradio as gr
59+
60+
def greet(msg, history):
61+
return "Hello"
62+
63+
demo = gr.ChatInterface(fn=greet, textbox=gr.Textbox(label="foo"))
64+
65+
if __name__ == "__main__":
66+
demo.launch()
67+
`;
68+
await page.goto(`http://localhost:${port}`);
69+
spawnSync(`echo '${demo}' > ${join(process.cwd(), demo_file)}`, {
70+
shell: true,
71+
stdio: "pipe",
72+
env: {
73+
...process.env,
74+
PYTHONUNBUFFERED: "true"
75+
}
76+
});
77+
await expect(page.getByLabel("foo")).toBeVisible();
78+
} finally {
79+
if (_process) kill_process(_process);
80+
}
81+
});

0 commit comments

Comments
 (0)