Skip to content

Unable to suppress traceback when opening handshake fails #1586

Open
@aseiger

Description

@aseiger

While implementing an Asyncio server (with the new API) - I am unable to catch the InvalidMessage exception that can occur. It is correctly logged as an error, but then a traceback occurs which, as far as I can tell, there is no way to suppress.

[ERROR][websockets.server] 2025-01-30 10:30:45,468 opening handshake failed
Traceback (most recent call last):
  File "/home/alex/development/arcbridge/.venv/lib/python3.11/site-packages/websockets/http11.py", line 138, in parse
    request_line = yield from parse_line(read_line)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex/development/arcbridge/.venv/lib/python3.11/site-packages/websockets/http11.py", line 305, in parse_line
    line = yield from read_line(MAX_LINE_LENGTH)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex/development/arcbridge/.venv/lib/python3.11/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 "/home/alex/development/arcbridge/.venv/lib/python3.11/site-packages/websockets/server.py", line 562, in parse
    request = yield from Request.parse(
              ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex/development/arcbridge/.venv/lib/python3.11/site-packages/websockets/http11.py", line 140, in parse
    raise EOFError("connection closed while reading HTTP request line") from exc
EOFError: connection closed while reading HTTP request line

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

Traceback (most recent call last):
  File "/home/alex/development/arcbridge/.venv/lib/python3.11/site-packages/websockets/asyncio/server.py", line 354, in conn_handler
    await connection.handshake(
  File "/home/alex/development/arcbridge/.venv/lib/python3.11/site-packages/websockets/asyncio/server.py", line 205, in handshake
    raise self.protocol.handshake_exc
websockets.exceptions.InvalidMessage: did not receive a valid HTTP request

As seen in the trace above - the conn_handler() function is properly logging the error... but then the traceback print occurs, due to exc_info=True on line 363 of server.py:

async with asyncio_timeout(self.open_timeout):
    try:
        await connection.handshake(
            self.process_request,
            self.process_response,
            self.server_header,
        )
    except asyncio.CancelledError:
        connection.transport.abort()
        raise
    except Exception:
        connection.logger.error("opening handshake failed", exc_info=True)
        connection.transport.abort()
        return

My suggestion is to have exc_info tied to a particular debug flag passed into the serve() call, as this clutters up the logs otherwise.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions