Signal shutdown and wait for completion. This should be called during graceful shutdown.
(self)
| 88 | self.logger.debug("ASGI lifespan startup complete") |
| 89 | |
| 90 | async def shutdown(self): |
| 91 | """Signal shutdown and wait for completion. |
| 92 | |
| 93 | This should be called during graceful shutdown. |
| 94 | """ |
| 95 | if self._app_finished: |
| 96 | self.logger.debug("ASGI lifespan already finished") |
| 97 | return |
| 98 | |
| 99 | # Send shutdown event |
| 100 | await self._receive_queue.put({"type": "lifespan.shutdown"}) |
| 101 | |
| 102 | # Wait for shutdown with timeout |
| 103 | try: |
| 104 | await asyncio.wait_for( |
| 105 | self._shutdown_complete.wait(), |
| 106 | timeout=30.0 # Reasonable shutdown timeout |
| 107 | ) |
| 108 | except asyncio.TimeoutError: |
| 109 | self.logger.warning("Lifespan shutdown timed out") |
| 110 | |
| 111 | if self._shutdown_error: |
| 112 | self.logger.error("Lifespan shutdown error: %s", self._shutdown_error) |
| 113 | |
| 114 | # Cancel the task if still running |
| 115 | if self._task and not self._task.done(): |
| 116 | self._task.cancel() |
| 117 | try: |
| 118 | await self._task |
| 119 | except asyncio.CancelledError: |
| 120 | pass |
| 121 | |
| 122 | self.logger.debug("ASGI lifespan shutdown complete") |
| 123 | |
| 124 | async def _run_lifespan(self, scope): |
| 125 | """Run the ASGI lifespan protocol.""" |