Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
13 changes: 12 additions & 1 deletion packages/core/src/flushable-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,12 @@ export async function flushablePipe(

// Read from source - don't count as pending op since we're just waiting for data
// The important ops are writes to the sink (server)
const readResult = await reader.read();
const readResult = await Promise.race([
reader.read(),
writer.closed.then(() => {
throw new Error('Writable stream closed prematurely');
}),
]);

// Check if stream has ended (e.g., due to error in another path) before processing
if (state.streamEnded) {
Expand Down Expand Up @@ -257,8 +262,14 @@ export async function flushablePipe(
// while other callers may depend on this rejection. Some known callers
// explicitly ignore this rejection (`.catch(() => {})`) and rely solely
// on `state.reject(err)` for error handling.

// Attempt to cancel the upstream reader so the source knows it should stop generating data.
reader.cancel(err).catch(() => {});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should all of these .catch(() => {}); statements instead log a warning? Presumably closing should work in most cases, and we'd like to know if it's not possible to close cleanly

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's log a warning for any cancel failures

throw err;
} finally {
// If we're exiting normally but the stream was externally ended before completion,
// we should cancel the reader to notify the source.
reader.cancel().catch(() => {});
reader.releaseLock();
writer.releaseLock();
}
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,12 @@ export class WorkflowServerReadableStream extends ReadableStream<Uint8Array> {
controller.enqueue(result.value);
}
},
cancel: async (reason) => {
if (this.#reader) {
await this.#reader.cancel(reason).catch(() => {});
this.#reader = undefined;
}
},
});
}
}
Expand Down