Skip to content

fix(apiv2): send the response to a client that half-closed its connection - #2693

Merged
Quick104 merged 1 commit into
Silo-Server:mainfrom
Rhainland:fix/2390-v2-half-close-response
Oct 21, 2026
Merged

Quick104 merged 1 commit into
Silo-Server:mainfrom
Rhainland:fix/2390-v2-half-close-response

Conversation

@Rhainland

@Rhainland Rhainland commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

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 OK instead of the operation's response, losing the body and, for a 201 or 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.flush treated a canceled context as a client that had gone and dropped a success response, so the handler wrote nothing and net/http sent its implicit 200 with Content-Length: 0. First-party apps and common HTTP clients don't half-close, so this hits raw clients and some proxies.

Approach

flush writes 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

  • Socket-level test that half-closes after a complete request: before the change it got 200 OK with an empty body; it now gets the handler's 201 and body.
  • Local server, 1000 half-closed GET /api/v2/system/info requests: 102 and 446 empty 200 responses 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.
  • Tests for the packages that import apiv2, and for internal/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.
  • Not run: the full 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 base 365e3d436 and at this branch's 7d35a5053. In 1000 requests, 446 got an empty 200 before the change and none after.

Before (365e3d436), one of the empty responses:

HTTP/1.1 200 OK
Cache-Control: no-cache
Content-Type: application/json
X-Request-ID: 8da6c138de5820fcb68ae286
Date: Mon, 19 Oct 2026 12:17:31 GMT
Content-Length: 0
Connection: close

After (7d35a5053):

HTTP/1.1 200 OK
Cache-Control: no-cache
Content-Type: application/json
X-Request-ID: 1395948da643ec424bbf66c3
Date: Mon, 19 Oct 2026 12:17:50 GMT
Content-Length: 250
Connection: close

{"server_version":"unavailable","api_major":2,"contract_digest":"66d65498d73f264f6b5b12894ca1ad7aa2901f7459b580dffeb29b224cb6bc24","links":{"openapi":"/api/v2/openapi.json","capabilities":"/api/v2/capabilities","identity":"/api/v2/system/identity"}}

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

  • I read and can explain the complete diff.
  • This pull request addresses one concern.
  • The Evidence section shows every change a user can see, or says there is none.

AI Disclosure

  • Harness: Claude Code in T3 Code
  • Tool(s): Claude Code; Codex CLI (review only)
  • Model(s): claude-opus-5-5; gpt-6.1-sol (review only)
  • Involvement: Fully AI-generated, human verified
  • Adversarial review: two independent reviews of the branch diff (claude-opus-5-5 and gpt-6.1-sol, medium effort); neither found an issue.

Note

Fix apiv2.bufferedWriter.flush to deliver responses to half-closed clients

  • The buffered middleware previously dropped successful responses (status < 400) when the request context was canceled. Now the deferred flush in bufferResponse always writes the buffered response, even after the client half-closes its connection or the context ends.
  • Removes the request-context field from bufferedWriter and updates statusClass docs to classify post-departure server errors as abandoned. See router.go.
  • Adds tests covering a canceled request that still records a 201 response and a raw TCP client that half-closes and still receives the created status and body. See canceled_response_test.go.
  • Behavioral Change: successful responses are now emitted after request cancellation instead of being left unwritten; TestCanceledRequestStillWritesResponse now asserts a 200 with body rather than an empty recorder.

Macroscope summarized 7d35a50.

…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-triage silo-triage Bot added the size/M 50–199 added + deleted lines (all files); change volume, not priority. label Oct 19, 2026
@silo-kody

silo-kody Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Silo Kody — review complete

Review finished. Check the inline comments for findings and verify each suggestion against the code and tests.

Reviewing changes in Silo
  • Include the related issue, expected behavior, and validation steps in the PR description.
  • For API changes, describe the effect on Apple and Android clients and Jellyfin compatibility.
  • For plugin changes, identify the affected SDK contract, plugin, and catalog entry.
  • Follow this repository's AGENTS.md and CONTRIBUTING.md.
  • Request another review with @kody start-review in a PR comment.
  • React with 👍 or 👎 to give feedback on individual suggestions.
Review settings
Review Options

The following review options are enabled or disabled:

Options Enabled
Bug ✅
Performance ✅
Security ✅
Business Logic ❌

@macroscopeapp

macroscopeapp Bot commented Oct 19, 2026

Copy link
Copy Markdown

The v2 API response to a half-closed request now carries the operation’s status and body instead of an implicit empty 200 OK; add before-and-after response excerpts for the same request, naming the surface and builds or commits. Automated check: Macroscope check run agent (gpt-6-luna). Evidence was not reviewed for correctness.

Posted via Macroscope — Visible change evidence

@coderabbitai

coderabbitai Bot commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You'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.

Check out review usage here.

View limit details

Limit details: You’ve used all 4 included reviews currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ebca6ec3-df2c-4955-b28b-e37c8ac74ea6
📥 Commits

Reviewing files that changed from the base of the PR and between 365e3d4 and 7d35a50.

📒 Files selected for processing: 4
  • internal/apiv2/canceled_response_test.go
  • internal/apiv2/observe.go
  • internal/apiv2/router.go
  • internal/apiv2/router_test.go
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

❤️ Share

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

@silo-triage silo-triage Bot added api-v2 API v2 / Huma migration program (epic #135); long-running, stacked by phase bug Something isn't working evidence-required Needs before-and-after evidence before the Evidence check passes go Pull requests that update go code impact: compat Breaks a supported client or server path priority: P2 Limited scope, workaround exists, or polish labels Oct 19, 2026
@Quick104

Copy link
Copy Markdown
Contributor

Review summary

Reviewed head: 7d35a5053

I found nothing to fix, so this branch has no new commits.

What I checked

  • The diff against its callers. observe uses activitylog.RecordedStatus, so a success on a canceled request is now recorded with the status it actually sent. A server error, or a request that wrote nothing, is still recorded as abandoned (499). The activity log's committed-row dedup now compares against the status that was really sent, so a canceled mutation no longer gets a 499 row next to its domain row.
  • Paths that could change: 204/304/HEAD, the 408 body-read timeout, panic recovery, and ErrAbortHandler all behave as before. Writing to a client that really left only fails silently, and the body is a bounded JSON document. v1 and jellycompat already write their JSON without a cancellation check, so v2 now matches them.
  • Handlers that return a partial result after cancellation: before this change, a half-closed client got an empty 200 for those, so dropping the gate does not weaken any completeness guarantee.
  • Closing the connection with panic(http.ErrAbortHandler) instead: that would discard a completed answer and leave the client unsure whether a mutation succeeded. Writing the response is the better repair.

Tests

  • TestCanceledClientSuccessIsRecorded, TestHalfClosedClientGetsSuccess, and TestCanceledRequestStillWritesResponse each fail on the merge base 365e3d436 for the reported reason (an empty 200 where the handler wrote a body, or 201) and pass on the head.
  • go test -race ./internal/apiv2/ ./internal/activitylog/ passes on the head.

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

  • Fit: a correctness fix at the boundary that caused the problem. It adds no setting or API surface, and it makes v2 match v1 and the v2 raw routes.
  • Readiness: the regression tests fail on the base and pass on the head, the race tests pass, CI is green, and neither review found a defect.

Review by Claude Code (claude-opus-5-5) with a cross-provider round from Codex (gpt-6.1-sol).

@Quick104
Quick104 merged commit 768011e into Silo-Server:main Oct 21, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api-v2 API v2 / Huma migration program (epic #135); long-running, stacked by phase bug Something isn't working evidence-required Needs before-and-after evidence before the Evidence check passes go Pull requests that update go code impact: compat Breaks a supported client or server path priority: P2 Limited scope, workaround exists, or polish size/M 50–199 added + deleted lines (all files); change volume, not priority.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] v2 sends an empty 200 instead of the response to a client that half-closed its connection

2 participants