Add a port timeout detection feature for gradio/preview. (#9707)

* Add a port timeout detection feature for gradio/preview. This feature can help users receive more informative messages when running 'gradio cc dev' in case of network issues

* add changeset

* add changeset

* fix

---------

Co-authored-by: Freddy Boulton <alfonsoboulton@gmail.com>
Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
This commit is contained in:
lcjia_you 2024-10-18 03:28:07 +08:00 committed by GitHub
parent cd7dab7ba5
commit 8d848dd1e4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 0 deletions

View File

@ -0,0 +1,5 @@
---
"@gradio/preview": patch
---
fix:Add a port timeout detection feature for gradio/preview.

View File

@ -129,6 +129,12 @@ export async function find_free_ports(
export function is_free_port(port: number): Promise<boolean> {
return new Promise((accept, reject) => {
const sock = net.createConnection(port, "127.0.0.1");
setTimeout(() => {
sock.destroy();
reject(
new Error(`Timeout while detecting free port with 127.0.0.1:${port} `)
);
}, 3000);
sock.once("connect", () => {
sock.end();
accept(false);