question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Errors during restart due to open websocket connections

See original GitHub issue

Hi,

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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
mariainescommented, Sep 27, 2021

@acrozes no, unfortunately not. We’ve learned to ignore the errors 😛

0reactions
Kludexcommented, May 15, 2022

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).

Read more comments on GitHub >

github_iconTop Results From Across the Web

WebSocket connection failed: Error during ... - Stack Overflow
Problem solved! I just figured out how to solve the issue, but I would still like to know if this is normal behavior...
Read more >
Troubleshooting connection issues | Socket.IO
Problem: the socket is not able to connect​. Possible explanations: You are trying to reach a plain WebSocket server; The server is not ......
Read more >
Error with WebSocket - Microsoft Q&A
Hello I get the error: WebSocket connection to 'wss://localhost:54970/Dashboard.Server/' failed: Error in connection establishment: net::ERR_ ...
Read more >
"WebSocket Unable to Connect” error message, what should I ...
The “WebSocket unable to connect” error message indicates that you are likely working behind a proxy that doesn't support the WebSocket ...
Read more >
Troubleshoot connection issues | Slack
To collect Net Logs, click the Download Logs button below the error message, or use the steps below. Open the Slack desktop app,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found