-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(deno): Handle reader.closed rejection from releaseLock() in streaming
#20187
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
+60
−2
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| // <reference lib="deno.ns" /> | ||
|
|
||
| import { assertEquals } from 'https://deno.land/std@0.212.0/assert/mod.ts'; | ||
|
|
||
| Deno.test('reader.closed.then(f, f) suppresses rejection when releaseLock is called on an open stream', async () => { | ||
| // Reproduces the bug from GitHub issue #20177: | ||
| // In monitorStream, reader.releaseLock() is called while the source stream | ||
| // is still open (e.g. the error path when controller.enqueue() throws). | ||
| // Per WHATWG Streams spec, this rejects reader.closed with a TypeError. | ||
| // Using .then(onDone, onDone) handles both cases; .finally() would propagate | ||
| // the rejection as unhandled. | ||
|
|
||
| let onDoneCalled = false; | ||
|
|
||
| const stream = new ReadableStream<Uint8Array>({ | ||
| start(controller) { | ||
| controller.enqueue(new TextEncoder().encode('data')); | ||
| // intentionally not closing — stream stays open | ||
| }, | ||
| }); | ||
|
|
||
| const reader = stream.getReader(); | ||
|
|
||
| // This is the exact pattern from monitorStream (line 84 in streaming.ts). | ||
| // With .finally(() => onDone()), this would propagate the rejection. | ||
| reader.closed.then( | ||
| () => { | ||
| onDoneCalled = true; | ||
| }, | ||
| () => { | ||
| onDoneCalled = true; | ||
| }, | ||
| ); | ||
|
|
||
| await reader.read(); | ||
|
|
||
| // This is what monitorStream does on the error path (line 98) when | ||
| // controller.enqueue() throws — releaseLock while the source is still open. | ||
| reader.releaseLock(); | ||
|
|
||
| let unhandledRejection: PromiseRejectionEvent | undefined; | ||
| const handler = (e: PromiseRejectionEvent): void => { | ||
| e.preventDefault(); | ||
| unhandledRejection = e; | ||
| }; | ||
| globalThis.addEventListener('unhandledrejection', handler); | ||
|
|
||
| try { | ||
| await new Promise(resolve => setTimeout(resolve, 50)); | ||
|
|
||
| assertEquals(onDoneCalled, true, 'onDone should have been called via the rejection handler'); | ||
| assertEquals(unhandledRejection, undefined, 'should not have caused an unhandled promise rejection'); | ||
| } finally { | ||
| globalThis.removeEventListener('unhandledrejection', handler); | ||
| } | ||
| }); | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unhandled rejection listener registered after triggering action
Low Severity
The
unhandledrejectionevent listener is registered on line 46, afterreader.releaseLock()on line 39 which is the action that would trigger the rejection. If the fix were ever reverted to.finally(), the unhandled rejection event could fire before the listener is attached, causing theassertEquals(unhandledRejection, undefined, ...)assertion to pass incorrectly — hiding the regression the test is meant to catch. The listener needs to be set up beforereader.releaseLock()to reliably detect unhandled rejections. Also, the test usessetTimeout(resolve, 50)which is a sleep-in-test pattern; a more deterministic signal would be preferable.Triggered by project rule: PR Review Guidelines for Cursor Bot
Reviewed by Cursor Bugbot for commit 7f65c39. Configure here.