Skip to content

createUIMessageStream does not cancel merged streams when the consumer cancels #21721

Description

@RaphaelFakhri

When the consumer of a createUIMessageStream response cancels (for example, the client disconnects), streams that were added with writer.merge() are not cancelled. The merge loop keeps reading the source until it ends and discards the chunks.

For a merged streamText(...).toUIMessageStream(), this keeps the upstream model request running and reading after nobody is listening, unless the caller also wired an abortSignal by hand.

Reproduction

const source = new ReadableStream({
  start(c) { c.enqueue({ type: 'start' }); },
  cancel() { console.log('source cancelled'); },
});

const stream = createUIMessageStream({
  execute: ({ writer }) => { writer.merge(source); },
});

const reader = stream.getReader();
await reader.read();
await reader.cancel('client disconnected');
// Expected: "source cancelled" is logged.
// Actual: nothing is logged.

AI SDK version

Current main (ai).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions