Repository navigation
fix(apiv2): send the response to a client that half-closed its connection - #2693
Conversation
…tion net/http cancels the request context when an HTTP/1.1 client half-closes its side after sending the request, though that client is still reading. bufferedWriter.flush took a canceled context to mean the client had gone and dropped a success response, so net/http answered with an empty 200 OK: the client lost the body, and a 201 or other success status arrived as 200. flush now always writes the buffered response. It is a bounded JSON document, and a client that has really gone never reads it. observe records a canceled success with the status it was sent, as v1 and the v2 raw routes already do; a server error on a canceled request is still recorded as abandoned. TestHalfClosedClientGetsSuccess half-closes a real socket after a complete request and expects the handler's 201 and body; before this change it got "200 OK" with an empty body. Closes Silo-Server#2390
Silo Kody — review completeReview finished. Check the inline comments for findings and verify each suggestion against the code and tests. Reviewing changes in Silo
Review settingsReview OptionsThe following review options are enabled or disabled:
|
|
The v2 API response to a half-closed request now carries the operation’s status and body instead of an implicit empty Posted via Macroscope — Visible change evidence |
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 39 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Review summaryReviewed head: I found nothing to fix, so this branch has no new commits. What I checked
Tests
Cross-provider review: one adversarial round on the head (gpt-6.1-sol, xhigh) found nothing. It checked the same paths, plus HTTP/2 stream resets by reading the source only. Validated features: none affected. Only clients that half-close their connection see a change, and the first-party apps don't. User manual: unaffected. No setting, default, or documented behavior changes. Apple/Android parity: not applicable. This is a server-only fix with no contract change. Remaining for maintainers: none. Operations that query the database still fail for a half-closed client, which #2692 tracks separately. Fit: 9/10 · Readiness: 9/10 — ready
Review by Claude Code (claude-opus-5-5) with a cross-provider round from Codex (gpt-6.1-sol). |
Problem
Closes #2390
Related issue: N/A
Validation tasks: none
A v2 client that half-closes its connection after sending a request can get an empty
200 OKinstead of the operation's response, losing the body and, for a201or other success status, the status too. This change makes v2 always send the response the operation produced.net/http cancels the request context when an HTTP/1.1 client half-closes its write side, although that client is still reading.
bufferedWriter.flushtreated a canceled context as a client that had gone and dropped a success response, so the handler wrote nothing and net/http sent its implicit200withContent-Length: 0. First-party apps and common HTTP clients don't half-close, so this hits raw clients and some proxies.Approach
flushwrites the buffered response whatever the state of the request context. It is a bounded JSON document, and a client that has really gone never reads it. A canceled context can't tell a half-close from a disconnect, so writing is the only option that never sends a status the operation didn't choose.A success on a canceled request is now recorded with the status that was sent, not as abandoned, in the v2 request log, metrics and trace and in the activity log. That matches v1 and the v2 raw routes. A server error on a canceled request is still recorded as abandoned, as in #2077.
Operations that query the database still fail for a half-closed client, because the canceled context cancels their queries too. That is filed separately as #2692.
Validation
200 OKwith an empty body; it now gets the handler's201and body.GET /api/v2/system/inforequests: 102 and 446 empty200responses before across two runs (the count depends on timing), 0 after. 200 requests from clients that reset the connection right after sending were handled without errors.go test -race ./internal/apiv2/: pass.apiv2, and forinternal/activitylog: pass.go build ./...,gofmt,go vet,make lint-changed,make lint-router-recovery,make verify-apiv2-openapi,make verify-apiv2-contract,make verify-local-paths: pass.make test-go(its database tests need Docker for testcontainers) and the web checks (no web changes).Evidence
Native API,
GET /api/v2/system/info, sent by a raw HTTP/1.1 client that half-closes its write side (shutdown(SHUT_WR)) right after the request. The same request went to a local server built at the merge base365e3d436and at this branch's7d35a5053. In 1000 requests, 446 got an empty200before the change and none after.Before (
365e3d436), one of the empty responses:After (
7d35a5053):Risks
Dashboards will show fewer abandoned v2 requests, because canceled successes are now counted under their status class. No contract, migration or client change.
Checklist
AI Disclosure
Note
Fix
apiv2.bufferedWriter.flushto deliver responses to half-closed clientsbufferResponsealways writes the buffered response, even after the client half-closes its connection or the context ends.bufferedWriterand updatesstatusClassdocs to classify post-departure server errors as abandoned. See router.go.TestCanceledRequestStillWritesResponsenow asserts a 200 with body rather than an empty recorder.Macroscope summarized 7d35a50.