File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -269,11 +269,28 @@ async def watch_restarts(self) -> None: # pragma: no cover
269269
270270
271271def serve_in_thread (server : TestServer ) -> typing .Iterator [TestServer ]:
272- thread = threading .Thread (target = server .run )
272+ server_exception = None
273+ server_caught_exception = threading .Event ()
274+
275+ def _run_server () -> None :
276+ nonlocal server_exception
277+ try :
278+ server .run ()
279+ except BaseException as exc : # pragma: nocover
280+ # BaseException as we need to catch SystemExit too;
281+ # `uvicorn` calls `sys.exit(1)` at failure.
282+ server_exception = exc
283+ server_caught_exception .set ()
284+
285+ thread = threading .Thread (target = _run_server )
273286 thread .start ()
287+
274288 try :
275289 while not server .started :
276- time .sleep (1e-3 )
290+ if server_caught_exception .wait (1e-3 ):
291+ raise RuntimeError (
292+ f"Server failed to start: { server_exception !r} " ,
293+ ) from server_exception
277294 yield server
278295 finally :
279296 server .should_exit = True
You can’t perform that action at this time.
0 commit comments