#800·flue

`observe()` cancels and reopens the stream when a subscriber throws

Author: rikbrownCreated Sep 30, 2026Updated Sep 30, 2026

Summary

In @flue/sdk 2.1.1, publish in createAgentConversationObservation calls each subscriber with no try/catch. The follow loop publishes once per applied chunk, so an error from any subscriber leaves the for await loop through its catch. The observation then reconnects for an error that did not come from the transport:

  1. The loop exit calls the stream iterator's return(), which cancels the live view=updates request.
  2. scheduleRetry publishes phase: 'connecting' with the subscriber's error as snapshot.error.
  3. After the backoff delay, hydrate reads view=history, and follow opens a new stream: a catch-up view=updates read, then a new live request.

The subscribers after the one that threw do not receive that publish.

The same code is on main (2.2.2):

Observed behaviour

A React app renders a conversation through @flue/react. AgentSession subscribes to the observation, and its publish calls the useSyncExternalStore listeners synchronously. During a streamed reply, an app component wrote state in an effect on each snapshot, and React threw #185 ("Maximum update depth exceeded") from inside AgentSession.publish, so the error passed through the SDK's publish.

The error came back on later chunks, and the tab repeated the cycle above about every 2.3 s for the whole reply. One 95 s reply made 22 live view=updates requests, 21 view=history reads, 21 catch-up view=updates reads and 21 client aborts. Each view=history read returns the whole conversation.

The component bug is fixed in the app. The SDK turned a UI error into a transport reconnect, and snapshot.error reported it as a connection error.

Minimal repro

typescript
import { createFlueClient } from '@flue/sdk';

const client = createFlueClient({ url: 'https://example.test/agents/my-agent/c1' });
const observation = client.observe();

let calls = 0;
observation.subscribe(() => {
	if (++calls === 5) throw new Error('subscriber bug');
});
observation.subscribe(() => {
	const { phase, error } = observation.getSnapshot();
	console.log(phase, error?.message);
});

Use an existing conversation, then send it a message so that the reply streams at least two chunks. The fifth publish is the second chunk.

Expected: the stream stays open, the second subscriber receives every publish, and the error is reported once.

Actual: the second subscriber misses the fifth publish and logs connecting subscriber bug. The network log shows the live view=updates request cancelled, then a view=history read and a new view=updates stream.

Proposed fix

Isolate each subscriber in publish. Report the error through reportError, which a browser reports as an uncaught exception, or through console.error in a runtime that has no reportError:

diff
 	const publish = (next: AgentConversationObservationSnapshot) => {
 		snapshot = next;
-		for (const listener of listeners) listener();
+		for (const listener of listeners) {
+			try {
+				listener();
+			} catch (cause) {
+				const error = new Error(
+					`A subscriber of the observation of conversation ${next.conversation?.conversationId ?? '(not loaded)'} threw during a "${next.phase}" publish. The observation continues.`,
+					{ cause },
+				);
+				if (typeof globalThis.reportError === 'function') globalThis.reportError(error);
+				else console.error(error);
+			}
+		}
 	};

With this change, a subscriber error does not cancel the stream, does not change phase or error, and does not stop the later subscribers. Error tracking still receives the error, with the subscriber's error as cause.

A local patch applies the equivalent change to the 2.1.1 dist/index.mjs. A test streams chunks over a fake SSE server to a subscriber that throws. With the change, it sees one history read, one catch-up read and one live request, and a reportError call. Without the change, it sees two of each. I can share the test.

AgentSession.publish in @flue/react has the same unguarded loop, so a throw from one React listener also skips the listeners after it.

Version

@flue/sdk 2.1.1. The code is unchanged on main at 54cabb7 (2.2.2).