Skip to content

fix(functions): release stream reader lock on iteration error - #2849

Open
Mohd Quamar Tyagi (Tyagiquamar) wants to merge 1 commit into
braintrustdata:mainfrom
Tyagiquamar:fix/stream-async-iterator-release-lock
Open

Mohd Quamar Tyagi (Tyagiquamar) wants to merge 1 commit into
braintrustdata:mainfrom
Tyagiquamar:fix/stream-async-iterator-release-lock

Conversation

@Tyagiquamar

Copy link
Copy Markdown
Contributor

Summary

Release the ReadableStreamDefaultReader lock in BraintrustStream[Symbol.asyncIterator] when reader.read() rejects due to a stream error.

Root Cause & Problem

In js/src/functions/stream.ts, BraintrustStream[Symbol.asyncIterator] acquires a reader via this.stream.getReader() and calls reader.releaseLock() when done is true, in return(), and in throw(). However, next() awaited reader.read() without a try/catch. When the underlying stream errored, reader.read() rejected and skipped reader.releaseLock(), leaving stream.toReadableStream().locked permanently true.

Technical Solution

  • Wrapped reader.read() in BraintrustStream[Symbol.asyncIterator]().next() in a try/catch block that calls reader.releaseLock() before rethrowing the error.
  • Added a regression test in js/src/functions/stream.test.ts verifying that when the underlying stream errors during for await iteration, the error is propagated and stream.toReadableStream().locked becomes false.

Test Plan

  • pnpm exec vitest run src/functions/stream.test.ts (in js/): passed (11/11 tests).
  • pnpm run check:typings (in js/): passed (tsc --noEmit && tsc --noEmit -p tsconfig.test.json, exit code 0).
  • pnpm exec vp fmt --check js/src/functions/stream.ts js/src/functions/stream.test.ts (repo root): passed.
  • pnpm exec vp lint js/src/functions/stream.ts js/src/functions/stream.test.ts (repo root): passed (0 warnings, 0 errors).

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.

1 participant