Skip to content
Closed
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
20 changes: 17 additions & 3 deletions uvicorn/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,23 @@ def __init__(self, config: Config) -> None:
self.server_state = ServerState()

self.started = False
self.should_exit = False
self._should_exit = False
self._exit_event: asyncio.Event | None = None
self.force_exit = False
self.last_notified = 0.0

self._captured_signals: list[int] = []

@property
def should_exit(self) -> bool:
return self._should_exit

@should_exit.setter
def should_exit(self, value: bool) -> None:
self._should_exit = value
if value and self._exit_event is not None:
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
self._exit_event.set()
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Outdated
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Outdated
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated

@functools.cached_property
def limit_max_requests(self) -> int | None:
if self.config.limit_max_requests is None:
Expand Down Expand Up @@ -240,12 +251,14 @@ def _log_started_message(self, listeners: Sequence[socket.SocketType]) -> None:
)

async def main_loop(self) -> None:
self._exit_event = asyncio.Event()
counter = 0
should_exit = await self.on_tick(counter)
while not should_exit:
counter += 1
counter = counter % 864000
await asyncio.sleep(0.1)
with contextlib.suppress(asyncio.TimeoutError):
await asyncio.wait_for(self._exit_event.wait(), timeout=0.1)
should_exit = await self.on_tick(counter)

async def on_tick(self, counter: int) -> bool:
Expand Down Expand Up @@ -290,7 +303,8 @@ async def shutdown(self, sockets: list[socket.socket] | None = None) -> None:
# Request shutdown on all existing connections.
for connection in list(self.server_state.connections):
connection.shutdown()
await asyncio.sleep(0.1)
if self.server_state.connections:
await asyncio.sleep(0.1)

try:
async with asyncio.timeout(self.config.timeout_graceful_shutdown):
Expand Down
Loading