diff --git a/sanic/server/protocols/base_protocol.py b/sanic/server/protocols/base_protocol.py index 4e7540c6..63d4bfb5 100644 --- a/sanic/server/protocols/base_protocol.py +++ b/sanic/server/protocols/base_protocol.py @@ -81,13 +81,24 @@ class SanicProtocol(asyncio.Protocol): self._data_received.clear() await self._data_received.wait() - def close(self): + def close(self, timeout: Optional[float] = None): + """ + Attempt close the connection. + """ + # Cause a call to connection_lost where further cleanup occurs + if self.transport: + self.transport.close() + if timeout is None: + timeout = self.app.config.GRACEFUL_SHUTDOWN_TIMEOUT + self.loop.call_later(timeout, self.abort) + + def abort(self): """ Force close the connection. """ # Cause a call to connection_lost where further cleanup occurs if self.transport: - self.transport.close() + self.transport.abort() self.transport = None # asyncio.Protocol API Callbacks # diff --git a/sanic/server/runners.py b/sanic/server/runners.py index c28c525e..39d8f736 100644 --- a/sanic/server/runners.py +++ b/sanic/server/runners.py @@ -180,7 +180,7 @@ def serve( if hasattr(conn, "websocket") and conn.websocket: coros.append(conn.websocket.close_connection()) else: - conn.close() + conn.abort() _shutdown = asyncio.gather(*coros) loop.run_until_complete(_shutdown)