POST /sync keeps queued work after the client cancels the request
Problem
On upstream main (3b91159), handleSync derives its work context from the server lifetime alone (ctx := s.ctx). If a client cancels its request while the handler waits for the database executor, the HTTP client returns but the sync remains queued. After the executor becomes available, that abandoned request can perform disk or replica work and delay later requests.
The problem also applies when the client deadline is shorter than the request's explicit server-side timeout. A request with wait: false still performs local database sync; it is not a detached background-job API.
Reproduction
A deterministic test uses a real Unix control socket and one SQLite database:
- Acquire the database executor semaphore in the test harness.
- Send
POST /syncfor the registered database with a cancelable HTTP request context. - Wait until
SyncDiagnostic().ExecutorWaiterCountreports one queued caller. - Cancel the HTTP request and verify that the client returns
context.Canceled. - Assert that the executor waiter count returns to zero while the test still holds the semaphore.
- Release the semaphore and verify that a later sync succeeds. A staging-file hook detects if the abandoned request starts disk work instead.
With the server-context behavior restored through a Go overlay, the test fails at step 5: executor waiters = 1, want 0. With the request-context fix, the waiter is removed and the later sync completes.
Proposed change
Derive the handler context from r.Context(), then connect server shutdown to its cancel function with context.AfterFunc. Apply the request's timeout to this combined context. Stop the callback and release the derived contexts when the handler exits.
This preserves all three cancellation sources: client cancellation, server shutdown, and the request timeout. Waiting requests still receive the existing 30-second default when no timeout is specified. The database and replica code continue to use their existing context checks.
Scope and limitations
Cancellation is cooperative. This change removes queued work and lets context-aware operations stop. It does not forcibly interrupt a filesystem call already blocked in the kernel or roll back an LTX file already committed. Background replication remains independent of the request.
The fork includes separate sync-observability changes. Those can be reviewed independently; they are not required for the context propagation fix.
Relation to #927
Issue #927 concerns disk growth. This change may avoid unnecessary work from abandoned callers, but it does not change WAL-reset snapshot policy, compaction, or retention, and does not claim to fix that issue. The proposed opt-in WAL-reset behavior should be discussed separately.
Tests
The fork's socket regressions cover client cancellation while queued, successful subsequent sync, cancellation during active sync, server shutdown, explicit timeout, and ordinary successful requests:
go test -race -v -run '^TestServer_HandleSync_' .
go test ./...For investigation, TestServer_HandleSync_ClientCancellationDoesNotBlockLaterSync was also run with only its work-context parent changed back from r.Context() to s.ctx; it failed with one abandoned executor waiter. The unmodified fork passes the full suite. The test uses local SQLite and a local replica, without external services.
Source: benbjohnson/litestream