Errors during restart due to open websocket connections
See original GitHub issueHi,
I’m using a WebsocketEndpoint and running the app with --reload
. All is working great, until I make some changes and the app gets restarted. Then I see a bunch of errors due to open websocket connections during the restart.
It seems that on restart, Starlette closes all open connections but does not stop accepting new connections, so when the client immediately tries to reconnect, we’re left with newly opened connections that then get force-killed when the shutdown completes. I’ve tried using the on_shutdown
handler to stop accepting new connections, but that doesn’t get called until after the shutdown, at which point we’ve already closed + reopened the connections.
One idea could be to expose a before_shutdown
handler that gets called before the shutdown, so in my WebsocketEndpoint I can check whether we’re shutting down and not accept the new connections.
Otherwise, any ideas for how to avoid or silence these errors?
Thanks!
Here are the errors I see:
INFO: Application shutdown complete.
INFO: Finished server process [587]
ERROR: Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x7fc1183846d0>
INFO: Started server process [601]
INFO: Waiting for application startup.
INFO: Application startup complete.
WARNING: Error in opening handshake
Traceback (most recent call last):
File "/usr/local/lib64/python3.7/site-packages/websockets/server.py", line 134, in handler
extra_headers=self.extra_headers,
File "/usr/local/lib64/python3.7/site-packages/websockets/server.py", line 584, in handshake
self.write_http_response(http.HTTPStatus.SWITCHING_PROTOCOLS, response_headers)
File "/usr/local/lib64/python3.7/site-packages/websockets/server.py", line 264, in write_http_response
self.transport.write(response.encode())
File "uvloop/handles/stream.pyx", line 673, in uvloop.loop.UVStream.write
File "uvloop/handles/handle.pyx", line 159, in uvloop.loop.UVHandle._ensure_alive
RuntimeError: unable to perform operation on <TCPTransport closed=True reading=False 0x27768c0>; the handler is closed
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/uvicorn/protocols/websockets/websockets_impl.py", line 153, in run_asgi
result = await self.app(self.scope, self.asgi_receive, self.asgi_send)
File "/usr/local/lib/python3.7/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__
return await self.app(scope, receive, send)
File "/usr/local/lib/python3.7/site-packages/starlette/applications.py", line 102, in __call__
await self.middleware_stack(scope, receive, send)
File "/usr/local/lib/python3.7/site-packages/starlette/middleware/errors.py", line 146, in __call__
await self.app(scope, receive, send)
File "/usr/local/lib/python3.7/site-packages/starlette/exceptions.py", line 58, in __call__
await self.app(scope, receive, send)
File "/usr/local/lib/python3.7/site-packages/starlette/routing.py", line 550, in __call__
await route.handle(scope, receive, send)
File "/usr/local/lib/python3.7/site-packages/starlette/routing.py", line 283, in handle
await self.app(scope, receive, send)
File "/usr/local/lib/python3.7/site-packages/starlette/endpoints.py", line 74, in dispatch
raise exc from None
File "/usr/local/lib/python3.7/site-packages/starlette/endpoints.py", line 65, in dispatch
message = await websocket.receive()
File "/usr/local/lib/python3.7/site-packages/starlette/websockets.py", line 40, in receive
message = await self._receive()
File "/usr/local/lib/python3.7/site-packages/uvicorn/protocols/websockets/websockets_impl.py", line 233, in asgi_receive
data = await self.recv()
File "/usr/local/lib64/python3.7/site-packages/websockets/protocol.py", line 495, in recv
return_when=asyncio.FIRST_COMPLETED,
File "/usr/lib64/python3.7/asyncio/tasks.py", line 387, in wait
fs = {ensure_future(f, loop=loop) for f in set(fs)}
File "/usr/lib64/python3.7/asyncio/tasks.py", line 387, in <setcomp>
fs = {ensure_future(f, loop=loop) for f in set(fs)}
File "/usr/lib64/python3.7/asyncio/tasks.py", line 619, in ensure_future
raise TypeError('An asyncio.Future, a coroutine or an awaitable is '
TypeError: An asyncio.Future, a coroutine or an awaitable is required
ERROR: Exception in ASGI application
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:5 (2 by maintainers)
Top GitHub Comments
@acrozes no, unfortunately not. We’ve learned to ignore the errors 😛
I’m closing this as no one confirmed. Please, if you arrived here, and you’re having the same issue, feel free to create a new one with a minimal reproducible example that we can use to reproduce (it can be a gist, or GitHub project, as well).