Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/app/v1/_lib/proxy/client-abort-listener.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* 绑定客户端中止监听器,返回幂等清理函数。
*
* 注意:如果传入的 signal 已经 aborted,会在当前调用栈同步执行 onAbort。
* 调用方必须先初始化 onAbort 闭包会访问的控制器、任务 ID 等资源。
*/
export function bindClientAbortListener(
signal: AbortSignal | null | undefined,
onAbort: () => void
Expand Down
26 changes: 24 additions & 2 deletions tests/unit/proxy/response-handler-abort-listener-cleanup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ function makeProvider(overrides: Partial<Provider> = {}): Provider {
return {
id: 99,
name: "test-provider",
providerType: "openai",
providerType: "openai-compatible",
baseUrl: "https://api.test.invalid",
priority: 1,
weight: 1,
Expand Down Expand Up @@ -166,7 +166,7 @@ function makeSession(clientAbortSignal: AbortSignal | null, stream: boolean): Pr
sessionId: null,
requestSequence: 1,
originalFormat: "openai",
providerType: "openai",
providerType: "openai-compatible",
originalModelName: "gpt-5.4",
originalUrlPathname: "/v1/chat/completions",
providerChain: [],
Expand Down Expand Up @@ -280,4 +280,26 @@ describe("ProxyResponseHandler client abort listener cleanup", () => {
expect(removeSpy.mock.calls.filter(([type]) => type === "abort")).toHaveLength(0);
expect(testState.cancelTask).toHaveBeenCalled();
});

it("invokes stream cancel once when client signal is already aborted", async () => {
const controller = new AbortController();
controller.abort();
const addSpy = vi.spyOn(controller.signal, "addEventListener");
const removeSpy = vi.spyOn(controller.signal, "removeEventListener");
const session = makeSession(controller.signal, true);
const upstreamResponse = new Response(
'data: {"choices":[{"delta":{"content":"ok"}}]}\n\ndata: [DONE]\n\n',
{
headers: { "content-type": "text/event-stream" },
}
);

const response = await ProxyResponseHandler.dispatch(session, upstreamResponse);
await response.text();
await drainAsyncTasks();

expect(addSpy.mock.calls.filter(([type]) => type === "abort")).toHaveLength(0);
expect(removeSpy.mock.calls.filter(([type]) => type === "abort")).toHaveLength(0);
expect(testState.cancelTask).toHaveBeenCalledTimes(1);
});
});
Loading