-
-
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
Merged
Changes from 1 commit
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,88 @@ | ||
| // <reference lib="deno.ns" /> | ||
|
|
||
| import { assertEquals } from 'https://deno.land/std@0.212.0/assert/mod.ts'; | ||
|
|
||
| /** | ||
| * Minimal reproduction of monitorStream to verify that reader.releaseLock() | ||
| * after stream completion does not cause an unhandled promise rejection. | ||
| * | ||
| * Per the WHATWG Streams spec, releaseLock() rejects reader.closed. | ||
| * Using .then(onDone, onDone) handles both the fulfilled and rejected cases | ||
| * so the rejection is suppressed. | ||
| */ | ||
| function monitorStream( | ||
| stream: ReadableStream<Uint8Array>, | ||
| onDone: () => void, | ||
| ): ReadableStream<Uint8Array> { | ||
| const reader = stream.getReader(); | ||
| reader.closed.then(() => onDone(), () => onDone()); | ||
| return new ReadableStream({ | ||
| async start(controller) { | ||
| let result: ReadableStreamReadResult<Uint8Array>; | ||
| do { | ||
| result = await reader.read(); | ||
| if (result.value) { | ||
| try { | ||
| controller.enqueue(result.value); | ||
| } catch (er) { | ||
| controller.error(er); | ||
| reader.releaseLock(); | ||
| return; | ||
| } | ||
| } | ||
| } while (!result.done); | ||
| controller.close(); | ||
| reader.releaseLock(); | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| Deno.test('monitorStream calls onDone and does not cause unhandled rejection after normal completion', async () => { | ||
| let doneCalled = false; | ||
|
|
||
| const source = new ReadableStream<Uint8Array>({ | ||
| start(controller) { | ||
| controller.enqueue(new TextEncoder().encode('chunk1')); | ||
| controller.enqueue(new TextEncoder().encode('chunk2')); | ||
| controller.close(); | ||
| }, | ||
| }); | ||
|
|
||
| const monitored = monitorStream(source, () => { | ||
| doneCalled = true; | ||
| }); | ||
|
|
||
| // Listen for unhandled rejections — the bug caused one here. | ||
| let unhandledRejection: PromiseRejectionEvent | undefined; | ||
| const handler = (e: PromiseRejectionEvent): void => { | ||
| e.preventDefault(); | ||
| unhandledRejection = e; | ||
| }; | ||
| globalThis.addEventListener('unhandledrejection', handler); | ||
|
|
||
| try { | ||
| const reader = monitored.getReader(); | ||
| const chunks: string[] = []; | ||
| const decoder = new TextDecoder(); | ||
|
|
||
| let result: ReadableStreamReadResult<Uint8Array>; | ||
| do { | ||
| result = await reader.read(); | ||
| if (result.value) { | ||
| chunks.push(decoder.decode(result.value)); | ||
| } | ||
| } while (!result.done); | ||
| reader.releaseLock(); | ||
|
|
||
| assertEquals(chunks, ['chunk1', 'chunk2']); | ||
|
|
||
| // Give microtasks a chance to settle so any unhandled rejection fires. | ||
| await new Promise(resolve => setTimeout(resolve, 50)); | ||
|
|
||
| assertEquals(doneCalled, true, 'onDone callback should have been called'); | ||
| assertEquals(unhandledRejection, undefined, 'should not have caused an unhandled promise rejection'); | ||
| } finally { | ||
| globalThis.removeEventListener('unhandledrejection', handler); | ||
| } | ||
| }); | ||
|
cursor[bot] marked this conversation as 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.