fix: clean up upstream reader abort listener (#1378)

Co-authored-by: Isaac Sanchez <isanchez-hawkins@arize.com>
This commit is contained in:
Isaac Sanchez-Hawkins
2026-05-23 20:48:16 +03:00
committed by GitHub
co-authored by Isaac Sanchez
parent 1d36995c47
commit 2b5408406c
2 changed files with 20 additions and 7 deletions
@@ -63,21 +63,34 @@ export function createUpstreamSseReader({
let stopped = false;
let activeController = null;
let lastEventId = typeof initialLastEventId === 'string' ? initialLastEventId : '';
let stopListenerAttached = false;
const stop = () => {
function detachStopListener() {
if (!stopListenerAttached) return;
signal?.removeEventListener('abort', stop);
stopListenerAttached = false;
}
function attachStopListener() {
if (!signal || signal.aborted || stopListenerAttached) return;
signal.addEventListener('abort', stop, { once: true });
stopListenerAttached = true;
}
function stop() {
stopped = true;
detachStopListener();
if (activeController && !activeController.signal.aborted) {
activeController.abort();
}
};
signal?.addEventListener('abort', stop, { once: true });
}
const start = () => {
if (running) {
return running;
}
attachStopListener();
stopped = false;
running = (async () => {
while (!stopped && !signal?.aborted) {
@@ -210,6 +223,7 @@ export function createUpstreamSseReader({
}
}
})().finally(() => {
detachStopListener();
running = null;
});