Skip to content

fix(live): cancel a web ReadableStream source when the upload stops early - #194

Open
RaphaelFakhri wants to merge 1 commit into
AssemblyAI:mainfrom
RaphaelFakhri:fix/live-chunks-cancel-web-stream
Open

RaphaelFakhri wants to merge 1 commit into
AssemblyAI:mainfrom
RaphaelFakhri:fix/live-chunks-cancel-web-stream

Conversation

@RaphaelFakhri

Copy link
Copy Markdown

Summary

liveChunks() reads a web ReadableStream through a reader and only releases the lock when the generator ends. If the upload stops before the stream is exhausted, the source is never cancelled and keeps producing data nobody reads.

The upload stops early when:

  • the server rejects the request while audio is still in flight and finish() ends the body,
  • the request is aborted or hits the deadline and the body is cancelled,
  • a chunk is not bytes and asBytes() throws.

Node streams and other async iterables do not have this problem, because for await closes them when the loop exits early. This change cancels the reader when the generator exits without having read the stream to its end, so a web stream source behaves the same way.

The fix applies to sync.transcribeLive() and dictation.transcribeLive(), which both pass their input through liveChunks().

Changes

  • Track whether the stream was read to completion in liveChunks().
  • Cancel the reader in the finally block when it was not, and ignore a cancel failure from a stream that already errored.
  • Add unit tests for full consumption (no cancel), early stop, and a non-bytes chunk.
  • Bump the version to 4.41.6 and add a changelog entry.

Testing

Run pnpm test:unit. The new tests are in tests/unit/live-chunks.test.ts. Without the change in src/utils/live.ts, the early-stop and non-bytes tests fail because the source's cancel callback is never called.

Fixes #193

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

transcribeLive() does not cancel a web ReadableStream source when the upload ends early

1 participant