fix: clean up upstream reader abort listener (#1378)
Co-authored-by: Isaac Sanchez <isanchez-hawkins@arize.com>
This commit is contained in:
committed by
GitHub
co-authored by
Isaac Sanchez
parent
1d36995c47
commit
2b5408406c
@@ -63,21 +63,34 @@ export function createUpstreamSseReader({
|
|||||||
let stopped = false;
|
let stopped = false;
|
||||||
let activeController = null;
|
let activeController = null;
|
||||||
let lastEventId = typeof initialLastEventId === 'string' ? initialLastEventId : '';
|
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;
|
stopped = true;
|
||||||
|
detachStopListener();
|
||||||
if (activeController && !activeController.signal.aborted) {
|
if (activeController && !activeController.signal.aborted) {
|
||||||
activeController.abort();
|
activeController.abort();
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
signal?.addEventListener('abort', stop, { once: true });
|
|
||||||
|
|
||||||
const start = () => {
|
const start = () => {
|
||||||
if (running) {
|
if (running) {
|
||||||
return running;
|
return running;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
attachStopListener();
|
||||||
stopped = false;
|
stopped = false;
|
||||||
running = (async () => {
|
running = (async () => {
|
||||||
while (!stopped && !signal?.aborted) {
|
while (!stopped && !signal?.aborted) {
|
||||||
@@ -210,6 +223,7 @@ export function createUpstreamSseReader({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
})().finally(() => {
|
})().finally(() => {
|
||||||
|
detachStopListener();
|
||||||
running = null;
|
running = null;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -234,7 +234,7 @@ describe('createUpstreamSseReader', () => {
|
|||||||
expect(attempt).toBe(2);
|
expect(attempt).toBe(2);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('removes reconnect delay abort listeners after normal timeout completion', async () => {
|
it('removes abort listeners after stop', async () => {
|
||||||
const tracked = createTrackedSignal();
|
const tracked = createTrackedSignal();
|
||||||
let attempt = 0;
|
let attempt = 0;
|
||||||
let reader;
|
let reader;
|
||||||
@@ -270,7 +270,6 @@ describe('createUpstreamSseReader', () => {
|
|||||||
await reader.start();
|
await reader.start();
|
||||||
|
|
||||||
expect(attempt).toBe(2);
|
expect(attempt).toBe(2);
|
||||||
// The top-level stop listener remains; the reconnect-delay listener should be removed.
|
expect(tracked.getListenerCount()).toBe(0);
|
||||||
expect(tracked.getListenerCount()).toBe(1);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user