Skip to content

fix(protocol): keep direct RPC connections alive while awaiting a reply - #883

Open
morningducke wants to merge 6 commits into
scaleapi:mainfrom
morningducke:fix/protocol-rpc-tcp-keepalive
Open

morningducke wants to merge 6 commits into
scaleapi:mainfrom
morningducke:fix/protocol-rpc-tcp-keepalive

Conversation

@morningducke

@morningducke morningducke commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

What and why

Long-running data-plane RPCs can lose an idle direct TCP connection while the environment prepares its reply. Enable keepalive on the direct connections used by data/add, data/get and data/reset. Where the kernel supports them, probing starts after 30 seconds of inactivity with a 10-second interval and three probes.

Apply each setting to the connected socket. A kernel can expose a setting to Python and still reject it on that socket. The remaining settings and the RPC continue in that case. HTTPX handles proxy routing, bypasses and TLS verification.

Verification

  • Ten of the sixteen direct, proxy, bypass and TLS regression cases failed on main at e4e8151. All sixteen passed with this change on HTTPX 0.27.0 and 0.28.1. A loopback peer received one request when the socket rejected TCP_KEEPINTVL, and observed the connection close.
  • The protocol suite passed 2,107 tests with one skip. Ruff passed.
  • Compared stock and patched native data/add clients against one temporary E2B server. Both short controls succeeded. For the paired long calls the server waited 315 seconds before sending headers and completed both writes. The stock client raised ReadTimeout after 361.461 seconds. The patched client received its result after 315.686 seconds.
  • Gateway shard 5 stopped while pulling nginx:alpine from the Docker mirror with HTTP 429. GitHub requires an upstream repository administrator to rerun that job. Failed gateway job

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; no actionable new defects were found.

Summary

Adds TCP keepalive to data-plane RPC connections while leaving proxy routing and TLS settings to HTTPX.

  • Data RPC connections stay alive while the client waits for a reply.

No new actionable issues were found. The earlier unnumbered finding about unnamed settings is addressed.

Reviews (4) · Last reviewed commit: "refactor(protocol): set RPC keepalive fr..." · Reviewed by Greptile

@morningducke
morningducke marked this pull request as ready for review November 24, 2026 13:39
@morningducke
morningducke requested a review from a team as a code owner November 24, 2026 13:39
morningducke and others added 3 commits November 24, 2026 16:51
Set the keepalive options from the `trace` request extension on `client.post` in `_rpc` instead of a custom
default transport. HTTPX keeps its own proxy discovery, so `_DirectTransport` and the private
`httpx._utils.get_environment_proxies` import go, and the hook covers every connection the RPC opens, a
proxy's included: behind a proxy whose client-side hop drops idle connections, a long reply now arrives
instead of timing out.

The routing tests now also expect the keepalive options on proxied connections.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@earakely-scale

Copy link
Copy Markdown
Collaborator

🤖 Drafted by Claude Code, reviewed by Edgar Arakelyan before posting.

Thanks, this fixes a real failure. We chaos-tested it against main, and against a smaller variant that passes the same hook per request as client.post(..., extensions={"trace": _keepalive}) with no custom transport.

  • From a Modal sandbox (default runtime): main lost every reply that took 300 s or more (0/10, replies at 300–1200 s; fine at 280 s). This PR and the variant both got 10/10.
  • Behind a NAT that forgets idle connections after 60 s: main got 0/20 on 90 s and 315 s replies; both fixes got 20/20 on httpx 0.27.0 and 0.28.1. Both also cut dead-peer detection from 300 s to 61 s, with no latency cost.
  • Through an HTTP proxy (HTTP_PROXY, HTTPS_PROXY or ALL_PROXY) with the same idle drop: this PR got 0/15 and the variant 15/15. The variant sets keepalive on whatever connection HTTPX opens, including the one to the proxy.
  • Everywhere else the two behave the same: refused options, NO_PROXY, and the protocol suite.

I pushed the variant to this branch as 2d3652a. It removes _DirectTransport and the private httpx._utils.get_environment_proxies import. It keeps your routing and TLS tests, which now also expect keepalive on proxied connections. _rpc doesn't follow redirects, so there's no later hop to scope it to. Happy to revert if you see a reason to keep the transport.

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.

2 participants