-
-
Notifications
You must be signed in to change notification settings - Fork 754
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Incorrect websocket close code on server shutdown #1344
Comments
I just reproduced the issue and confirmed the code 1006 is used even after commenting out diff --git a/uvicorn/protocols/websockets/websockets_impl.py b/uvicorn/protocols/websockets/websockets_impl.py
index 9a07a5d..a90dc1c 100644
--- a/uvicorn/protocols/websockets/websockets_impl.py
+++ b/uvicorn/protocols/websockets/websockets_impl.py
@@ -97,7 +97,7 @@ class WebSocketProtocol(websockets.WebSocketServerProtocol):
def shutdown(self):
self.ws_server.closing = True
- self.transport.close()
+ # self.transport.close()
def on_task_complete(self, task):
self.tasks.discard(task) The terminal output ^CINFO: Shutting down
INFO: Waiting for connections to close. (CTRL+C to force quit)
DEBUG: ! failing connection with code 1006
DEBUG: = connection is CLOSED
DEBUG: x half-closing TCP connection
INFO: connection closed
INFO: Finished server process [3676] |
It will be available on Uvicorn |
@Kludex this is still an issue unfortunately, even with Here's what I'm seeing:
Server output:
Client output:
This is happening for both the |
Can you please try with another client? The server logs point out that we are sending 1012, which is the intention. |
I've tested with https://github.com/websockets/wscat, and indeed, this is still happening, but only on |
It does solve. I've added tests to check what the client receives. 🙏 |
Indeed, it solves it with Update: |
Checklist
master
.Describe the bug
This seems related to #1140 but may be a simpler issue.
When the server is shutdown with
SIGTERM
, websocket connections are terminated with a close code of1006
. This is seen on both the server and the client.According to RFC 6455,
1006
indicates an abnormal connection termination (noClose
control frame) and1001
indicatesGoing Away
.Steps to reproduce the bug
websocket_test.py
with:uvicorn --ws-ping-interval 5 --ws-ping-timeout 5 --log-level debug --lifespan off websocket_test:App
Ctrl+C
or sending aSIGTERM
signal1006
.Expected behavior
uvicorn should send a
Close
frame and the client connection should be closed with code1001
.Actual behavior
Client connection is closed with code
1006
.Debugging material
No response
Environment
uvicorn --ws-ping-interval 5 --ws-ping-timeout 5 --log-level debug --lifespan off websocket_test:App
Additional context
When testing with daphne, the server close code is 1001 and the client close code is 1006.
Looking at the
WebSocketCommonProtocol
, there is aclose
method that properly implements the close handshake. However, it is not called from theWebSocketProtocol
class on shutdown in uvicorn (although it is called as part of unwindinghandler
inWebSocketServerProtocol
). Not callingtransport.close
fromshutdown
may solve the problem, but need to test it.The text was updated successfully, but these errors were encountered: