Skip to content

Commit 5b9ea86

Browse files
rustyconoverclaude
andcommitted
vgi-serve: listen before printing PORT under waitress
The port line went out before waitress bound its socket, so a harness that read PORT: and connected at once could get connection refused (CI hit it in test_serve_blob_vendor on both Linux lanes). Create the server first, then announce it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent ac874df commit 5b9ea86

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

‎vgi/serve.py‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,12 +1685,6 @@ def _serve_http(
16851685
iroh_authenticate=iroh_authenticate,
16861686
)
16871687

1688-
# Machine-readable port for process managers and test harnesses
1689-
print(f"PORT:{port}", flush=True)
1690-
_logger.info("http_server_starting host=%s port=%d prefix=%s", host, port, prefix)
1691-
sys.stderr.write(f"Serving {worker_cls.__name__} on http://{host}:{port}{prefix}\n")
1692-
sys.stderr.flush()
1693-
16941688
# Same Arrow-sized buffers the other two serve paths use. Without them
16951689
# waitress spools multi-MiB bodies through temp files and reads sockets in
16961690
# 8 KiB chunks; measured on this path, tuning is worth ~2x.
@@ -1701,16 +1695,28 @@ def _serve_http(
17011695
serve_kwargs.update(waitress_arrow_tuning(resolve_blob_vendor_http_config().max_request_bytes))
17021696
if http_threads is not None:
17031697
serve_kwargs["threads"] = http_threads
1704-
if idle_exit_seconds is None:
1705-
waitress.serve(wsgi_app, **serve_kwargs)
1698+
serve_kwargs.pop("_quiet")
1699+
tracker = _idle_exit.ActivityTracker() if idle_exit_seconds is not None else None
1700+
app = _idle_exit.IdleMiddleware(wsgi_app, tracker) if tracker is not None else wsgi_app
1701+
# Bind and listen before announcing the port: a harness that reads PORT:
1702+
# and connects at once must never race the listener.
1703+
waitress_server = waitress.create_server(app, **serve_kwargs)
1704+
1705+
# Machine-readable port for process managers and test harnesses
1706+
print(f"PORT:{port}", flush=True)
1707+
_logger.info("http_server_starting host=%s port=%d prefix=%s", host, port, prefix)
1708+
sys.stderr.write(f"Serving {worker_cls.__name__} on http://{host}:{port}{prefix}\n")
1709+
sys.stderr.flush()
1710+
1711+
if tracker is None or idle_exit_seconds is None:
1712+
waitress_server.run()
17061713
return
17071714
# One process: the watcher interrupts the main thread, where waitress turns
17081715
# KeyboardInterrupt into a clean shutdown and returns.
1709-
tracker = _idle_exit.ActivityTracker()
17101716
watcher = _idle_exit.IdleWatcher(tracker, idle_exit_seconds, _idle_exit.interrupt_main)
17111717
watcher.start()
17121718
try:
1713-
waitress.serve(_idle_exit.IdleMiddleware(wsgi_app, tracker), **serve_kwargs)
1719+
waitress_server.run()
17141720
except KeyboardInterrupt:
17151721
if watcher.is_alive():
17161722
raise # a real Ctrl-C, not the idle timer

0 commit comments

Comments
 (0)