fix: gracefully handle port-in-use error on server startup (#13001)

Catch EADDRINUSE OSError when binding the TCP site and exit with a clear error message instead of an unhandled traceback.
This commit is contained in:
Luke Mino-Altherr 2026-05-03 05:44:20 -07:00 committed by GitHub
parent d0f0b15cf5
commit 867b8d2408
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,3 +1,4 @@
import errno
import os
import sys
import asyncio
@ -1245,7 +1246,13 @@ class PromptServer():
address = addr[0]
port = addr[1]
site = web.TCPSite(runner, address, port, ssl_context=ssl_ctx)
await site.start()
try:
await site.start()
except OSError as e:
if e.errno == errno.EADDRINUSE:
logging.error(f"Port {port} is already in use on address {address}. Please close the other application or use a different port with --port.")
raise SystemExit(1)
raise
if not hasattr(self, 'address'):
self.address = address #TODO: remove this