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.

Handshaking may not be completed yet at `shutdown` in wsproto impl

See original GitHub issue

Traceback:

LocalProtocolError: Event CloseConnection(code=1012, reason=None) cannot be sent during the handshake
  File "gunicorn/arbiter.py", line 583, in spawn_worker
    worker.init_process()
  File "uvicorn/workers.py", line 57, in init_process
    super(UvicornWorker, self).init_process()
  File "gunicorn/workers/base.py", line 140, in init_process
    self.run()
  File "uvicorn/workers.py", line 66, in run
    loop.run_until_complete(server.serve(sockets=self.sockets))
  File "uvloop/loop.pyx", line 1456, in uvloop.loop.Loop.run_until_complete
  File "uvicorn/main.py", line 403, in serve
    await self.shutdown(sockets=sockets)
  File "uvicorn/main.py", line 539, in shutdown
    connection.shutdown()
  File "uvicorn/protocols/websockets/wsproto_impl.py", line 115, in shutdown
    output = self.conn.send(wsproto.events.CloseConnection(code=1012))
  File "__init__.py", line 61, in send
    data += self.handshake.send(event)
  File "wsproto/handshake.py", line 101, in send
    "Event {} cannot be sent during the handshake".format(event)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
euri10commented, Mar 21, 2020

on a side note thanks for websocat, it rocks

0reactions
florimondmancacommented, Dec 9, 2020

This didn’t get fixe by #704 — I was able to reproduce against this sample app:

import asyncio


async def app(scope, receive, send):
    assert scope["type"] == "websocket"
    event = await receive()
    assert event["type"] == "websocket.connect"

    print("Go ahead, stop the server...")
    await asyncio.sleep(10)
    print("Too late!")

    await send({"type": "websocket.accept"})
>>> import websockets
>>> await websockets.connect("ws://localhost:8000")
websockets.exceptions.InvalidMessage: did not receive a valid HTTP response

After connecting the client I go ahead and shut down the server with Ctrl+C, reproducing the error:

$ uvicorn debug.app:app --ws wsproto
INFO:     Started server process [91950]
INFO:     Waiting for application startup.
INFO:     ASGI 'lifespan' protocol appears unsupported.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
Go ahead, stop the server...
^CINFO:     Shutting down
Traceback (most recent call last):
  File "/Users/florimond/Developer/python-projects/uvicorn/venv/bin/uvicorn", line 33, in <module>
    sys.exit(load_entry_point('uvicorn', 'console_scripts', 'uvicorn')())
  File "/Users/florimond/Developer/python-projects/uvicorn/venv/lib/python3.9/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/Users/florimond/Developer/python-projects/uvicorn/venv/lib/python3.9/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/Users/florimond/Developer/python-projects/uvicorn/venv/lib/python3.9/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/Users/florimond/Developer/python-projects/uvicorn/venv/lib/python3.9/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/Users/florimond/Developer/python-projects/uvicorn/uvicorn/main.py", line 362, in main
    run(**kwargs)
  File "/Users/florimond/Developer/python-projects/uvicorn/uvicorn/main.py", line 386, in run
    server.run()
  File "/Users/florimond/Developer/python-projects/uvicorn/uvicorn/server.py", line 48, in run
    loop.run_until_complete(self.serve(sockets=sockets))
  File "uvloop/loop.pyx", line 1456, in uvloop.loop.Loop.run_until_complete
  File "/Users/florimond/Developer/python-projects/uvicorn/uvicorn/server.py", line 69, in serve
    await self.shutdown(sockets=sockets)
  File "/Users/florimond/Developer/python-projects/uvicorn/uvicorn/server.py", line 221, in shutdown
    connection.shutdown()
  File "/Users/florimond/Developer/python-projects/uvicorn/uvicorn/protocols/websockets/wsproto_impl.py", line 115, in shutdown
    output = self.conn.send(wsproto.events.CloseConnection(code=1012))
  File "/Users/florimond/Developer/python-projects/uvicorn/venv/lib/python3.9/site-packages/wsproto/__init__.py", line 61, in send
    data += self.handshake.send(event)
  File "/Users/florimond/Developer/python-projects/uvicorn/venv/lib/python3.9/site-packages/wsproto/handshake.py", line 100, in send
    raise LocalProtocolError(
wsproto.utilities.LocalProtocolError: Event CloseConnection(code=1012, reason=None) cannot be sent during the handshake
Read more comments on GitHub >

github_iconTop Results From Across the Web

RFC 6455: The WebSocket Protocol
1. If the connection is happening on an HTTPS (HTTP-over-TLS) port, perform a TLS handshake over the connection. · 2. The server can...
Read more >
uvicorn - Bountysource
0 likes. uvicorn eats SIGINTs, does not propagate exceptions paid out closed ; 0 likes. Handshaking may not be completed yet at `shutdown`...
Read more >
ASGI Documentation
chronous apps, with a WSGI backwards-compatibility implementation and ... multiple incoming events (like receiving WebSocket frames) can't trigger this.
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