Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Catch errors from TestServer (instead of hanging if server.started
…never becomes true)
  • Loading branch information
akx committed Feb 24, 2026
commit 65e2d866625d8834258d49d947db4700150f3c29
21 changes: 19 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,28 @@ async def watch_restarts(self) -> None: # pragma: no cover


def serve_in_thread(server: TestServer) -> typing.Iterator[TestServer]:
thread = threading.Thread(target=server.run)
server_exception = None
server_caught_exception = threading.Event()

def _run_server() -> None:
nonlocal server_exception
try:
server.run()
except BaseException as exc: # pragma: nocover
# BaseException as we need to catch SystemExit too;
# `uvicorn` calls `sys.exit(1)` at failure.
server_exception = exc
server_caught_exception.set()

thread = threading.Thread(target=_run_server)
thread.start()

try:
while not server.started:
time.sleep(1e-3)
if server_caught_exception.wait(1e-3):
raise RuntimeError(
f"Server failed to start: {server_exception!r}",
) from server_exception
yield server
finally:
server.should_exit = True
Expand Down