Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix([issue-1087]): close the firing EventSource instance, guard stale callbacks

Adam Eivyeb9d79f780e8
diff --git a/client/src/hooks/useInstallStream.js b/client/src/hooks/useInstallStream.js
index 1ab6a9892d..95f7267108 100644
--- a/client/src/hooks/useInstallStream.js
+++ b/client/src/hooks/useInstallStream.js
@@ -107,8 +107,13 @@ export function useInstallStream(url, {
setStreamStarted(true);
const es = new EventSource(url);
esRef.current = es;
+ // Close the instance that fired this event, not whatever esRef currently
+ // points at — so a late callback from a superseded stream can't tear down
+ // (or null out) a newer one after a url/enabled change.
+ const closeThis = () => { if (esRef.current === es) esRef.current = null; es.close(); };
es.onmessage = (ev) => {
+ if (esRef.current !== es) return; // stale callback from a superseded stream
const msg = safeParseJSON(ev.data);
if (!msg) return;
if (msg.type === 'stage') {
@@ -121,27 +126,28 @@ export function useInstallStream(url, {
doneRef.current = true;
appendLog({ kind: 'success', text: msg.message });
flush();
- closeStream();
+ closeThis();
onCompleteRef.current?.();
} else if (msg.type === 'error') {
setError(msg.message);
appendLog({ kind: 'error', text: msg.message });
flush();
- closeStream();
+ closeThis();
}
};
es.onerror = () => {
+ if (esRef.current !== es) return; // stale callback from a superseded stream
// Network drop or server killed the stream. If we already saw `complete`
// this is harmless; otherwise surface it so the user isn't stuck on a
// forever-spinning modal.
setError((prev) => prev ?? (doneRef.current ? null : CONNECTION_LOST_MESSAGE));
- closeStream();
+ closeThis();
};
return () => {
clearFlush();
- closeStream();
+ closeThis();
};
// onComplete intentionally excluded — it lives in onCompleteRef.
// eslint-disable-next-line react-hooks/exhaustive-deps
diff --git a/client/src/hooks/useInstallStream.test.jsx b/client/src/hooks/useInstallStream.test.jsx
index 4723e47fc5..451701cee6 100644
--- a/client/src/hooks/useInstallStream.test.jsx
+++ b/client/src/hooks/useInstallStream.test.jsx
@@ -160,6 +160,29 @@ describe('useInstallStream', () => {
expect(result.current.error).toBeNull();
});
+ it('ignores a stale callback from a superseded stream after url change', () => {
+ const { result, rerender } = renderHook(
+ ({ url }) => useInstallStream(url, { enabled: true }),
+ { initialProps: { url: '/a' } },
+ );
+ const first = MockEventSource.instances[0];
+ // url change re-runs the effect: cleanup closes `first`, a second stream opens.
+ rerender({ url: '/b' });
+ const second = MockEventSource.instances[1];
+ expect(first.closed).toBe(true);
+ expect(second.closed).toBe(false);
+
+ // A late frame on the now-superseded first stream must NOT mutate state
+ // nor close the live second stream.
+ act(() => { first.emit({ type: 'error', message: 'stale boom' }); });
+ expect(result.current.error).toBeNull();
+ expect(second.closed).toBe(false);
+
+ // The live stream still works.
+ act(() => { second.emit({ type: 'log', message: 'live' }); });
+ expect(result.current.logs.map((e) => e.text)).toEqual(['live']);
+ });
+
it('close() tears down the active stream', () => {
const { result } = renderHook(() => useInstallStream('/x', { enabled: true }));
expect(last().closed).toBe(false);