Skip to content

fix: unstick S3 restores under load and stop repo-scoped deletes wiping every entry - #296

Merged
LouisHaftmann merged 11 commits into
devfrom
fix/contributor-stability-fixes
Oct 25, 2026
Merged

LouisHaftmann merged 11 commits into
devfrom
fix/contributor-stability-fixes

Conversation

@LouisHaftmann

Copy link
Copy Markdown
Collaborator

📝 Summary

@chenrui333 reported a set of bugs from running the server under real CI load in #292–#295: restores that hang until every cache step times out, a management delete that ignores repoId and wipes every entry, and 9.9.0 loading all storage SDKs whatever the driver. They had fixes ready but couldn't open PRs here, so this cherry-picks their branches (authorship kept) plus a small round of review cleanup and two extra tests.

Item Fix
#292.3 DELETE /management-api/cache-entries honors repoId instead of deleting everything
#292.1 The merge pump stops waiting for drain on a destroyed stream, so a client hangup or failed merge upload no longer parks it forever
#292.2 No forced full GC after every part (and no --expose-gc); about 10x first-restore throughput at 16 concurrent
#293.1 STORAGE_S3_MAX_SOCKETS (default 50), applied to http:// endpoints too, which previously got an unbounded default agent
#293.2 Separate S3 socket pools for reads, writes and control requests, so streaming Merges can't deadlock the pool
#293.5 The download route destroys the storage body when the client left before storage answered
#293.7 The Postgres startup probe client is released
#294.2 Request, error and passthrough logs drop the query string, which can carry SAS-style credentials
#295 Azure and GCS SDKs load only when their driver is selected (~40 MB less idle heap)

The deadlock in #293.2:

 first restore of an unmerged entry
   GetObject(part)            # paused by backpressure, holds a socket
   upload(merged object)
-    wait for a socket in the same pool   # N merges hold all N sockets: nothing completes
+    socket from the write pool           # reads never block writes
 cache lookup HeadObject
-  queued behind every bulk transfer
+  control pool, fails after STORAGE_S3_CONTROL_QUEUE_TIMEOUT_MS per attempt

New env vars are also exposed in the Helm chart (config.storage.s3.maxSockets, controlMaxSockets, controlQueueTimeoutMs).

✅ Verification

Every new test was run against origin/dev source and fails there:

  • Before (dev):
    • management-api delete by repoId: fails, the other repo's entry is gone
    • download-merge-sink-gone (3 tests): time out, the pump hangs
    • download-client-gone: times out, the body is never destroyed and the reader lease stays
    • db-destroy (Postgres): db.destroy() hangs
    • storage-sdk-loading: loads @azure/identity, @azure/storage-blob, @google-cloud/storage with the filesystem driver
    • s3-socket-pool pool-size test: fails, the http endpoint ignores the pool size
    • merge-socket-pool + lookup HEAD (on S3 pool deadlock, unbounded merges and stalled downloads: fixes from a patched 9.8.0 #293.1 without the split): 4 concurrent first restores on a 4-socket pool deadlock (60 s timeout); a HEAD gets no answer while reads hold the pool
  • After: all pass. Full suite:
DB + storage Result
sqlite + filesystem 72 passed, 8 skipped
sqlite + s3 77 passed, 3 skipped
postgres + filesystem 72 passed, 8 skipped
mysql + azblob 72 passed, 8 skipped
postgres + gcs 72 passed, 8 skipped

type-check, eslint, prettier --check, build and helm template pass. #292.2 has no test: it's a perf change with nothing behavioral to assert; the benchmark is in #292.

🔍 Reviews

Review Found Fixed
Bug review 0 0
Over-engineering review 7 6
Standards review 7 4
Spec review 5 1

Not fixed:

🔗 Issues

Closes #292
Closes #295

Partially addresses #293 (1, 2, 5, 7) and #294 (2).

💥 Merge danger

Door: two-way

No migrations or data changes; every fix reverts cleanly.

Blast Radius: S3 deployments

S3 now opens up to 50 + 50 + 16 sockets instead of 50 shared, and http:// endpoints are bounded for the first time. Cache lookups that wait more than 5 s per attempt for a control socket now fail instead of hanging. The upload route turns storage failures into a 500 with a generic message.

⚠️ Needs attention

🤖 Generated with Claude Code

chenrui333 and others added 11 commits October 25, 2026 12:13
`DELETE /management-api/cache-entries` accepted a `repoId` filter and
documented it, but never applied it, so a delete filtered only by
repository removed every cache entry of every repository.

Apply it like the other filters. The Rust rewrite in #288 already fixes
this; this is the same fix for the current TypeScript server.

Signed-off-by: Rui Chen <rui@chenrui.dev>
getDatabase checks a client out of the pool to fail fast on bad connection
settings and never released it. That client stays checked out for the life
of the process, leaving the pool one connection short, and Kysely's
destroy() (pool.end()) would wait for it forever.

Signed-off-by: Rui Chen <rui@chenrui.dev>
The first download of an unmerged entry tees its parts into the client
response and the background merge upload. A destroyed stream returns false
from write() and never emits 'drain' (nor 'error' when destroyed without
one), so the pump parks forever when:

- the merge upload fails: `download()` destroys the merger stream, and the
  client's download stalls mid-body until the client times out;
- the client hangs up while the pump waits on the merger: the next write to
  the destroyed response stream parks the pump, the merge never completes,
  and its lease keeps renewing.

Either way the current part's backend read stays open (one S3 socket per
occurrence), so enough of them fill the S3 socket pool.

Wait for 'drain' or 'close', skip destroyed sinks, keep feeding the one that
is still open, and stop reading once both are gone so `streamParts` destroys
the current part stream.

The regression tests fail on the previous pump with the filesystem and S3
drivers: the merge never settles after the client leaves, the client never
finishes after the merge upload fails, and the part stream is never
destroyed when both are gone.

Signed-off-by: Rui Chen <rui@chenrui.dev>
`streamParts()` awaited `globalThis.gc()` after every part, and the image
starts Node with `--expose-gc`, so every part of every unmerged restore and
streaming merge ran a synchronous full mark-compact on the event loop. With
~1 MiB parts (BuildKit) that caps all concurrent first restores combined at
roughly one part per GC pause, and the pause grows with the heap, so more
concurrent restores make every one of them slower.

Memory no longer depends on it: the upload route lets storage pace the body
(#275) and the download route pipes with backpressure (#282), so the bytes
in flight are bounded by stream high-water marks and become ordinary
garbage that V8 collects incrementally.

Remove both calls, the `--expose-gc` flag and the startup warning about it.

Signed-off-by: Rui Chen <rui@chenrui.dev>
`lib/storage.ts` imported `@azure/storage-blob`, `@azure/identity` and
`@google-cloud/storage` at the top level, so every server loaded all three
SDKs whatever `STORAGE_DRIVER` said. Since the Azure Blob driver (#280) that
adds about 40 MB of live heap to an idle S3 or filesystem server, and every
GC cycle has to walk it.

Import each SDK inside its adapter's `fromEnv`; only types stay as static
imports.

Signed-off-by: Rui Chen <rui@chenrui.dev>
…endpoints

The S3 client's agent pool was hard-coded at 50 sockets, and the agent was
passed only as `httpsAgent`. NodeHttpHandler picks the agent by endpoint
scheme, so an http:// endpoint (MinIO, RustFS, an in-cluster gateway)
silently fell back to the SDK's default agent and ignored the configured
pool. Local tests and benchmarks against RustFS therefore never exercised
the pool an https deployment uses.

Add STORAGE_S3_MAX_SOCKETS (default 50, the previous value) and build both
the http and the https agent from the same options.

tests/s3-socket-pool.test.ts holds one GetObject body unread with a pool of
one socket and checks that a second GetObject waits for it; against an
http:// endpoint it did not wait before this change.

Signed-off-by: Rui Chen <rui@chenrui.dev>
All S3 traffic shared one client and one agent pool. A streaming Merge (the
first restore of an unmerged entry, or a streaming Eager Merge) holds a part
GetObject open, paused by backpressure, while lib-storage waits in that same
pool for a socket to upload the merged object. Once concurrent Merges reach
maxSockets, every socket is a paused GET waiting for a socket that never
frees: the pool deadlocks, Merge Leases renew forever, no Merge or restore
completes, and every later S3 call queues behind them. That includes the
HeadObject each cache lookup issues, which also waits behind every bulk
transfer in the agent's FIFO queue even without a deadlock.

Use one S3Client per kind of work:

- reads: GetObject (and presigning) (STORAGE_S3_MAX_SOCKETS)
- writes: PutObject, multipart uploads, UploadPartCopy compose
  (STORAGE_S3_MAX_SOCKETS)
- control: HeadObject, HeadBucket, ListObjectsV2, DeleteObjects
  (STORAGE_S3_CONTROL_MAX_SOCKETS, default 16)

Write-socket holders never wait on reads, and control requests hold a socket
only briefly, so no wait cycle spans pools. The control client also sets
connectionTimeout (STORAGE_S3_CONTROL_QUEUE_TIMEOUT_MS, default 5000), which
bounds the wait for a socket plus the connect, so a starved lookup fails
instead of hanging.

tests/merge-socket-pool.test.ts runs as many concurrent first restores as
pool sockets (4) against RustFS; with the shared pool it never finishes.
tests/s3-socket-pool.test.ts checks that a HeadObject is answered while every
data socket is held by an unread GetObject body.

Signed-off-by: Rui Chen <rui@chenrui.dev>
… storage answers

A client can give up on /download while its storage read is still waiting,
for example a GetObject queued behind a saturated socket pool. The body then
arrives for a response that is already closed. `pipeline` rejects a closed
destination with ERR_STREAM_UNABLE_TO_PIPE before it takes ownership of the
source, and the route rethrew without destroying it, so the body pinned its
backend socket and the Storage Reader Lease for the life of the process.
Enough of them fill the pool and every storage call queues.

Check the response before piping and destroy the body if the client is
gone, and destroy it on every error exit of the pipe as well. #282 and #291
already cover a client leaving mid-body.

tests/download-client-gone.test.ts holds the storage read until the client
has hung up, then checks that the backend socket and the reader lease are
released; on Node 26.12.1 the socket stayed held before this change.

Signed-off-by: Rui Chen <rui@chenrui.dev>
The error hook, the debug request/response hooks and the Results
Passthrough logged `event.path` with its query string. Upload URLs are
used by Azure Blob-style clients that put SAS-style parameters in the
query, so those logs could record credentials. Log the path without its
query string.

Storage errors in the upload route are logged by the route and rethrown
as a handled error, because Nitro logs unhandled errors with the full
request URL.

The request-logging hooks move from `plugins/setup.ts` into their own
plugin so the test can drive them against the real upload route.

Signed-off-by: Rui Chen <rui@chenrui.dev>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Drop the duplicate pre-pipe check in the download route (the catch already
handles ERR_STREAM_UNABLE_TO_PIPE), import the Azure SDK where SAS signing
uses it, rename loggedPath to pathWithoutQuery, expose the new S3 pool
settings in the Helm chart, and let hangs fail as test timeouts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@LouisHaftmann
LouisHaftmann added this pull request to stack #301 October 25, 2026 11:52
@LouisHaftmann
LouisHaftmann merged commit 580e28a into dev Oct 25, 2026
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants