python-websockets / websockets

Library for building WebSocket servers and clients in Python
https://websockets.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
5.23k stars 519 forks source link

EOFError: stream ends after 0 bytes #1551

Closed castelo-software closed 1 week ago

castelo-software commented 1 week ago

I am running a containerized WebSocket service on Azure Container Apps, and while everything actually works, my logs get full with handshake errors, even when no requests are being sent to the server.

I'm hoping someone can help me identify which requests are failing.

The error (which is raised non-stop) is the following:

opening handshake failed
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/websockets/http11.py", line 134, in parse
    request_line = yield from parse_line(read_line)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/websockets/http11.py", line 380, in parse_line
    line = yield from read_line(MAX_LINE_LENGTH)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/websockets/streams.py", line 46, in read_line
    raise EOFError(f"stream ends after {p} bytes, before end of line")
EOFError: stream ends after 0 bytes, before end of line

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/websockets/asyncio/server.py", line 353, in conn_handler
    await connection.handshake(
  File "/usr/local/lib/python3.12/site-packages/websockets/asyncio/server.py", line 204, in handshake
    raise self.protocol.handshake_exc
  File "/usr/local/lib/python3.12/site-packages/websockets/server.py", line 551, in parse
    request = yield from Request.parse(
              ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/websockets/http11.py", line 136, in parse
    raise EOFError("connection closed while reading HTTP request line") from exc
EOFError: connection closed while reading HTTP request line

This happens before the process_request even gets the opportunity to run, so I haven't been able to log any information about the request.

Important to note, that this does not happen when the container runs locally.

The actual requests made by my client go through and work without any issues.

aaugustin commented 1 week ago

This is almost certainly a TCP healthcheck run automatically by Azure to tell if your app is alive. Azure makes a TCP connection to see if you app is alive and closes it immediately, which is why websockets cannot read anything from the connection. I recommend that you switch to an HTTP healthcheck.