Perform graceful shutdown.
(self)
| 219 | await self._shutdown() |
| 220 | |
| 221 | async def _shutdown(self): |
| 222 | """Perform graceful shutdown.""" |
| 223 | self.log.info("Worker shutting down...") |
| 224 | |
| 225 | # Stop accepting new connections |
| 226 | for server in self.servers: |
| 227 | server.close() |
| 228 | |
| 229 | # Wait for servers to close (skip on quick shutdown) |
| 230 | if not self._quick_shutdown: |
| 231 | for server in self.servers: |
| 232 | if self._quick_shutdown: |
| 233 | break |
| 234 | try: |
| 235 | await asyncio.wait_for(server.wait_closed(), timeout=0.5) |
| 236 | except asyncio.TimeoutError: |
| 237 | pass # Check _quick_shutdown on next iteration |
| 238 | |
| 239 | # Wait for in-flight connections (skip on quick shutdown) |
| 240 | if self.nr_conns > 0 and not self._quick_shutdown: |
| 241 | graceful_timeout = self.cfg.graceful_timeout |
| 242 | self.log.info("Waiting for %d connections to finish...", self.nr_conns) |
| 243 | deadline = self.loop.time() + graceful_timeout |
| 244 | while self.nr_conns > 0 and self.loop.time() < deadline: |
| 245 | if self._quick_shutdown: |
| 246 | self.log.info("Quick shutdown requested") |
| 247 | break |
| 248 | await asyncio.sleep(0.1) |
| 249 | |
| 250 | if self.nr_conns > 0: |
| 251 | self.log.warning("Forcing close of %d connections", self.nr_conns) |
| 252 | |
| 253 | # Run lifespan shutdown (skip on quick shutdown) |
| 254 | if self.lifespan and not self._quick_shutdown: |
| 255 | try: |
| 256 | await self.lifespan.shutdown() |
| 257 | except Exception as e: |
| 258 | self.log.error("ASGI lifespan shutdown error: %s", e) |
| 259 | |
| 260 | def _get_ssl_context(self): |
| 261 | """Get SSL context if configured.""" |