Skip to content

Swallow client-hangup errors in the serve HTTP handler#25

Merged
jdoss merged 1 commit intomasterfrom
fix/serve-broken-pipe
Apr 9, 2026
Merged

Swallow client-hangup errors in the serve HTTP handler#25
jdoss merged 1 commit intomasterfrom
fix/serve-broken-pipe

Conversation

@jdoss
Copy link
Copy Markdown
Contributor

@jdoss jdoss commented Apr 9, 2026

Summary

  • Extract a single _write_response() helper in psi/serve.py that both _respond and _respond_error call. It wraps end_headers() and wfile.write(body) in a try/except that swallows BrokenPipeError and ConnectionResetError.
  • Both conditions are normal peer-hangup signals for any HTTP server — previously they escaped into stdlib BaseHTTPRequestHandler and got logged as full tracebacks, polluting journals on every failed auth lookup (curl -sf closes the socket as soon as it sees a non-2xx).

Test plan

  • pytest tests/test_serve.py — new TestClientHangup class: 401 path with a BrokenPipeError-raising wfile and 200 path with ConnectionResetError, asserting do_GET() does not propagate.
  • ruff check / ruff format --check / ty check — clean.

Podman's shell driver uses curl -sf, which exits as soon as it sees a
non-2xx status and closes the socket before the server has finished
writing the response body. The handler's self.wfile.write(body) then
raises BrokenPipeError, which escapes into stdlib BaseHTTPRequestHandler
and is logged as a full traceback. Every failed auth lookup (wrong
token, missing token, 401) generated a fresh stack trace in the journal.

Extract a single _write_response() helper that both _respond and
_respond_error call. Wrap end_headers() and wfile.write() in a
try/except that swallows BrokenPipeError and ConnectionResetError —
normal peer-hangup conditions for any HTTP server.

Tests cover a 401 path with a BrokenPipeError-raising wfile and a 200
path with ConnectionResetError, asserting do_GET() does not propagate.
@jdoss jdoss merged commit 98c9717 into master Apr 9, 2026
2 checks passed
@jdoss jdoss deleted the fix/serve-broken-pipe branch April 9, 2026 15:58
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.

1 participant