perf: stop idle server promptly on should_exit
Closed
RA
8h ago+45+45+01234567890123456789-5-5-0123456789
Fixes #3216
Motivation and Context
When embedding Uvicorn in isolated test runners or programmatic environments, an idle server took ~202 ms to stop from setting server.should_exit = True until server.serve() returned, even with no active connections and lifespan disabled.
This was caused by two fixed delays in Server:
main_looppolledon_tick()with an unconditionalawait asyncio.sleep(0.1).shutdown()unconditionally slept another 100 ms (await asyncio.sleep(0.1)) after iterating connections, even whenself.server_state.connectionswas empty.
Solution
- Wakeable Event on
should_exit:- Manage
_exit_event = asyncio.Event(). - Expose
should_exitas a property with getter/setter; settingserver.should_exit = Trueimmediately wakes up theasyncio.wait_for(self._exit_event.wait(), timeout=0.1)inmain_loop.
- Manage
- Conditional Connection Drain:
- Only sleep the 100 ms grace period in
shutdown()if active connections exist inself.server_state.connections.
- Only sleep the 100 ms grace period in
- Tests:
- Added
test_idle_server_fast_shutdownintests/test_server.py. - Verified idle server stop latency drops from ~216 ms down to <1 ms (<0.3 ms in local test runs).
- Added
Signed-off-by: Raj Gahoi gahoiraj01@gmail.com