Skip to content
18 changes: 18 additions & 0 deletions lib/replicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
}
}

for (const req of this.inflight) {
req.peer._cancelRequest(req)
}
Expand Down Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions test/replicate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}