-
Notifications
You must be signed in to change notification settings - Fork 529
fix unhandledRejection misfires #6049
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
Open
anonrig
wants to merge
4
commits into
main
Choose a base branch
from
yagiz/fix-misfires-in-unhandledrejection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+370
−37
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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,223 @@ | ||
| // Regression tests for https://github.com/cloudflare/workerd/issues/6020 | ||
| // Unhandled rejection should NOT fire for promises that are handled through | ||
| // multi-tick promise chains. | ||
|
|
||
| import { strictEqual, ok, rejects } from 'node:assert'; | ||
| import { mock } from 'node:test'; | ||
|
|
||
| const asyncFunction = async (name) => { | ||
| throw new Error(`this function rejects: ${name}`); | ||
| }; | ||
|
|
||
| // Verifies assert.rejects handles rejections without unhandledrejection. | ||
| export const assertRejects = { | ||
| async test() { | ||
| const handler = mock.fn(); | ||
| addEventListener('unhandledrejection', handler); | ||
| try { | ||
| await rejects(async () => asyncFunction('A')); | ||
| strictEqual( | ||
| handler.mock.callCount(), | ||
| 0, | ||
| 'unhandledrejection should not fire for assert.rejects' | ||
| ); | ||
| } finally { | ||
| removeEventListener('unhandledrejection', handler); | ||
| } | ||
| }, | ||
| }; | ||
|
|
||
| // Verifies chained .then().catch() handling avoids unhandledrejection. | ||
| export const promiseChainCatch = { | ||
| async test() { | ||
| const handler = mock.fn(); | ||
| addEventListener('unhandledrejection', handler); | ||
| try { | ||
| const error = await Promise.resolve() | ||
| .then(() => asyncFunction('B')) | ||
| .then(() => null) | ||
| .catch((e) => e); | ||
| ok(error instanceof Error); | ||
| strictEqual(error.message, 'this function rejects: B'); | ||
| strictEqual( | ||
| handler.mock.callCount(), | ||
| 0, | ||
| 'unhandledrejection should not fire for .catch() chain' | ||
| ); | ||
| } finally { | ||
| removeEventListener('unhandledrejection', handler); | ||
| } | ||
| }, | ||
| }; | ||
|
|
||
| // Verifies try/catch around awaited chain avoids unhandledrejection. | ||
| export const tryCatchAwait = { | ||
| async test() { | ||
| const handler = mock.fn(); | ||
| addEventListener('unhandledrejection', handler); | ||
| try { | ||
| try { | ||
| await Promise.resolve('C').then(asyncFunction); | ||
| } catch (error) { | ||
| ok(error instanceof Error); | ||
| strictEqual(error.message, 'this function rejects: C'); | ||
| } | ||
| strictEqual( | ||
| handler.mock.callCount(), | ||
| 0, | ||
| 'unhandledrejection should not fire for try/catch' | ||
| ); | ||
| } finally { | ||
| removeEventListener('unhandledrejection', handler); | ||
| } | ||
| }, | ||
| }; | ||
|
|
||
| // Verifies a truly unhandled rejection still emits unhandledrejection. | ||
| export const genuineUnhandledRejectionStillFires = { | ||
| async test() { | ||
| const { promise, resolve } = Promise.withResolvers(); | ||
| const handler = mock.fn(() => resolve()); | ||
| addEventListener('unhandledrejection', handler, { once: true }); | ||
| Promise.reject('boom'); | ||
| await promise; | ||
| strictEqual( | ||
| handler.mock.callCount(), | ||
| 1, | ||
| 'unhandledrejection should fire for genuinely unhandled rejection' | ||
| ); | ||
| }, | ||
| }; | ||
|
|
||
| // Verifies unhandledrejection fires after a Promise.resolve tick. | ||
| export const unhandledRejectionAfterPromiseResolve = { | ||
| async test() { | ||
| const { promise, resolve } = Promise.withResolvers(); | ||
| const handler = mock.fn(() => resolve()); | ||
| addEventListener('unhandledrejection', handler, { once: true }); | ||
| Promise.reject('boom'); | ||
| await Promise.resolve(); | ||
| await promise; | ||
| strictEqual( | ||
| handler.mock.callCount(), | ||
| 1, | ||
| 'unhandledrejection should fire after Promise.resolve' | ||
| ); | ||
| }, | ||
| }; | ||
|
|
||
| // Verifies unhandledrejection followed by rejectionhandled on late catch. | ||
| export const lateHandlerTriggersRejectionhandled = { | ||
| async test() { | ||
| const { promise: unhandledPromise, resolve: resolveUnhandled } = | ||
| Promise.withResolvers(); | ||
| const { promise: handledPromise, resolve: resolveHandled } = | ||
| Promise.withResolvers(); | ||
| let unhandledReason; | ||
| let handledReason; | ||
| const unhandledHandler = mock.fn((event) => { | ||
| unhandledReason = event.reason; | ||
| resolveUnhandled(); | ||
| }); | ||
| const handledHandler = mock.fn((event) => { | ||
| handledReason = event.reason; | ||
| resolveHandled(); | ||
| }); | ||
| addEventListener('unhandledrejection', unhandledHandler, { once: true }); | ||
| addEventListener('rejectionhandled', handledHandler, { once: true }); | ||
| try { | ||
| const error = new Error('late'); | ||
| const promise = Promise.reject(error); | ||
| await unhandledPromise; | ||
| promise.catch(() => {}); | ||
| await handledPromise; | ||
| strictEqual( | ||
| unhandledHandler.mock.callCount(), | ||
| 1, | ||
| 'unhandledrejection should fire once before late handler' | ||
| ); | ||
| strictEqual( | ||
| handledHandler.mock.callCount(), | ||
| 1, | ||
| 'rejectionhandled should fire after late handler' | ||
| ); | ||
| ok(unhandledReason instanceof Error); | ||
| strictEqual(unhandledReason.message, 'late'); | ||
| strictEqual( | ||
| handledReason, | ||
| undefined, | ||
| 'rejectionhandled reason should be undefined' | ||
| ); | ||
| } finally { | ||
| removeEventListener('unhandledrejection', unhandledHandler); | ||
| removeEventListener('rejectionhandled', handledHandler); | ||
| } | ||
| }, | ||
| }; | ||
|
|
||
| // Verifies unhandledrejection handler can trigger another unhandled rejection. | ||
| export const handlerTriggeredUnhandledRejection = { | ||
| async test() { | ||
| const { promise, resolve } = Promise.withResolvers(); | ||
| const timeout = new Promise((resolveTimeout) => { | ||
| setTimeout(resolveTimeout, 25); | ||
| }); | ||
| const reasons = []; | ||
| let callCount = 0; | ||
| const handler = mock.fn((event) => { | ||
| reasons.push(event.reason); | ||
| callCount += 1; | ||
| if (callCount === 1) { | ||
| queueMicrotask(() => Promise.reject(new Error('second'))); | ||
| } | ||
| if (callCount === 2) { | ||
| resolve(); | ||
| } | ||
| }); | ||
| addEventListener('unhandledrejection', handler); | ||
| try { | ||
| Promise.reject(new Error('first')); | ||
| await Promise.race([promise, timeout]); | ||
| strictEqual( | ||
| handler.mock.callCount(), | ||
| 2, | ||
| 'unhandledrejection should fire for rejection triggered by handler' | ||
| ); | ||
| strictEqual(reasons.length, 2); | ||
| ok(reasons[0] instanceof Error); | ||
| strictEqual(reasons[0].message, 'first'); | ||
| ok(reasons[1] instanceof Error); | ||
| strictEqual(reasons[1].message, 'second'); | ||
| } finally { | ||
| removeEventListener('unhandledrejection', handler); | ||
| } | ||
| }, | ||
| }; | ||
|
|
||
| // Verifies each unhandled rejection emits its own event. | ||
| export const multipleUnhandledRejections = { | ||
| async test() { | ||
| const { promise, resolve } = Promise.withResolvers(); | ||
| const timeout = new Promise((resolveTimeout) => { | ||
| setTimeout(resolveTimeout, 25); | ||
| }); | ||
| const handler = mock.fn(() => { | ||
| if (handler.mock.callCount() === 2) { | ||
| resolve(); | ||
| } | ||
| }); | ||
| addEventListener('unhandledrejection', handler); | ||
| try { | ||
| Promise.reject(new Error('one')); | ||
| Promise.reject(new Error('two')); | ||
| await Promise.race([promise, timeout]); | ||
| strictEqual( | ||
| handler.mock.callCount(), | ||
| 2, | ||
| 'unhandledrejection should fire for each unhandled rejection' | ||
| ); | ||
| } finally { | ||
| removeEventListener('unhandledrejection', handler); | ||
| } | ||
| }, | ||
| }; | ||
anonrig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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,17 @@ | ||
| using Workerd = import "/workerd/workerd.capnp"; | ||
|
|
||
| const unitTests :Workerd.Config = ( | ||
| services = [ | ||
| ( name = "unhandled-rejection-test", | ||
| worker = ( | ||
| modules = [ | ||
| (name = "worker", esModule = embed "unhandled-rejection-test.js") | ||
| ], | ||
| compatibilityFlags = [ | ||
| "nodejs_compat", | ||
| "unhandled_rejection_after_microtask_checkpoint", | ||
| ] | ||
| ) | ||
| ), | ||
| ], | ||
| ); |
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.