Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(llm-proxy): refuse a late poll past the queue wait bound

Alfredo Lopezb69a5139d0eb
diff --git a/codex-llm-proxy/src/requestLimits.ts b/codex-llm-proxy/src/requestLimits.ts
index 4b9cf2285..e7e046990 100644
--- a/codex-llm-proxy/src/requestLimits.ts
+++ b/codex-llm-proxy/src/requestLimits.ts
@@ -4,9 +4,10 @@ export const STREAM_LIMITS = {
maxConcurrentStreams: 8,
maxQueuedRequests: 16,
maxStreamDurationMs: 1_800_000,
- // Longest time a request may wait for a stream slot. Bounded so that queue
- // wait + redeem + the first keepalive stays below the Host HTTP client's
- // 300 s header timeout.
+ // Longest time a request may wait for a stream slot. A visual request can
+ // wait twice (visualStreamGate, then streamGate), so the bound is chosen so
+ // that two queue waits + redeem + one heartbeat interval stay below the Host
+ // HTTP client's 300 s header timeout.
maxQueueWaitMs: 60_000,
// Longest silence tolerated while waiting on the upstream (response headers
// or the next SSE chunk). Matches the Codex CLI stream idle timeout.
@@ -66,8 +67,18 @@ export class StreamGate {
private readonly maxQueued: number = STREAM_LIMITS.maxQueuedRequests,
private readonly maxQueueWaitMs: number = STREAM_LIMITS.maxQueueWaitMs
) {
- // setTimeout fires after 1 ms for any delay above 2^31 - 1, which would
- // refuse every queued waiter at once instead of after the configured wait.
+ // A NaN or fractional size would let every caller through or queue
+ // without bound, because the comparisons below would never hold.
+ if (!Number.isInteger(maxConcurrent) || maxConcurrent < 1) {
+ throw new RangeError('maxConcurrent must be a positive integer')
+ }
+ if (!Number.isInteger(maxQueued) || maxQueued < 0) {
+ throw new RangeError('maxQueued must be a non-negative integer')
+ }
+ // setTimeout turns NaN, 0, a negative delay or one above 2^31 - 1 into a
+ // 1 ms timer, which would refuse every queued waiter at once instead of
+ // after the configured wait. Fractions are refused so the bound stays a
+ // whole number of milliseconds.
if (!Number.isInteger(maxQueueWaitMs) || maxQueueWaitMs <= 0 || maxQueueWaitMs > MAX_TIMER_DELAY_MS) {
throw new RangeError(`maxQueueWaitMs must be an integer in 1..${MAX_TIMER_DELAY_MS}`)
}
@@ -77,8 +88,10 @@ export class StreamGate {
* Take a stream slot. When `signal` aborts (client disconnected) while the
* caller is still queued, the waiter is rejected and its queue slot freed, so
* a dropped client never proceeds to redeem an attempt. A waiter still
- * queued after `maxQueueWaitMs` is rejected the same way, on its own timer:
- * a slot that frees after the bound and before the next poll does not admit it.
+ * queued after `maxQueueWaitMs` is rejected the same way. A deadline timer
+ * settles it at the bound, and a poll that runs after the bound checks the
+ * elapsed wait before the slot count, so a slot that frees after the bound
+ * does not admit it even when the event loop stalled across the bound.
*/
async acquire(signal?: AbortSignal): Promise<() => void> {
if (signal?.aborted) throw new RequestLimitError('stream request was aborted')
@@ -97,11 +110,19 @@ export class StreamGate {
settle()
reject(new RequestLimitError('stream request was aborted'))
}
- const deadline = setTimeout(() => {
+ const expire = () => {
settle()
reject(new RequestLimitError('stream queue wait exceeded'))
- }, this.maxQueueWaitMs)
+ }
+ const queuedAt = performance.now()
+ const deadline = setTimeout(expire, this.maxQueueWaitMs)
const wait = () => {
+ // After the event loop stalls, a poll and the deadline can both be
+ // overdue, and Node runs the poll first because it was due first.
+ if (performance.now() - queuedAt >= this.maxQueueWaitMs) {
+ expire()
+ return
+ }
if (this.running < this.maxConcurrent) {
settle()
resolve()
diff --git a/codex-llm-proxy/test/requestLimits.test.ts b/codex-llm-proxy/test/requestLimits.test.ts
index b5f7ce363..797c5d939 100644
--- a/codex-llm-proxy/test/requestLimits.test.ts
+++ b/codex-llm-proxy/test/requestLimits.test.ts
@@ -1,3 +1,4 @@
+import { getEventListeners } from 'node:events'
import { describe, expect, it, vi } from 'vitest'
import { CONTROL_API_REQUEST_TIMEOUT_MS } from '../src/controlApiClient.js'
import {
@@ -50,6 +51,22 @@ describe('assertBoundedDeadline', () => {
})
})
+// Queues a waiter behind `gate` and records how it settled.
+function track(gate: StreamGate, signal?: AbortSignal) {
+ const state: { outcome?: string } = {}
+ const waiter = gate.acquire(signal).then(
+ next => {
+ state.outcome = 'admitted'
+ return next
+ },
+ (err: Error) => {
+ state.outcome = err.message
+ return undefined
+ }
+ )
+ return { state, waiter }
+}
+
describe('StreamGate', () => {
it('admits a queued waiter once a running stream releases', async () => {
const gate = new StreamGate(1, 1)
@@ -103,6 +120,31 @@ describe('StreamGate', () => {
releaseNext()
})
+ it('clears the deadline and the poll when a queued client aborts', async () => {
+ vi.useFakeTimers()
+ try {
+ const gate = new StreamGate(1, 1, 55)
+ const release = await gate.acquire()
+ const abort = new AbortController()
+ const { state, waiter } = track(gate, abort.signal)
+ await vi.advanceTimersByTimeAsync(20)
+ // Witness: the waiter is queued with its deadline, its poll and its
+ // abort listener live.
+ expect(state.outcome).toBeUndefined()
+ expect(vi.getTimerCount()).toBe(2)
+ expect(getEventListeners(abort.signal, 'abort')).toHaveLength(1)
+ abort.abort()
+ await vi.advanceTimersByTimeAsync(0)
+ expect(state.outcome).toBe('stream request was aborted')
+ expect(vi.getTimerCount()).toBe(0)
+ await waiter
+ expect(gate.snapshot()).toEqual({ running: 1, queued: 0 })
+ release()
+ } finally {
+ vi.useRealTimers()
+ }
+ })
+
it('rejects a waiter queued longer than maxQueueWaitMs and frees its queue slot', async () => {
const gate = new StreamGate(1, 1, 50)
const release = await gate.acquire()
@@ -120,70 +162,53 @@ describe('StreamGate', () => {
releaseNext()
})
- // The gate polls for a free slot every 10 ms. A bound that is not a multiple
- // of 10 falls between two polls, so these two cases separate a deadline the
- // gate enforces on its own timer from one it only notices on the next poll.
- it('rejects at maxQueueWaitMs even when a slot frees before the next poll', async () => {
+ it('refuses a waiter at the 60 000 ms default maxQueueWaitMs', async () => {
vi.useFakeTimers()
try {
- const gate = new StreamGate(1, 1, 55)
+ const gate = new StreamGate(1, 1)
const release = await gate.acquire()
- let outcome: string | undefined
- const waiter = gate.acquire().then(
- () => {
- outcome = 'admitted'
- },
- (err: Error) => {
- outcome = err.message
- }
- )
- await vi.advanceTimersByTimeAsync(54)
- // Witness: the waiter is still queued one millisecond before the bound.
- expect(outcome).toBeUndefined()
- expect(gate.snapshot()).toEqual({ running: 1, queued: 1 })
+ const { state, waiter } = track(gate)
+ await vi.advanceTimersByTimeAsync(59_999)
+ expect(state.outcome).toBeUndefined()
await vi.advanceTimersByTimeAsync(1)
- expect(outcome).toBe('stream queue wait exceeded')
- // The poll stopped with the rejection: no timer is left behind.
- expect(vi.getTimerCount()).toBe(0)
- // A slot that frees after the bound and before the next poll (60 ms)
- // must not reach the waiter that was already refused.
- await vi.advanceTimersByTimeAsync(2)
- release()
- await vi.advanceTimersByTimeAsync(10)
+ expect(state.outcome).toBe('stream queue wait exceeded')
await waiter
- expect(outcome).toBe('stream queue wait exceeded')
- expect(gate.snapshot()).toEqual({ running: 0, queued: 0 })
+ release()
} finally {
vi.useRealTimers()
}
})
- it('admits a waiter whose slot frees before maxQueueWaitMs', async () => {
+ // The gate polls for a free slot every 10 ms. A bound that is not a multiple
+ // of 10 falls between two polls, so the next two cases separate a deadline
+ // the gate enforces on its own timer from one it only notices on the next poll.
+ it('rejects at maxQueueWaitMs and leaves no timer or abort listener behind', async () => {
vi.useFakeTimers()
try {
const gate = new StreamGate(1, 1, 55)
const release = await gate.acquire()
- let outcome: string | undefined
- const waiter = gate.acquire().then(
- next => {
- outcome = 'admitted'
- return next
- },
- (err: Error) => {
- outcome = err.message
- return undefined
- }
- )
- await vi.advanceTimersByTimeAsync(45)
- release()
- await vi.advanceTimersByTimeAsync(5)
- expect(outcome).toBe('admitted')
- // The deadline timer was cleared on admission, before it could fire.
+ const abort = new AbortController()
+ const { state, waiter } = track(gate, abort.signal)
+ await vi.advanceTimersByTimeAsync(54)
+ // Witness: one millisecond before the bound the waiter is still queued,
+ // with its deadline, its poll and its abort listener live.
+ expect(state.outcome).toBeUndefined()
+ expect(gate.snapshot()).toEqual({ running: 1, queued: 1 })
+ expect(vi.getTimerCount()).toBe(2)
+ expect(getEventListeners(abort.signal, 'abort')).toHaveLength(1)
+ await vi.advanceTimersByTimeAsync(1)
+ expect(state.outcome).toBe('stream queue wait exceeded')
expect(vi.getTimerCount()).toBe(0)
- await vi.advanceTimersByTimeAsync(100)
- expect(outcome).toBe('admitted')
+ expect(getEventListeners(abort.signal, 'abort')).toHaveLength(0)
+ await waiter
+ // The queue slot was returned exactly once: one new waiter queues and
+ // the one after it finds the queue full.
expect(gate.snapshot()).toEqual({ running: 1, queued: 0 })
- ;(await waiter)?.()
+ const next = gate.acquire()
+ await expect(gate.acquire()).rejects.toThrow('stream queue is full')
+ release()
+ await vi.advanceTimersByTimeAsync(10)
+ ;(await next)()
} finally {
vi.useRealTimers()
}
@@ -194,24 +219,16 @@ describe('StreamGate', () => {
try {
const gate = new StreamGate(1, 1, 55)
const release = await gate.acquire()
- let outcome: string | undefined
- const waiter = gate.acquire().then(
- () => {
- outcome = 'admitted'
- },
- (err: Error) => {
- outcome = err.message
- }
- )
+ const { state, waiter } = track(gate)
// Last poll at 50 ms sees the slot taken; it frees at 52 ms.
await vi.advanceTimersByTimeAsync(52)
release()
// Witness: nothing has settled the waiter yet.
- expect(outcome).toBeUndefined()
+ expect(state.outcome).toBeUndefined()
await vi.advanceTimersByTimeAsync(3)
// The bound is a deadline: the waiter is settled at 55 ms, not at the
// 60 ms poll, and the deadline does not look at the slot count.
- expect(outcome).toBe('stream queue wait exceeded')
+ expect(state.outcome).toBe('stream queue wait exceeded')
await waiter
expect(gate.snapshot()).toEqual({ running: 0, queued: 0 })
} finally {
@@ -219,6 +236,69 @@ describe('StreamGate', () => {
}
})
+ it('refuses a waiter when the poll due at the bound finds a free slot', async () => {
+ vi.useFakeTimers()
+ try {
+ const gate = new StreamGate(1, 1, 50)
+ const release = await gate.acquire()
+ const { state, waiter } = track(gate)
+ await vi.advanceTimersByTimeAsync(45)
+ release()
+ expect(state.outcome).toBeUndefined()
+ // The deadline and the 50 ms poll fall due together; the poll must not
+ // admit a waiter whose wait has reached the bound.
+ await vi.advanceTimersByTimeAsync(5)
+ expect(state.outcome).toBe('stream queue wait exceeded')
+ await waiter
+ } finally {
+ vi.useRealTimers()
+ }
+ })
+
+ it('refuses a waiter whose poll runs after the bound because the event loop stalled', async () => {
+ const gate = new StreamGate(1, 1, 200)
+ const release = await gate.acquire()
+ const queuedAt = performance.now()
+ const { state, waiter } = track(gate)
+ await new Promise(resolve => setTimeout(resolve, 20))
+ // Witness: the poll has run and found the slot taken.
+ expect(state.outcome).toBeUndefined()
+ // Hold the event loop past the bound, then free the slot before any timer
+ // can run. The next poll and the deadline are now both overdue, and Node
+ // runs the poll first because it was due first.
+ while (performance.now() - queuedAt < 230) {
+ // busy wait
+ }
+ release()
+ ;(await waiter)?.()
+ expect(state.outcome).toBe('stream queue wait exceeded')
+ })
+
+ it('admits a waiter whose slot frees before maxQueueWaitMs', async () => {
+ vi.useFakeTimers()
+ try {
+ const gate = new StreamGate(1, 1, 55)
+ const release = await gate.acquire()
+ const abort = new AbortController()
+ const { state, waiter } = track(gate, abort.signal)
+ await vi.advanceTimersByTimeAsync(45)
+ // Witness: the abort listener is registered while the waiter is queued.
+ expect(getEventListeners(abort.signal, 'abort')).toHaveLength(1)
+ release()
+ await vi.advanceTimersByTimeAsync(5)
+ expect(state.outcome).toBe('admitted')
+ // The deadline timer and the abort listener were cleared on admission.
+ expect(vi.getTimerCount()).toBe(0)
+ expect(getEventListeners(abort.signal, 'abort')).toHaveLength(0)
+ await vi.advanceTimersByTimeAsync(100)
+ expect(state.outcome).toBe('admitted')
+ expect(gate.snapshot()).toEqual({ running: 1, queued: 0 })
+ ;(await waiter)?.()
+ } finally {
+ vi.useRealTimers()
+ }
+ })
+
it('refuses a maxQueueWaitMs that setTimeout cannot honor', () => {
// Witness: the bounds themselves are accepted.
expect(() => new StreamGate(1, 1, 1)).not.toThrow()
@@ -228,6 +308,17 @@ describe('StreamGate', () => {
}
})
+ it('refuses a slot or queue size the gate cannot enforce', () => {
+ // Witness: the smallest sizes are accepted, including a gate with no queue.
+ expect(() => new StreamGate(1, 0)).not.toThrow()
+ for (const maxConcurrent of [0, -1, 1.5, Number.NaN]) {
+ expect(() => new StreamGate(maxConcurrent, 1), `maxConcurrent=${maxConcurrent}`).toThrow(RangeError)
+ }
+ for (const maxQueued of [-1, 1.5, Number.NaN]) {
+ expect(() => new StreamGate(1, maxQueued), `maxQueued=${maxQueued}`).toThrow(RangeError)
+ }
+ })
+
it('pins the visual 2/8 sibling against the ordinary 8/16 stream gate', () => {
expect(VISUAL_STREAM_LIMITS).toEqual({ maxConcurrentStreams: 2, maxQueuedRequests: 8 })
expect(STREAM_LIMITS.maxConcurrentStreams).toBe(8)
diff --git a/docs/architecture/codex-subscription-transport-contract.md b/docs/architecture/codex-subscription-transport-contract.md
index d2c829060..ebcca9a6c 100644
--- a/docs/architecture/codex-subscription-transport-contract.md
+++ b/docs/architecture/codex-subscription-transport-contract.md
@@ -309,9 +309,10 @@ behavior changes:
before redeeming a ticket. It also rejects an invalid or out-of-bounds
deadline before the redeem, so the single-use ticket is not consumed.
- It rejects a stream-gate waiter still queued after `maxQueueWaitMs` with
- `provider_unavailable` (reason `stream queue wait exceeded`). Queue wait,
- the 15 s control-api redeem timeout and the first keepalive together stay
- below the Host HTTP client's 300 s header timeout.
+ `provider_unavailable` (reason `stream queue wait exceeded`). A visual
+ request can wait twice (the visual gate, then the ordinary gate), so two
+ queue waits, the 15 s control-api redeem timeout and one heartbeat interval
+ together stay below the Host HTTP client's 300 s header timeout.
- It requires the redeem response to carry `maxStreamDurationMs` greater
than 0. An absent value is a contract violation, not a default.
- It logs one `codex_proxy_attempt_finished` event per completion attempt,
diff --git a/grok-llm-proxy/src/requestLimits.ts b/grok-llm-proxy/src/requestLimits.ts
index 1353fa7d9..7cb86ea60 100644
--- a/grok-llm-proxy/src/requestLimits.ts
+++ b/grok-llm-proxy/src/requestLimits.ts
@@ -62,8 +62,18 @@ export class StreamGate {
private readonly maxQueued: number = STREAM_LIMITS.maxQueuedRequests,
private readonly maxQueueWaitMs: number = STREAM_LIMITS.maxQueueWaitMs
) {
- // setTimeout fires after 1 ms for any delay above 2^31 - 1, which would
- // refuse every queued waiter at once instead of after the configured wait.
+ // A NaN or fractional size would let every caller through or queue
+ // without bound, because the comparisons below would never hold.
+ if (!Number.isInteger(maxConcurrent) || maxConcurrent < 1) {
+ throw new RangeError('maxConcurrent must be a positive integer')
+ }
+ if (!Number.isInteger(maxQueued) || maxQueued < 0) {
+ throw new RangeError('maxQueued must be a non-negative integer')
+ }
+ // setTimeout turns NaN, 0, a negative delay or one above 2^31 - 1 into a
+ // 1 ms timer, which would refuse every queued waiter at once instead of
+ // after the configured wait. Fractions are refused so the bound stays a
+ // whole number of milliseconds.
if (!Number.isInteger(maxQueueWaitMs) || maxQueueWaitMs <= 0 || maxQueueWaitMs > MAX_TIMER_DELAY_MS) {
throw new RangeError(`maxQueueWaitMs must be an integer in 1..${MAX_TIMER_DELAY_MS}`)
}
@@ -73,8 +83,10 @@ export class StreamGate {
* Take a stream slot. When `signal` aborts (client disconnected) while the
* caller is still queued, the waiter is rejected and its queue slot freed, so
* a dropped client never proceeds to redeem an attempt. A waiter still
- * queued after `maxQueueWaitMs` is rejected the same way, on its own timer:
- * a slot that frees after the bound and before the next poll does not admit it.
+ * queued after `maxQueueWaitMs` is rejected the same way. A deadline timer
+ * settles it at the bound, and a poll that runs after the bound checks the
+ * elapsed wait before the slot count, so a slot that frees after the bound
+ * does not admit it even when the event loop stalled across the bound.
*/
async acquire(signal?: AbortSignal): Promise<() => void> {
if (signal?.aborted) throw new RequestLimitError('stream request was aborted')
@@ -93,11 +105,19 @@ export class StreamGate {
settle()
reject(new RequestLimitError('stream request was aborted'))
}
- const deadline = setTimeout(() => {
+ const expire = () => {
settle()
reject(new RequestLimitError('stream queue wait exceeded'))
- }, this.maxQueueWaitMs)
+ }
+ const queuedAt = performance.now()
+ const deadline = setTimeout(expire, this.maxQueueWaitMs)
const wait = () => {
+ // After the event loop stalls, a poll and the deadline can both be
+ // overdue, and Node runs the poll first because it was due first.
+ if (performance.now() - queuedAt >= this.maxQueueWaitMs) {
+ expire()
+ return
+ }
if (this.running < this.maxConcurrent) {
settle()
resolve()
diff --git a/grok-llm-proxy/test/requestLimits.test.ts b/grok-llm-proxy/test/requestLimits.test.ts
index 52b8ce4e3..15e8e00ac 100644
--- a/grok-llm-proxy/test/requestLimits.test.ts
+++ b/grok-llm-proxy/test/requestLimits.test.ts
@@ -1,3 +1,4 @@
+import { getEventListeners } from 'node:events'
import { describe, expect, it, vi } from 'vitest'
import { CONTROL_API_REQUEST_TIMEOUT_MS } from '../src/controlApiClient.js'
import {
@@ -45,6 +46,22 @@ describe('assertBoundedDeadline', () => {
})
})
+// Queues a waiter behind `gate` and records how it settled.
+function track(gate: StreamGate, signal?: AbortSignal) {
+ const state: { outcome?: string } = {}
+ const waiter = gate.acquire(signal).then(
+ next => {
+ state.outcome = 'admitted'
+ return next
+ },
+ (err: Error) => {
+ state.outcome = err.message
+ return undefined
+ }
+ )
+ return { state, waiter }
+}
+
describe('StreamGate', () => {
it('admits a queued waiter once a running stream releases', async () => {
const gate = new StreamGate(1, 1)
@@ -98,6 +115,30 @@ describe('StreamGate', () => {
releaseNext()
})
+ it('clears the deadline and the poll when a queued client aborts', async () => {
+ vi.useFakeTimers()
+ try {
+ const gate = new StreamGate(1, 1, 55)
+ const release = await gate.acquire()
+ const abort = new AbortController()
+ const { state, waiter } = track(gate, abort.signal)
+ await vi.advanceTimersByTimeAsync(20)
+ // Witness: the waiter is queued with its deadline, its poll and its
+ // abort listener live.
+ expect(state.outcome).toBeUndefined()
+ expect(vi.getTimerCount()).toBe(2)
+ expect(getEventListeners(abort.signal, 'abort')).toHaveLength(1)
+ abort.abort()
+ await vi.advanceTimersByTimeAsync(0)
+ expect(state.outcome).toBe('stream request was aborted')
+ expect(vi.getTimerCount()).toBe(0)
+ await waiter
+ release()
+ } finally {
+ vi.useRealTimers()
+ }
+ })
+
it('rejects a waiter queued longer than maxQueueWaitMs and frees its queue slot', async () => {
const gate = new StreamGate(1, 1, 50)
const release = await gate.acquire()
@@ -115,37 +156,51 @@ describe('StreamGate', () => {
releaseNext()
})
+ it('refuses a waiter at the 60 000 ms default maxQueueWaitMs', async () => {
+ vi.useFakeTimers()
+ try {
+ const gate = new StreamGate(1, 1)
+ const release = await gate.acquire()
+ const { state, waiter } = track(gate)
+ await vi.advanceTimersByTimeAsync(59_999)
+ expect(state.outcome).toBeUndefined()
+ await vi.advanceTimersByTimeAsync(1)
+ expect(state.outcome).toBe('stream queue wait exceeded')
+ await waiter
+ release()
+ } finally {
+ vi.useRealTimers()
+ }
+ })
+
// The gate polls for a free slot every 10 ms. A bound that is not a multiple
- // of 10 falls between two polls, so these two cases separate a deadline the
- // gate enforces on its own timer from one it only notices on the next poll.
- it('rejects at maxQueueWaitMs even when a slot frees before the next poll', async () => {
+ // of 10 falls between two polls, so the next two cases separate a deadline
+ // the gate enforces on its own timer from one it only notices on the next poll.
+ it('rejects at maxQueueWaitMs and leaves no timer or abort listener behind', async () => {
vi.useFakeTimers()
try {
const gate = new StreamGate(1, 1, 55)
const release = await gate.acquire()
- let outcome: string | undefined
- const waiter = gate.acquire().then(
- () => {
- outcome = 'admitted'
- },
- (err: Error) => {
- outcome = err.message
- }
- )
+ const abort = new AbortController()
+ const { state, waiter } = track(gate, abort.signal)
await vi.advanceTimersByTimeAsync(54)
- // Witness: the waiter is still queued one millisecond before the bound.
- expect(outcome).toBeUndefined()
+ // Witness: one millisecond before the bound the waiter is still queued,
+ // with its deadline, its poll and its abort listener live.
+ expect(state.outcome).toBeUndefined()
+ expect(vi.getTimerCount()).toBe(2)
+ expect(getEventListeners(abort.signal, 'abort')).toHaveLength(1)
await vi.advanceTimersByTimeAsync(1)
- expect(outcome).toBe('stream queue wait exceeded')
- // The poll stopped with the rejection: no timer is left behind.
+ expect(state.outcome).toBe('stream queue wait exceeded')
expect(vi.getTimerCount()).toBe(0)
- // A slot that frees after the bound and before the next poll (60 ms)
- // must not reach the waiter that was already refused.
- await vi.advanceTimersByTimeAsync(2)
+ expect(getEventListeners(abort.signal, 'abort')).toHaveLength(0)
+ await waiter
+ // The queue slot was returned exactly once: one new waiter queues and
+ // the one after it finds the queue full.
+ const next = gate.acquire()
+ await expect(gate.acquire()).rejects.toThrow('stream queue is full')
release()
await vi.advanceTimersByTimeAsync(10)
- await waiter
- expect(outcome).toBe('stream queue wait exceeded')
+ ;(await next)()
} finally {
vi.useRealTimers()
}
@@ -156,37 +211,58 @@ describe('StreamGate', () => {
try {
const gate = new StreamGate(1, 1, 55)
const release = await gate.acquire()
- let outcome: string | undefined
- const waiter = gate.acquire().then(
- () => {
- outcome = 'admitted'
- },
- (err: Error) => {
- outcome = err.message
- }
- )
+ const { state, waiter } = track(gate)
// Last poll at 50 ms sees the slot taken; it frees at 52 ms.
await vi.advanceTimersByTimeAsync(52)
release()
// Witness: nothing has settled the waiter yet.
- expect(outcome).toBeUndefined()
+ expect(state.outcome).toBeUndefined()
await vi.advanceTimersByTimeAsync(3)
// The bound is a deadline: the waiter is settled at 55 ms, not at the
// 60 ms poll, and the deadline does not look at the slot count.
- expect(outcome).toBe('stream queue wait exceeded')
+ expect(state.outcome).toBe('stream queue wait exceeded')
await waiter
} finally {
vi.useRealTimers()
}
})
- it('refuses a maxQueueWaitMs that setTimeout cannot honor', () => {
- // Witness: the bounds themselves are accepted.
- expect(() => new StreamGate(1, 1, 1)).not.toThrow()
- expect(() => new StreamGate(1, 1, 2_147_483_647)).not.toThrow()
- for (const maxQueueWaitMs of [0, -1, 1.5, Number.NaN, 2_147_483_648]) {
- expect(() => new StreamGate(1, 1, maxQueueWaitMs), String(maxQueueWaitMs)).toThrow(RangeError)
+ it('refuses a waiter when the poll due at the bound finds a free slot', async () => {
+ vi.useFakeTimers()
+ try {
+ const gate = new StreamGate(1, 1, 50)
+ const release = await gate.acquire()
+ const { state, waiter } = track(gate)
+ await vi.advanceTimersByTimeAsync(45)
+ release()
+ expect(state.outcome).toBeUndefined()
+ // The deadline and the 50 ms poll fall due together; the poll must not
+ // admit a waiter whose wait has reached the bound.
+ await vi.advanceTimersByTimeAsync(5)
+ expect(state.outcome).toBe('stream queue wait exceeded')
+ await waiter
+ } finally {
+ vi.useRealTimers()
+ }
+ })
+
+ it('refuses a waiter whose poll runs after the bound because the event loop stalled', async () => {
+ const gate = new StreamGate(1, 1, 200)
+ const release = await gate.acquire()
+ const queuedAt = performance.now()
+ const { state, waiter } = track(gate)
+ await new Promise(resolve => setTimeout(resolve, 20))
+ // Witness: the poll has run and found the slot taken.
+ expect(state.outcome).toBeUndefined()
+ // Hold the event loop past the bound, then free the slot before any timer
+ // can run. The next poll and the deadline are now both overdue, and Node
+ // runs the poll first because it was due first.
+ while (performance.now() - queuedAt < 230) {
+ // busy wait
}
+ release()
+ ;(await waiter)?.()
+ expect(state.outcome).toBe('stream queue wait exceeded')
})
it('admits a waiter whose slot frees before maxQueueWaitMs', async () => {
@@ -194,28 +270,42 @@ describe('StreamGate', () => {
try {
const gate = new StreamGate(1, 1, 55)
const release = await gate.acquire()
- let outcome: string | undefined
- const waiter = gate.acquire().then(
- next => {
- outcome = 'admitted'
- return next
- },
- (err: Error) => {
- outcome = err.message
- return undefined
- }
- )
+ const abort = new AbortController()
+ const { state, waiter } = track(gate, abort.signal)
await vi.advanceTimersByTimeAsync(45)
+ // Witness: the abort listener is registered while the waiter is queued.
+ expect(getEventListeners(abort.signal, 'abort')).toHaveLength(1)
release()
await vi.advanceTimersByTimeAsync(5)
- expect(outcome).toBe('admitted')
- // The deadline timer was cleared on admission, before it could fire.
+ expect(state.outcome).toBe('admitted')
+ // The deadline timer and the abort listener were cleared on admission.
expect(vi.getTimerCount()).toBe(0)
+ expect(getEventListeners(abort.signal, 'abort')).toHaveLength(0)
await vi.advanceTimersByTimeAsync(100)
- expect(outcome).toBe('admitted')
+ expect(state.outcome).toBe('admitted')
;(await waiter)?.()
} finally {
vi.useRealTimers()
}
})
+
+ it('refuses a maxQueueWaitMs that setTimeout cannot honor', () => {
+ // Witness: the bounds themselves are accepted.
+ expect(() => new StreamGate(1, 1, 1)).not.toThrow()
+ expect(() => new StreamGate(1, 1, 2_147_483_647)).not.toThrow()
+ for (const maxQueueWaitMs of [0, -1, 1.5, Number.NaN, 2_147_483_648]) {
+ expect(() => new StreamGate(1, 1, maxQueueWaitMs), String(maxQueueWaitMs)).toThrow(RangeError)
+ }
+ })
+
+ it('refuses a slot or queue size the gate cannot enforce', () => {
+ // Witness: the smallest sizes are accepted, including a gate with no queue.
+ expect(() => new StreamGate(1, 0)).not.toThrow()
+ for (const maxConcurrent of [0, -1, 1.5, Number.NaN]) {
+ expect(() => new StreamGate(maxConcurrent, 1), `maxConcurrent=${maxConcurrent}`).toThrow(RangeError)
+ }
+ for (const maxQueued of [-1, 1.5, Number.NaN]) {
+ expect(() => new StreamGate(1, maxQueued), `maxQueued=${maxQueued}`).toThrow(RangeError)
+ }
+ })
})