Skip to content

fix(dev): HTTP/1.1 keep-alive in HA proxy (run.py) - #1391

Merged
kalbasit merged 2 commits into
mainfrom
user/wnasreddine/fix-proxy-keepalive
Jun 10, 2026
Merged

kalbasit merged 2 commits into
mainfrom
user/wnasreddine/fix-proxy-keepalive

Conversation

@kalbasit

Copy link
Copy Markdown
Owner

Summary

A second, distinct dev-harness bug from the chunked-body fix in #1390 (which is necessary and stays). After #1390, nix copy of a large closure (e.g. a ~9.35 GB nixos-system) to the :8500 HA proxy still intermittently fails with curl error 56: Connection reset by peer, aborting the whole copy on the first reset.

Root cause: the proxy's ProxyHandler left protocol_version at the stdlib BaseHTTPRequestHandler default of HTTP/1.0, so it force-closed every client connection after one request. nix copy pushes hundreds of objects over a small pool of HTTP/1.1 keep-alive connections; because the proxy refused keep-alive, nix was forced to open a brand-new TCP connection per object — thousands for a multi-GB closure. Under that churn (amplified by the default listen backlog of 5 and a ~1 s-per-PUT stall from unanswered Expect: 100-continue), the accept loop stalls and intermittently nix's upload connection is reset before the proxy ever handles it (curl error 56).

Reproduced & fix-validated on a clean isolated ncps behind the real proxy: the unpatched proxy dies at PUT #2; the patched proxy completes 3,488 PUTs with 0 resets. curl -v confirms HTTP/1.1 200 + "Connection left intact" (reuse).

What changed (dev-scripts/run.py proxy only; ncps unchanged)

  • ProxyHandler.protocol_version = "HTTP/1.1" — persistent, reused client connections; also makes BaseHTTPRequestHandler auto-answer Expect: 100-continue, removing the per-PUT stall.
  • Response framing for safe reuse: preserve Content-Length; treat 204/304/HEAD/1xx as bodiless; send Connection: close when the body length is unknown (e.g. backend chunked, stripped as hop-by-hop) so the client reads to EOF instead of mis-framing the next response.
  • Raise the listener backlog (request_queue_size) above the stdlib default of 5, set before the socket is activated.
  • All fix(dev): forward chunked request bodies in HA proxy #1390 hardening preserved (chunked request-body forwarding, RFC 7230 §3.3.3, bounded _fail drain, error logging).

Test plan

  • task fmt (0 changed), task lint (0 issues), task test (all Go packages ok)
  • New keep-alive / framing / Expect: 100-continue regression tests drive the real run.py proxy via NCPS_RUN_PY; confirmed RED on HTTP/1.0, GREEN after the fix
  • e2e-harness-unit nix check green (52 passed; +4 new tests)
  • End-to-end: full nix copy of the 9.35 GB closure through the patched proxy — 3,488 PUTs, 0 resets

The dev HA reverse proxy on :8500 left protocol_version at the stdlib
HTTP/1.0 default, so it force-closed every client connection after one
request. nix copy of a large closure pushes hundreds of objects over a
small pool of HTTP/1.1 keep-alive connections; because the proxy refused
to keep any connection alive, nix was forced to open a brand-new TCP
connection per object -- thousands for a multi-GB closure. Under that
churn (amplified by the default listen backlog of 5 and a ~1s-per-PUT
stall from unanswered Expect: 100-continue), the accept loop stalls and
intermittently nix's upload connection is reset before the proxy ever
handles it, surfacing as 'curl error 56: Connection reset by peer' and
aborting the whole copy.

This is a second, distinct bug from the chunked-body fix in #1390
(which stays). Reproduced on a clean isolated ncps behind the real
proxy and fix-validated: 3488 PUTs, 0 resets vs the unpatched proxy
dying at PUT #2.

Fix (dev-scripts/run.py proxy only; ncps unchanged):
- Set ProxyHandler.protocol_version = 'HTTP/1.1' so client connections
  are persistent and reused; this also makes BaseHTTPRequestHandler
  auto-answer Expect: 100-continue, removing the per-PUT stall.
- Frame forwarded responses for safe reuse: preserve Content-Length,
  treat 204/304/HEAD/1xx as bodiless, and send Connection: close when
  the body length is unknown so the client reads to EOF.
- Raise the listener backlog (request_queue_size) above the default 5.

Adds keep-alive/framing/Expect regression tests that drive the real
run.py proxy via NCPS_RUN_PY (wired into the e2e-harness-unit check).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@kalbasit

kalbasit commented Jun 10, 2026 •

Copy link
Copy Markdown
Owner Author

This change is part of the following stack:

Change managed by git-spice.

@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. bug Something isn't working labels Jun 10, 2026
@coderabbitai

coderabbitai Bot commented Jun 10, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 67470c90-7845-4b18-a091-db5acf1855f1

📥 Commits

Reviewing files that changed from the base of the PR and between 341d0da and 10f60ae.

📒 Files selected for processing (2)
  • dev-scripts/run.py
  • nix/e2e-tests/tests/test_dev_proxy.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • dev-scripts/run.py

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes

    • Fixed intermittent failures during large uploads through the development reverse proxy by improving HTTP keep-alive handling and response framing; now forces connection close when response length is indeterminate.
    • Increased proxy listen backlog to better absorb bursts of simultaneous connections.
  • Tests

    • Added socket-level regression tests for HTTP/1.1 connection reuse, response framing, and Expect: 100-continue behavior.
  • Documentation

    • Added design, proposal, tasks, and spec entries clarifying proxy HTTP/1.1 persistence and framing requirements.

Walkthrough

The PR updates the dev HA reverse proxy to support HTTP/1.1 persistent connections and safe response framing. The proxy handler now uses HTTP/1.1 to enable keep-alive, detects bodiless and Content-Length-delimited responses, and signals Connection: close when body length is unknown. A larger server backlog absorbs connection bursts, and new regression tests verify keep-alive reuse, correct framing, and Expect: 100-continue handling.

Changes

Dev Proxy HTTP/1.1 Keep-Alive Fix

Layer / File(s) Summary
Proxy HTTP/1.1 Protocol and Response Framing
dev-scripts/run.py
Proxy handler explicitly sets protocol_version to HTTP/1.1. Response forwarding records Content-Length presence and classifies bodiless responses (204, 304, 1xx, HEAD); when neither applies, the proxy sends Connection: close and closes after the body to avoid mis-framing subsequent requests. start_proxy uses a _ProxyServer subclass with request_queue_size = 256.
Regression tests and raw-socket helpers
nix/e2e-tests/tests/test_dev_proxy.py
_CaptureHandler.do_GET() serves /upload/framed (with Content-Length) and /upload/unframed (no Content-Length). Adds _read_response_head() and _send_put() socket helpers and tests for: connection reuse across sequential requests, Expect: 100-continue interim response, client Connection: close echoing, unframed GET forcing connection close and EOF body, and framed GET preserving keep-alive for the next request.
Specification, design, and tasks
openspec/changes/archive/2026-06-10-fix-dev-proxy-http10-keepalive/{.openspec.yaml, design.md, proposal.md, specs/dev-ha-proxy/spec.md, tasks.md}, openspec/specs/dev-ha-proxy/spec.md
Adds OpenSpec archive metadata, a design document describing the HTTP/1.1 + framing approach and trade-offs, a proposal, TDD tasks, and a main spec section formalizing HTTP/1.1 keep-alive and safe response framing requirements (preserve Content-Length/bodeless responses; fall back to Connection: close when body length is unknown).

Sequence Diagram

sequenceDiagram
  participant Client
  participant ProxyHandler
  participant Backend
  Client->>ProxyHandler: PUT/GET (HTTP/1.1)
  ProxyHandler->>Backend: Forward request
  Backend->>ProxyHandler: Response (with or without Content-Length)
  alt Response has Content-Length or is bodiless (204/304/1xx/HEAD)
    ProxyHandler->>Client: Forward with Connection: keep-alive
  else Response body length unknown (no Content-Length, not bodiless)
    ProxyHandler->>Client: Forward with Connection: close
    Client->>Client: Read to EOF (body terminates)
  end
  Client->>ProxyHandler: Next request on same socket (if keep-alive)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • kalbasit/ncps#1389: Both PRs modify the HA round-robin reverse proxy implementation in dev-scripts/run.py; this PR builds on that proxy work with HTTP/1.1 keep-alive and framing fixes.

Poem

🐰 I hopped past sockets, headers, and queues,
Swapped to HTTP/1.1 for fewer sad news,
When bodies have length the connection stays near,
If not, I close gently so messages clear —
A tiny rabbit patch to keep streams sincere.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title 'fix(dev): HTTP/1.1 keep-alive in HA proxy (run.py)' clearly and concisely summarizes the main change: enabling HTTP/1.1 keep-alive in the dev proxy by fixing protocol_version in run.py.
Description check ✅ Passed The description is directly related to the changeset, providing detailed context about the HTTP/1.0 root cause, the fix applied, validation performed, and specific improvements made to the proxy handler.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Comment @coderabbitai help to get the list of available commands and usage tips.

Comment thread nix/e2e-tests/tests/test_dev_proxy.py Dismissed
Comment thread nix/e2e-tests/tests/test_dev_proxy.py Dismissed
Comment thread nix/e2e-tests/tests/test_dev_proxy.py Dismissed
Comment thread nix/e2e-tests/tests/test_dev_proxy.py Dismissed
Comment thread nix/e2e-tests/tests/test_dev_proxy.py Dismissed
Comment thread nix/e2e-tests/tests/test_dev_proxy.py Dismissed
Comment thread nix/e2e-tests/tests/test_dev_proxy.py Dismissed
Comment thread nix/e2e-tests/tests/test_dev_proxy.py Dismissed
Comment thread nix/e2e-tests/tests/test_dev_proxy.py Dismissed
Comment thread nix/e2e-tests/tests/test_dev_proxy.py Dismissed

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request updates the dev HA proxy in dev-scripts/run.py to support HTTP/1.1 persistent connections, which resolves connection reset issues during large uploads. It introduces response framing to safely handle keep-alive connections, increases the listener backlog, and adds comprehensive end-to-end tests and specifications. The review feedback correctly points out that the proxy should explicitly send a Connection: close header if the connection is already marked for closure, aligning with RFC 7230.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread dev-scripts/run.py Resolved
When the proxy will close the connection for any reason — the client
sent Connection: close, or it is an HTTP/1.0 client — RFC 7230 §6.3
requires the response to carry Connection: close so a keep-alive client
does not assume the connection persists and reuse it (the same class of
mis-framing this PR fixes). Previously the proxy only emitted it for an
unframed (unknown-length) response; now it also emits it whenever
self.close_connection is already set. Adds a regression test asserting a
client Connection: close request gets Connection: close echoed even on a
delimitable (204) response.

Addresses Gemini review feedback on PR #1391
(thread PRRT_kwDONV6tZs6InEEx).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Comment thread nix/e2e-tests/tests/test_dev_proxy.py Dismissed
Comment thread nix/e2e-tests/tests/test_dev_proxy.py Dismissed
Comment thread nix/e2e-tests/tests/test_dev_proxy.py Dismissed
Comment thread nix/e2e-tests/tests/test_dev_proxy.py Dismissed
@kalbasit
kalbasit merged commit 14fba4e into main Jun 10, 2026
46 of 59 checks passed
@kalbasit
kalbasit deleted the user/wnasreddine/fix-proxy-keepalive branch June 10, 2026 20:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants