You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Promisify crashing node (or making it hang) #49607
Linux xxx 6.2.0-31-generic #31-Ubuntu SMP PREEMPT_DYNAMIC Mon Aug 14 13:42:26 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
Subsystem
No response
What steps will reproduce the bug?
Run this code:
import{promisify}from"node:util";constasyncFunction=async()=>5;awaitpromisify(asyncFunction)();console.log('This does not show');
How often does it reproduce? Is there a required condition?
Every time
What is the expected behavior? Why is that the expected behavior?
Anything but a silent crash... if it's not allowed throw an error, if you do accept async functions maybe promisify should be a noop since async functions already return promises...
The same code crashing in the code I linked above (semantic-release/commit-analyzer#525) exits with code 0. I'm no node developer so I'm probably a lot out of my depth here...
I for sure can't catch whatever error is happening (I tried).
process.exitCode is probably the culprit of hte crash being silent, so it is crashing node but not silent by default...
* `13` **Unfinished Top-Level Await**: `await` was used outside of a function
in the top-level code, but the passed `Promise` never resolved.
What's happening is that promisify returns a promise which will be resolved once the last argument of asyncFunction is called. Because that never happens, the promise never settles, and the process exits because it has nothing left to do.
You need to call that last argument, e.g.:
import{promisify}from"node:util";constasyncFunction=async(callback)=>callback(null,5);awaitpromisify(asyncFunction)();console.log('This does show');
if you do accept async functions maybe promisify should be a noop since async functions already return promises...
An async function is indistinguishable from a sync function, I don't think there's a reliable way for the runtime to detect if the passed function is async or not. Maybe a linter could catch it though
Activity
aduh95 commentedon Sep 11, 2023
That's really surprising, I'm getting exit code 13, can you double check if you're seeing an exit code 0?
SamMousa commentedon Sep 11, 2023
I can, one sec.
Edit: hmm, you are right, this reproduction is code 13, looking into source repo to see if it might be 13 there as well.
SamMousa commentedon Sep 11, 2023
The same code crashing in the code I linked above (semantic-release/commit-analyzer#525) exits with code
0. I'm no node developer so I'm probably a lot out of my depth here...I for sure can't catch whatever error is happening (I tried).
process.exitCodeis probably the culprit of hte crash being silent, so it is crashing node but not silent by default...aduh95 commentedon Sep 11, 2023
Assuming you are seeing an exit code of 13, then it's the expected and documented behavior:
node/doc/api/process.md
Lines 3929 to 3930 in 718981e
What's happening is that
promisifyreturns a promise which will be resolved once the last argument ofasyncFunctionis called. Because that never happens, the promise never settles, and the process exits because it has nothing left to do.You need to call that last argument, e.g.:
An async function is indistinguishable from a sync function, I don't think there's a reliable way for the runtime to detect if the passed function is async or not. Maybe a linter could catch it though
SamMousa commentedon Sep 11, 2023
.constructor.nameallows you to, not sure if it's standard though.I'll look a bit further. I'm not sure the mistakes you point out mean there is no bug, or my reproduction is simply wrong...
util: runtime deprecate `promisify`-ing a function returning a `Promise`