Skip to content

Commit 2a27091

Browse files
committed
fix(api): address CodeRabbit review on bedrock createMessage abort lifecycle
The external abort bridge listener was only removed when the signal actually aborted; a completed request left the listener (and its closure over the request controller) attached to the caller's signal. Make the controller request-local and detach the listener in a finally block so the external signal keeps no reference after the request ends (success or error). Test: createMessage regression - first request completes normally, a second request starts with a different external signal; the first signal's listener is removed on completion and aborting it late does not cancel the second stream.
1 parent 913405a commit 2a27091

2 files changed

Lines changed: 88 additions & 7 deletions

File tree

‎src/api/providers/__tests__/bedrock.spec.ts‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2186,6 +2186,77 @@ describe("AwsBedrockHandler", () => {
21862186

21872187
await expect(consumed).rejects.toMatchObject({ name: "AbortError" })
21882188
})
2189+
2190+
it("createMessage should detach the external abort listener when the request completes", async () => {
2191+
const handler = new AwsBedrockHandler({
2192+
apiModelId: "anthropic.claude-3-5-sonnet-20241022-v2:0",
2193+
awsAccessKey: "test-access-key",
2194+
awsSecretKey: "test-secret-key",
2195+
awsRegion: "us-east-1",
2196+
})
2197+
2198+
const streamChunks = [
2199+
JSON.stringify({ contentBlockDelta: { delta: { text: "hello" } } }),
2200+
JSON.stringify({ messageStop: {} }),
2201+
]
2202+
2203+
let secondSendSignal: AbortSignal | undefined
2204+
const mockSend = vi
2205+
.fn()
2206+
.mockResolvedValueOnce({ stream: streamChunks })
2207+
.mockImplementation(async (_command: unknown, options?: { abortSignal?: AbortSignal }) => {
2208+
secondSendSignal = options?.abortSignal
2209+
return { stream: streamChunks }
2210+
})
2211+
handler["client"].send = mockSend
2212+
2213+
// First request completes normally with its own external signal
2214+
const firstController = new AbortController()
2215+
const firstRemoveSpy = vi.spyOn(firstController.signal, "removeEventListener")
2216+
const firstGenerator = handler.createMessage(
2217+
"You are a helpful assistant",
2218+
[{ role: "user", content: "Hello" }],
2219+
makeCreateMessageMetadata({ abortSignal: firstController.signal }),
2220+
)
2221+
const firstText = await (async () => {
2222+
let text = ""
2223+
for await (const chunk of firstGenerator) {
2224+
if (chunk.type === "text") {
2225+
text += chunk.text
2226+
}
2227+
}
2228+
return text
2229+
})()
2230+
expect(firstText).toBe("hello")
2231+
2232+
// The bridge listener must be detached as soon as the request completes
2233+
expect(firstRemoveSpy).toHaveBeenCalledWith("abort", expect.any(Function))
2234+
2235+
// Second request starts with a DIFFERENT external signal
2236+
const secondController = new AbortController()
2237+
const secondGenerator = handler.createMessage(
2238+
"You are a helpful assistant",
2239+
[{ role: "user", content: "Hello" }],
2240+
makeCreateMessageMetadata({ abortSignal: secondController.signal }),
2241+
)
2242+
const secondText = await (async () => {
2243+
let text = ""
2244+
for await (const chunk of secondGenerator) {
2245+
if (chunk.type === "text") {
2246+
text += chunk.text
2247+
}
2248+
}
2249+
return text
2250+
})()
2251+
expect(secondText).toBe("hello")
2252+
2253+
// Aborting the first (already completed) signal late must not cancel the second request
2254+
firstController.abort()
2255+
expect(secondSendSignal).toBeDefined()
2256+
expect(secondSendSignal?.aborted).toBe(false)
2257+
2258+
firstRemoveSpy.mockRestore()
2259+
})
21892260
})
21902261
})
21912262
})

‎src/api/providers/bedrock.ts‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -558,33 +558,37 @@ export class AwsBedrockHandler extends BaseProvider implements SingleCompletionH
558558
...(useServiceTier && { [SERVICE_TIER_KEY]: this.options.awsBedrockServiceTier }),
559559
}
560560

561-
// Create AbortController with 10 minute timeout
562-
const controller = new AbortController()
561+
// Create a request-local AbortController with 10 minute timeout. Keeping it
562+
// request-local (and detaching the bridge listener in the finally block) means
563+
// a completed request can never leave a stale listener on the caller's signal.
564+
const requestController = new AbortController()
563565
let timeoutId: NodeJS.Timeout | undefined
564566

565-
// Bridge external abort signal to our controller using the Bedrock pattern:
567+
// Bridge external abort signal to the request controller using the Bedrock pattern:
566568
// - pre-aborted guard: check if already aborted before adding listener
567569
// - { once: true }: remove listener after first abort to avoid leaks
570+
let abortListener: (() => void) | undefined
568571
const externalAbortSignal = metadata?.abortSignal
569572
if (externalAbortSignal) {
570573
if (externalAbortSignal.aborted) {
571-
controller.abort()
574+
requestController.abort()
572575
} else {
573-
externalAbortSignal.addEventListener("abort", () => controller.abort(), { once: true })
576+
abortListener = () => requestController.abort()
577+
externalAbortSignal.addEventListener("abort", abortListener, { once: true })
574578
}
575579
}
576580

577581
try {
578582
timeoutId = setTimeout(
579583
() => {
580-
controller.abort()
584+
requestController.abort()
581585
},
582586
10 * 60 * 1000,
583587
)
584588

585589
const command = new ConverseStreamCommand(payload)
586590
const response = await this.client.send(command, {
587-
abortSignal: controller.signal,
591+
abortSignal: requestController.signal,
588592
})
589593

590594
if (!response.stream) {
@@ -833,6 +837,12 @@ export class AwsBedrockHandler extends BaseProvider implements SingleCompletionH
833837
} else {
834838
throw new Error("An unknown error occurred")
835839
}
840+
} finally {
841+
// Detach the bridge listener once the request ends (success or error) so the
842+
// external signal keeps no reference to this request's controller.
843+
if (abortListener) {
844+
externalAbortSignal?.removeEventListener("abort", abortListener)
845+
}
836846
}
837847
}
838848

0 commit comments

Comments
 (0)