Skip to content

Fix unhandled promise rejection tracker, again #1049

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
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

saghul
Copy link
Contributor

@saghul saghul commented May 6, 2025

Mark a rejected promise as handled if we are passing it as as a resolution up a promise chain.

bnoordhuis
bnoordhuis previously approved these changes May 6, 2025
@saghul
Copy link
Contributor Author

saghul commented May 6, 2025

@ChALkeR mind taking a look?

@ChALkeR
Copy link

ChALkeR commented May 7, 2025

#1038 (comment) now works

#1038 (comment) doesn't

assert.rejects also doesn't work due to #1038 (comment)

@ChALkeR
Copy link

ChALkeR commented May 7, 2025

Simplified testcase:

const promise = Promise.reject('reject')
const error = await Promise.resolve().then(() => promise).catch(e => e)
console.log('Got this error:', error)

This still crashes the process

@saghul
Copy link
Contributor Author

saghul commented May 7, 2025

Actually a simpler one:

const promise = Promise.reject('reject')
print(promise)

@saghul
Copy link
Contributor Author

saghul commented May 7, 2025

Back to the drawing board :-)

@ChALkeR
Copy link

ChALkeR commented May 7, 2025

Actually a simpler one:

const promise = Promise.reject('reject')
print(promise)

Node.js disagrees on that and treats that as unhandled (unlike the other examples above)

@ChALkeR
Copy link

ChALkeR commented May 7, 2025

Latest commit doesn't catch this anymore:

async function foo() {
  throw new Error('failed')
}

foo()

const setTimeout = globalThis.setTimeout || os.setTimeout
setTimeout(() => console.log('done'), 1000)

@saghul
Copy link
Contributor Author

saghul commented May 7, 2025

You're right...

@saghul saghul force-pushed the fix-unhandled-rejection-tracker2 branch from 51b4dda to 1c395ba Compare May 7, 2025 12:15
@saghul
Copy link
Contributor Author

saghul commented May 7, 2025

Removed the latest commit. I need to see how to tell those 2 cases appart.

@ChALkeR
Copy link

ChALkeR commented May 7, 2025

That depends on the event loop

In Node.js:

  • Promises with .catch attached in Promise.resolve().then are not treated as unhandled
  • Promises with .catch attached in process.nextTick are not treated as unhandled
  • Promises with .catch attached in setTimeout including setTimeout(,0, or setImmediate are treated as unhandled

@saghul saghul force-pushed the fix-unhandled-rejection-tracker2 branch from 1c395ba to 0452dfe Compare May 8, 2025 09:14
Delay checking for unhandled rejections so we can make sure to check
after other promise jobs have ran.
@saghul saghul force-pushed the fix-unhandled-rejection-tracker2 branch from 0452dfe to 8c43cf9 Compare May 8, 2025 09:14
@saghul saghul dismissed bnoordhuis’s stale review May 8, 2025 09:15

Changed the approach.

@saghul
Copy link
Contributor Author

saghul commented May 8, 2025

@bnoordhuis PTAL again, I changed the approach.

The way it currently works Promise.reject(...) would enqueue a tracker job always, and if another promise adds a then / catch to handle it, it will be too late because jobs are executed FIFO of course. So I added some logic in the unhandled rejection tracker to re-enqueue the job if it sees other stuff going on, this way, delaying the execution means there is a chance to handle the rejections.

@saghul
Copy link
Contributor Author

saghul commented May 8, 2025

@ChALkeR do your worst :-)

Copy link
Contributor

@bnoordhuis bnoordhuis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking for specific callbacks seems a little fragile but I don't have a better suggestion.

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.

3 participants