Skip to content

Commit d1f58c4

Browse files
steipeteosolmaz
andauthored
fix(ollama): stop response handling after abort (#125961)
Recheck the request signal after an async response hook settles so a concurrent abort cannot reach response body handling. Related to #125802 and #125834. Co-authored-by: Onur Solmaz <[email protected]>
1 parent a99253f commit d1f58c4

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

‎extensions/ollama/src/stream-runtime.test.ts‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,6 +1762,58 @@ describe("createOllamaStreamFn streaming events", () => {
17621762
expect(release).toHaveBeenCalledTimes(1);
17631763
});
17641764

1765+
it("does not resume response handling after the hook resolves concurrently with abort", async () => {
1766+
let markHookStarted: () => void = () => undefined;
1767+
const hookStarted = new Promise<void>((resolve) => {
1768+
markHookStarted = resolve;
1769+
});
1770+
let settleHook: () => void = () => undefined;
1771+
const hookPending = new Promise<void>((resolve) => {
1772+
settleHook = resolve;
1773+
});
1774+
const getReader = vi.fn(() => ({
1775+
read: vi.fn(async () => ({ done: true as const, value: undefined })),
1776+
cancel: vi.fn(async () => undefined),
1777+
releaseLock: vi.fn(),
1778+
}));
1779+
const cancel = vi.fn(async () => undefined);
1780+
const release = vi.fn(async () => undefined);
1781+
fetchWithSsrFGuardMock.mockResolvedValue({
1782+
response: {
1783+
status: 200,
1784+
ok: true,
1785+
headers: new Headers({ "Content-Type": "application/x-ndjson" }),
1786+
body: { getReader, cancel },
1787+
} as unknown as Response,
1788+
release,
1789+
});
1790+
const abortController = new AbortController();
1791+
1792+
const eventsPromise = collectStreamEvents(
1793+
await createOllamaTestStream({
1794+
baseUrl: "http://ollama-host:11434",
1795+
options: {
1796+
onResponse: () => {
1797+
markHookStarted();
1798+
return hookPending;
1799+
},
1800+
signal: abortController.signal,
1801+
},
1802+
}),
1803+
);
1804+
await hookStarted;
1805+
await Promise.resolve();
1806+
void hookPending.then(() => abortController.abort());
1807+
settleHook();
1808+
const events = await eventsPromise;
1809+
1810+
expect(events).toHaveLength(1);
1811+
expect(events[0]).toMatchObject({ type: "error", reason: "aborted" });
1812+
expect(getReader).not.toHaveBeenCalled();
1813+
expect(cancel).toHaveBeenCalledTimes(1);
1814+
expect(release).toHaveBeenCalledTimes(1);
1815+
});
1816+
17651817
it("emits start, text_start, text_delta, text_end, done for text responses", async () => {
17661818
const events = await collectMockedOllamaEvents([
17671819
'{"model":"m","created_at":"t","message":{"role":"assistant","content":"Hello"},"done":false}',

‎extensions/ollama/src/stream.runtime.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ async function runOllamaResponseHook(params: {
102102
signal.removeEventListener("abort", onAbort);
103103
}
104104
}
105+
throwIfOllamaStreamAborted(signal);
105106
}
106107

107108
function createOllamaStreamCooperativeScheduler(

0 commit comments

Comments
 (0)