MCPcopy
hub / github.com/benoitc/gunicorn / connection_lost

Method connection_lost

gunicorn/asgi/protocol.py:672–713  ·  view source on GitHub ↗

Called when the connection is lost or closed. Instead of immediately cancelling the task, we signal a disconnect event and send an http.disconnect message to the receive queue. This allows the ASGI app to clean up resources (like database connections) gracefully befo

(self, exc)

Source from the content-addressed store, hash-verified

670 self._close_transport()
671
672 def connection_lost(self, exc):
673 """Called when the connection is lost or closed.
674
675 Instead of immediately cancelling the task, we signal a disconnect
676 event and send an http.disconnect message to the receive queue.
677 This allows the ASGI app to clean up resources (like database
678 connections) gracefully before the task is cancelled.
679
680 See: https://github.com/benoitc/gunicorn/issues/3484
681 """
682 # Guard against multiple calls (idempotent)
683 if self._closed:
684 return
685
686 self._closed = True
687 self.worker.nr_conns -= 1
688
689 # Cancel keepalive timer
690 self._cancel_keepalive_timer()
691
692 if self.reader:
693 self.reader.feed_eof()
694
695 # Signal EOF to WebSocket if active
696 if self._websocket:
697 self._websocket.feed_eof()
698
699 # Signal disconnect to the app via the body receiver
700 if self._body_receiver is not None:
701 self._body_receiver.signal_disconnect()
702
703 # Schedule task cancellation after grace period if task doesn't complete
704 if self._task and not self._task.done():
705 grace_period = getattr(self.cfg, 'asgi_disconnect_grace_period', 3)
706 if grace_period > 0:
707 self.worker.loop.call_later(
708 grace_period,
709 self._cancel_task_if_pending
710 )
711 else:
712 # Grace period of 0 means cancel immediately
713 self._task.cancel()
714
715 def _cancel_task_if_pending(self):
716 """Cancel the task if it's still pending after grace period."""

Calls 3

feed_eofMethod · 0.80
signal_disconnectMethod · 0.80