Skip to content

Do not log a traceback when a TLS client disconnects mid-response - #1261

Open
Yohan-Senadheera wants to merge 1 commit into
prometheus:masterfrom
Yohan-Senadheera:ignore-tls-eof
Open

Yohan-Senadheera wants to merge 1 commit into
prometheus:masterfrom
Yohan-Senadheera:ignore-tls-eof

Conversation

@Yohan-Senadheera

Copy link
Copy Markdown

Fixes #983.

When a client drops the connection while /metrics is being written, wsgiref ignores BrokenPipeError and ConnectionResetError. Over TLS the same disconnect raises ssl.SSLEOFError, which wsgiref does not ignore, so handle_error() prints a full traceback. The server keeps working, but the traceback looks like a crash (as in #983).

This adds a ServerHandler subclass whose handle_error() ignores SSLEOFError the same way, and _SilentHandler.handle() uses it. handle() is the same as WSGIRequestHandler.handle() apart from the handler class.

Tested with start_http_server(certfile=..., keyfile=...) and a TLS client that resets the connection after the first bytes, three times:

  • master: 3 tracebacks ending in ssl.SSLEOFError: EOF occurred in violation of protocol
  • this branch: nothing on stderr; the next scrape still returns 200

The new tests check that SSLEOFError is not logged and that other errors still are. The full test suite, flake8 and isort pass.

wsgiref ignores BrokenPipeError and ConnectionResetError when a client
goes away during a response, but over TLS the same disconnect raises
ssl.SSLEOFError, which is logged with a full traceback even though the
server keeps working. Treat it like the other disconnect errors.

Fixes prometheus#983

Signed-off-by: Yohan Senadheera <yohansenadheera@gmail.com>
Copilot AI balanced review requested due to automatic review settings November 18, 2026 02:45

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@chrikrah chrikrah left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing blocks a merge: at 9d90462 TLS stays quiet where master prints an SSLEOFError traceback.

$ python inject.py tests/certs   # start_wsgi_server with TLS, _SocketWriter.write raises SSLEOFError once
master c0f1bdf: stderr lines: 17 | SSLEOFError tracebacks: 1 | next scrape: 200
head 9d90462:   stderr lines: 0  | SSLEOFError tracebacks: 0 | next scrape: 200
$ pytest -q tests   # Python 3.12.14 and 3.14.7, head
493 passed, 16 skipped, 32 subtests passed
# guard removed from handle_error: test_dropped_tls_connection_is_not_logged fails
$ flake8 prometheus_client/ tests/ && isort --check prometheus_client/ tests/   # flake8 7.3, isort 5.10.1: exit 0
# not run: mypy env, pypy

non-blocking: the copied handle() matches stdlib on 3.9 to 3.15. Pin that with an inspect.getsource test, or skip it?

@Yohan-Senadheera

Copy link
Copy Markdown
Author

Nothing blocks a merge: at 9d90462 TLS stays quiet where master prints an SSLEOFError traceback.

$ python inject.py tests/certs   # start_wsgi_server with TLS, _SocketWriter.write raises SSLEOFError once
master c0f1bdf: stderr lines: 17 | SSLEOFError tracebacks: 1 | next scrape: 200
head 9d90462:   stderr lines: 0  | SSLEOFError tracebacks: 0 | next scrape: 200
$ pytest -q tests   # Python 3.12.14 and 3.14.7, head
493 passed, 16 skipped, 32 subtests passed
# guard removed from handle_error: test_dropped_tls_connection_is_not_logged fails
$ flake8 prometheus_client/ tests/ && isort --check prometheus_client/ tests/   # flake8 7.3, isort 5.10.1: exit 0
# not run: mypy env, pypy

non-blocking: the copied handle() matches stdlib on 3.9 to 3.15. Pin that with an inspect.getsource test, or skip it?

Thanks for testing it! I'd skip the getsource pin. It would fail on any harmless stdlib change in a patch release, and test_dropped_tls_connection_is_not_logged already fails if the guard is removed, so the behaviour is covered. Happy to add it if a maintainer prefers.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Exceptions not handled in server thread

3 participants