-
-
Notifications
You must be signed in to change notification settings - Fork 540
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
process_request
returning response runs connection handler
#1419
Comments
I noticed that it is receiving empty bytes as a data response, and this causes the socket to close. websockets/src/websockets/sync/connection.py Lines 549 to 550 in e217458
websockets/src/websockets/sync/connection.py Lines 614 to 616 in e217458
Another thing is that when the status code is not 101, the state does not end up being OPEN, which then closes the connection as well. websockets/src/websockets/server.py Lines 541 to 547 in e217458
websockets/src/websockets/sync/server.py Lines 143 to 146 in e217458
I have made this PR that solved this error for me, but I don't know if it broke tests or other functinalities |
The change causes tests to hang -- because you removed the code paths that manages the end of a connection. The issue looks legit but that's not the right fix. |
I think I figured it out now. The code is handling all connections as WebSocket connections. When it is an HTTP connection and a request is made, it is supposed to close the connection, which differs from a WebSocket, where it is expected to keep the connection alive. I created a checker to verify if it is a WebSocket before handling it. I have created this new PR with this change: |
@MauroAntonino - the approach in your PR is generally correct; however it can be simplified to Also, we leak an open socket in that case. We should close it. |
process_request
raise ConnectionClosedErrorprocess_request
returning response runs connection handler
Rephrased title to reflect the fact that this issue also affects the new asyncio implementation |
When process_request or process_response returned a HTTP response with a status code other than 101, the connection handler used to start, which was incorrect. Fix #1419.
When process_request() or process_response() returned a HTTP response without calling accept() or reject() and with a status code other than 101, the connection handler used to start, which was incorrect. Fix #1419. Also move start_keepalive() outside of handshake() and bring it together with starting the connection handler, which is more logical.
After further analysis, it turns out that the problem happens when returning a In that case, The fix for this problem actually belongs in the Sans-I/O layer. By setting This makes the logic robust to (1) creating a response "manually" and (2) creating and not sending a response, which could cause the opposite problem. |
When process_request() or process_response() returned a HTTP response without calling accept() or reject() and with a status code other than 101, the connection handler used to start, which was incorrect. Fix #1419. Also move start_keepalive() outside of handshake() and bring it together with starting the connection handler, which is more logical.
When process_request() or process_response() returned a HTTP response without calling accept() or reject() and with a status code other than 101, the connection handler used to start, which was incorrect. Fix #1419. Also move start_keepalive() outside of handshake() and bring it together with starting the connection handler, which is more logical.
Almost there... needs a test for the behavior change in the Sans-I/O layer. |
Hello, I'm using 13.1 and the below process_request for a health check and I'm still seeing these errors pop up in logs. Is there something I should be doing differently? async def process_request(
self, connection: websockets.asyncio.server.ServerConnection, request: Request
) -> Response | None:
global HEALTH_PING
if request.path == "/health":
if not HEALTH_PING:
logger.info("First health ping received")
HEALTH_PING = True
return connection.respond(http.HTTPStatus.OK, "Ok") error message received:
|
Yes, I noticed that as well last week, it's less than ideal. I'm going to file a new issue about this. |
I'm using:
When using the threading implementation like in this code:
Any call to the /healthz endpoint works but raise a
ConnectionCloseError
:The text was updated successfully, but these errors were encountered: