From dcda482eca6069341d75c17f2726009a46b5fabe Mon Sep 17 00:00:00 2001 From: islandryu Date: Thu, 2 Oct 2025 10:38:43 +0900 Subject: [PATCH] process: fix wrong asyncContext under unhandled-rejections=strict Fixes: https://github.com/nodejs/node/issues/60034 --- lib/internal/process/promises.js | 2 +- ...omise-unhandled-error-with-reading-file.js | 29 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-promise-unhandled-error-with-reading-file.js diff --git a/lib/internal/process/promises.js b/lib/internal/process/promises.js index 56a9f287a0a86e..da5581d650ab04 100644 --- a/lib/internal/process/promises.js +++ b/lib/internal/process/promises.js @@ -281,7 +281,7 @@ function strictUnhandledRejectionsMode(promise, promiseInfo, promiseAsyncId) { reason : new UnhandledPromiseRejection(reason); // This destroys the async stack, don't clear it after triggerUncaughtException(err, true /* fromPromise */); - if (promiseAsyncId === undefined) { + if (promiseAsyncId !== undefined) { pushAsyncContext( promise[kAsyncIdSymbol], promise[kTriggerAsyncIdSymbol], diff --git a/test/parallel/test-promise-unhandled-error-with-reading-file.js b/test/parallel/test-promise-unhandled-error-with-reading-file.js new file mode 100644 index 00000000000000..5a037eec7ca229 --- /dev/null +++ b/test/parallel/test-promise-unhandled-error-with-reading-file.js @@ -0,0 +1,29 @@ +// Flags: --unhandled-rejections=strict +'use strict'; + +const common = require('../common'); +const fs = require('fs'); +const assert = require('assert'); + +process.on('unhandledRejection', common.mustNotCall); + +process.on('uncaughtException', common.mustCall((err) => { + assert.ok(err.message.includes('foo')); +})); + + +async function readFile() { + return fs.promises.readFile(__filename); +} + +async function crash() { + throw new Error('foo'); +} + + +async function main() { + crash(); + readFile(); +} + +main();