Skip to content

fix: cap concurrent Merges and close downloads whose client stopped reading - #300

Merged
LouisHaftmann merged 5 commits into
devfrom
fix/merge-cap-download-idle-timeout
Oct 25, 2026
Merged

LouisHaftmann merged 5 commits into
devfrom
fix/merge-cap-download-idle-timeout

Conversation

@LouisHaftmann

@LouisHaftmann LouisHaftmann commented Oct 25, 2026 •

Copy link
Copy Markdown
Collaborator

📝 Summary

Stacked on #296. Two more resource leaks from #293:

  • No cap on concurrent Merges (S3 pool deadlock, unbounded merges and stalled downloads: fixes from a patched 9.8.0 #293.3). Every first restore of an unmerged entry, and every finalize with EAGER_MERGE, starts a Merge that holds sockets, a Merge Lease and buffers for its whole duration. A burst of first restores exhausts them. Now MAX_CONCURRENT_MERGES (default 16) caps Merges per process; past it a restore streams the parts, exactly as when another worker holds the Merge Lease, and a later download merges.
  • Downloads whose client stopped reading are never closed (S3 pool deadlock, unbounded merges and stalled downloads: fixes from a patched 9.8.0 #293.4). A client that keeps the connection open but stops reading parks the pipe forever: it holds the storage socket, the Storage Reader Lease and, on a first restore, the Merge it paces. Now DOWNLOAD_IDLE_TIMEOUT_MS (default 5 min, 0 disables) closes it.
 startMerge(location)
+  if activeMerges >= MAX_CONCURRENT_MERGES
+    return false            # caller streams parts, a later download merges
   acquire Merge Lease
   ...

 GET /download/:id
+  idle timer, refreshed on every chunk read from storage
+    fires -> destroy storage stream -> pipeline ends, lease released
   pipeline(storage stream, response)

Both settings are in the Helm chart (config.maxConcurrentMerges, config.downloadIdleTimeoutMs). ADR-0009 now records that Eager Merges share the cap and fall back to a lazy Merge past it.

✅ Verification

Each new test fails on the #296 branch without the fix:

  • Before:
    • merge-concurrency: a first restore beyond the cap still merges, so mergedAt is set
    • proxy-backpressure idle timeout: a stalled client never gets its storage read released (20 s timeout)
    • proxy-backpressure slow download: with the timer not refreshed on each chunk, a download that keeps moving is cut off
  • After: all pass. Full suite:
DB + storage Result
sqlite + filesystem 74 passed, 8 skipped
sqlite + s3 79 passed, 3 skipped
postgres + filesystem 74 passed, 8 skipped
mysql + gcs 74 passed, 8 skipped

The matrix ran before the last commit, which only renames the error class, adds the slow-download test and edits docs; proxy-backpressure was re-run after it. type-check, eslint, prettier --check and helm template pass.

🔍 Reviews

Review Found Fixed
Bug review 2 2
Over-engineering review 10 7
Standards review 4 2
Spec review 6 1

Not fixed:

  • res.setTimeout instead of a timer: the socket timeout would carry over to later keep-alive requests on the same socket; the timer is scoped to this response.
  • Shared test helpers between merge-concurrency and merge-socket-pool: the signatures differ, small gain.
  • Inlining the test download helper: it would pass undefined where a stream is expected.
  • Slot released in two places in startMerge: one for a Merge that never started, one when it ends.
  • No dedicated first-restore idle test or eager-cap test: the merge pump tests from fix: unstick S3 restores under load and stop repo-scoped deletes wiping every entry #296 cover a destroyed response during a first restore, and both Merge paths go through the same startMerge guard.
  • write() throwing synchronously would leak a slot: same on dev for the lease renewal timer, and every writer is async.

🔗 Issues

Refs #293 (items 3 and 4). Item 6 is now #297; #293 can close once this and #296 merge.

💥 Merge danger

Door: two-way

No migrations; both settings can be raised or disabled (DOWNLOAD_IDLE_TIMEOUT_MS=0) without a redeploy of code.

Blast Radius: downloads

  • The idle timer measures how fast storage is read, not the client. If storage itself stalls for 5 minutes (e.g. a saturated read pool), a healthy client is cut off too.
  • A small entry that fits entirely in socket buffers is already fully read from storage, so a stalled client there is not closed; its lease and Merge are already released.
  • With EAGER_MERGE=true, finalizes past the cap leave the entry to merge on first download.

⚠️ Needs attention

🤖 Generated with Claude Code

@LouisHaftmann
LouisHaftmann added this pull request to stack #301 October 25, 2026 11:52
Base automatically changed from fix/contributor-stability-fixes to dev October 25, 2026 11:53
chenrui333 and others added 5 commits October 25, 2026 12:53
Every first restore of an unmerged entry and, with EAGER_MERGE, every
finalize started a Merge with no global limit. Each Merge holds storage
connections, a Merge Lease and stream buffers for its whole duration, and a
lazy Merge is paced by the restoring client's read speed. A burst of first
restores or of finalizes therefore grew Merges, sockets and memory without
bound.

Start a Merge only while fewer than MAX_CONCURRENT_MERGES (default 16) run
in this process. Beyond that, startMerge returns false: a restore streams the
parts directly, exactly as when another worker holds the Merge Lease, an
Eager Merge is left to the first download, and a later download merges the
entry. The slot is reserved before the first await, so concurrent callers
cannot overshoot it.

tests/merge-concurrency.test.ts covers a first restore and an Eager Merge
beyond the limit.

Signed-off-by: Rui Chen <rui@chenrui.dev>
Node's HTTP server has no idle timeout for a response in progress and the
download route set none. A client that stays connected but stops reading
parks the pipe forever: the backend read keeps its storage socket, the
Storage Reader Lease keeps renewing, and on the first restore of an
unmerged entry the Merge it paces never finishes and keeps its Merge Lease.
Each such client permanently removes one socket from the storage pool.

Destroy the download when no bytes have moved for DOWNLOAD_IDLE_TIMEOUT_MS
(default 300000, 0 disables). The timer is refreshed on every chunk, so slow
but progressing clients are unaffected. It covers whole and Range responses
and the first restore of an unmerged entry, whose Merge keeps going after the
client's side of the tee is closed.

tests/proxy-backpressure.test.ts parks a client after the first chunk of a
200, a 206 and an unmerged first restore, and checks that the storage read is
released, the response closed, and the Merge completed.

Signed-off-by: Rui Chen <rui@chenrui.dev>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Release the Merge slot in one finally, cap DOWNLOAD_IDLE_TIMEOUT_MS at
setTimeout's maximum, describe the timeout by what it measures, and drop
tests that repeat coverage of the merge pump.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Name the idle timeout error like the other route errors and cover a slow
download staying open.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@LouisHaftmann
LouisHaftmann force-pushed the fix/merge-cap-download-idle-timeout branch from eb59f6a to f328e3a Compare October 25, 2026 11:53
@LouisHaftmann
LouisHaftmann merged commit 0377fce into dev Oct 25, 2026
21 of 28 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

Development

Successfully merging this pull request may close these issues.

2 participants