diff --git a/lib/replicator.js b/lib/replicator.js index 24bb15d9..5dfa4855 100644 --- a/lib/replicator.js +++ b/lib/replicator.js @@ -212,8 +212,25 @@ class BlockRequest extends Attachable { _unref() { if (this.resolved) return + + // Also leave the replication queue: nothing else removes the request + // while no peer is connected, so it would stay there until the core + // closes. Removal is amortized: the queue is compacted in a single + // pass once enough stale entries collect, so unreferencing many + // queued requests at once stays linear instead of quadratic, and a + // cancelled batch does not linger waiting for a later read. The current + // request is marked first so the pass can drop it too. + const queued = this.queued this.queued = false + if (queued === true) { + const r = this.replicator + if (++r._queuedStale > 16 && r._queuedStale * 2 > r._queued.length) { + r._queued = r._queued.filter((b) => b.queued === true) + r._queuedStale = 0 + } + } + for (const req of this.inflight) { req.peer._cancelRequest(req) } @@ -2044,6 +2061,7 @@ module.exports = class Replicator { this._alwaysLatestBlockEnabled = !!alwaysLatestBlock this._alwaysLatestBlock = this._alwaysLatestBlockEnabled ? 1 : 0 this._queued = [] + this._queuedStale = 0 this._seeks = [] this._upgrade = null diff --git a/test/replicate.js b/test/replicate.js index c850b875..0b583e1a 100644 --- a/test/replicate.js +++ b/test/replicate.js @@ -3521,4 +3521,30 @@ test('stream that closes while the core opens is not attached', async function ( await core.close() }) +test('timed-out block requests are removed from the replication queue', async function (t) { + const a = await create(t) + const b = await create(t, a.key) + + // b never connects to a peer, so its requests can only time out. Track the + // largest the queue gets: the bound must hold between compaction passes, + // not just at the end. + let maxQueued = 0 + for (let i = 0; i < 100; i++) { + await t.exception(b.get(i, { timeout: 20 }), /REQUEST_TIMEOUT/) + maxQueued = Math.max(maxQueued, b.core.replicator._queued.length) + } + + // A concurrent batch that all times out together must be cleaned up too, + // without any later read to trigger it + const batch = [] + for (let i = 0; i < 40; i++) batch.push(t.exception(b.get(i, { timeout: 20 }), /REQUEST_TIMEOUT/)) + await Promise.all(batch) + maxQueued = Math.max(maxQueued, b.core.replicator._queued.length) + + // Removal is amortized: the queue is compacted once more than 16 stale + // entries dominate it, so it never retains more than 16 timed-out + // requests (previously it retained all of them until the core closed). + t.ok(maxQueued <= 16, 'timed-out requests leave the queue') +}) + function noop() {}