← Back to Pull requests

proxy: end writes that stall when no write timeout is set #351

Closed
unknown closed this pull request without merging on Dec 02, 2026 11:23 UTC
View on GitHub
unknown unknown commented on Dec 01, 2026 08:29 UTC
Author

With writetimeout: 0, which keeps long-lived gRPC streams from being cut, nothing ends a write to a client that cannot complete. A client that never opens its HTTP/2 receive window, or stops reading its socket, keeps the handler and its stream open for as long as it keeps the connection.

Before and after

Before
1. Aperture runs with writetimeout: 0.
2. A client opens a stream, for example GET / or a whitelisted gRPC call, and never opens its receive window, or stops reading its socket.
3. The handler's flush, or the write the server makes after the handler returns, waits.
4. Nothing times it out. The stream and its goroutine stay until the client closes the connection, and one connection can hold many such streams.

After
1. to 3. The same.
4. After writestalltimeout (1m by default) the blocked write fails and the server resets the stream. If the socket itself is not read, the HTTP/2 server closes the connection.
5. A stream that is only quiet between writes, such as a notification stream, is not affected: the deadline is only set while a write is in progress.

Fix

  • A new response writer sets the write deadline before each write and flush and clears it once they return. It wraps every response at the top of ServeHTTP, so preflight and error replies are covered too. It is only used while writetimeout is 0; with a write timeout the rolling window already ends stalled writes.

  • When the handler returns, an HTTP/2 response with body bytes is flushed under the deadline. A response without a body, such as aperture's own gRPC replies and the L402 challenge, is left to the server so its headers still end the stream. HTTP/1 keeps the deadline for the server's last write. Which of the two a response is goes by the server's writer, not the request, since an HTTP/1 request line can claim HTTP/2.0.

  • The writer has Hijack, so the hashmail WebSocket proxy still upgrades.

  • While writetimeout is 0, the same value is the HTTP/2 servers' WriteByteTimeout, for clients that stop reading their socket.

  • The new writestalltimeout option sets it, and 0s turns it off. A negative writetimeout or writestalltimeout is now refused at startup.
  • Tests

  • TestStallTimeoutEndsUnreadResponse: over h2c, a client with a zero receive window gets its stream reset, for a small body, a large body and a streamed backend response. Without the timeout, it does not.

  • TestStallTimeoutEndsUnreadGRPCStream: the same for a gRPC stream, which flushes through http.Flusher.

  • TestStallTimeoutKeepsQuietStream: a gRPC stream quiet for three times the timeout keeps working.

  • TestStallTimeoutKeepsPausedResponse: a response that pauses between writes without flushing keeps working.

  • TestStallTimeoutKeepsGRPCChallenge: a gRPC client still sees the L402 challenge.

  • TestStallTimeoutKeepsHijack: a handler can hijack the connection, and no deadline is left on it.

  • TestStallTimeoutLeavesEndedStreams: completed and cancelled streams are not reset afterwards.

  • TestStallTimeoutLeavesPanics: a handler that panics gets its stream reset with no response, as before.

  • TestStallTimeoutRefusedBodyEndsWithHeaders: a body the server refuses, as for a 204, does not count as written.

  • TestStallTimeoutEndsUnreadPRIRequests: HTTP/1 requests whose request line claims HTTP/2.0, pipelined by a client that never reads, get the connection closed.

  • TestStallTimeoutYieldsToWriteWindow: the stall writer is not used while the rolling window is set.

  • TestH2CServerEndsUnreadSocket: the h2c server closes a connection whose client stops reading its socket.

  • TestWriteTimeoutsConfig: defaults, negative values, and when WriteByteTimeout applies.
  • This does not limit how many streams or connections a client opens, and it does not change deployments with writetimeout set.

    Diff: 480 lines (+360 / -120) across 6 files Reviewer: hieblmi Review duration: 1614m
    All Vouch review confidence checks passed
    Residual risk 0.19 = Change Risk (0.47) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.47 · 480 lines diff across 6 files
    0.47
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.369
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #351"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 47.3% (MEDIUM) · Higher = larger blast radius
    47.3%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 18.7% · Safe to merge
    18.7%