Skip to content

fix: prevent enqueuing into closed ReadableStream #1781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/smart-coins-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@trigger.dev/core": patch
---

fix: Realtime streams: prevent enqueuing into closed ReadableStream
85 changes: 53 additions & 32 deletions packages/core/src/v3/apiClient/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,10 @@ class ReadableShapeStream<T extends Row<unknown> = Row> {
readonly #changeStream: AsyncIterableStream<T>;
#error: FetchError | false = false;
#unsubscribe?: () => void;
#isStreamClosed: boolean = false;

stop() {
this.#isStreamClosed = true;
this.#unsubscribe?.();
}

Expand All @@ -101,52 +103,69 @@ class ReadableShapeStream<T extends Row<unknown> = Row> {
const source = new ReadableStream<Message<T>[]>({
start: (controller) => {
this.#unsubscribe = this.#stream.subscribe(
(messages) => controller.enqueue(messages),
(messages) => {
if (!this.#isStreamClosed) {
controller.enqueue(messages);
}
},
this.#handleError.bind(this)
);
},
cancel: () => {
this.#isStreamClosed = true;
this.#unsubscribe?.();
}
});

// Create the transformed stream that processes messages and emits complete rows
this.#changeStream = createAsyncIterableStream(source, {
transform: (messages, controller) => {
const updatedKeys = new Set<string>();

for (const message of messages) {
if (isChangeMessage(message)) {
const key = message.key;
switch (message.headers.operation) {
case "insert": {
// New row entirely
this.#currentState.set(key, message.value);
updatedKeys.add(key);
break;
if (this.#isStreamClosed) {
return;
}

try {
const updatedKeys = new Set<string>();

for (const message of messages) {
if (isChangeMessage(message)) {
const key = message.key;
switch (message.headers.operation) {
case "insert": {
this.#currentState.set(key, message.value);
updatedKeys.add(key);
break;
}
case "update": {
const existingRow = this.#currentState.get(key);
const updatedRow = existingRow
? { ...existingRow, ...message.value }
: message.value;
this.#currentState.set(key, updatedRow);
updatedKeys.add(key);
break;
}
}
case "update": {
// Merge updates into existing row if any, otherwise treat as new
const existingRow = this.#currentState.get(key);
const updatedRow = existingRow
? { ...existingRow, ...message.value }
: message.value;
this.#currentState.set(key, updatedRow);
updatedKeys.add(key);
break;
} else if (isControlMessage(message)) {
if (message.headers.control === "must-refetch") {
this.#currentState.clear();
this.#error = false;
}
}
} else if (isControlMessage(message)) {
if (message.headers.control === "must-refetch") {
this.#currentState.clear();
this.#error = false;
}
}
}

// Now enqueue only one updated row per key, after all messages have been processed.
for (const key of updatedKeys) {
const finalRow = this.#currentState.get(key);
if (finalRow) {
controller.enqueue(finalRow);
// Now enqueue only one updated row per key, after all messages have been processed.
if (!this.#isStreamClosed) {
for (const key of updatedKeys) {
const finalRow = this.#currentState.get(key);
if (finalRow) {
controller.enqueue(finalRow);
}
}
}
} catch (error) {
console.error("Error processing stream messages:", error);
this.#handleError(error as Error);
}
},
});
Expand Down Expand Up @@ -192,6 +211,8 @@ class ReadableShapeStream<T extends Row<unknown> = Row> {
if (e instanceof FetchError) {
this.#error = e;
}
this.#isStreamClosed = true;
this.#unsubscribe?.();
}
}

Expand Down