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
Inconsistent behavior of nextTick and queueMicrotask #51156
### queueMicrotask ### Error
at /Users/matteo/c.cjs:7:23
at process.processTicksAndRejections (node:internal/process/task_queues:77:11)
node:events:492
throw er; // Unhandled 'error' event
^
Error
at /Users/matteo/c.cjs:7:23
at process.processTicksAndRejections (node:internal/process/task_queues:77:11)
Emitted 'error' event at:
at /Users/matteo/c.cjs:7:9
at process.processTicksAndRejections (node:internal/process/task_queues:77:11)
Node.js v20.10.0
Why is this an issue? In most part of Node.js core,
we use process.nextTick(() => ee.emit('error')) to let users install event handlers.
However, if an EventEmitter is returned by an async function, there is a significant possibility that the error could not be caught.
(This is essentially a problem for Node.js streams).
Just to expand a bit. There is an assumption that process.nextTick tasks always run before micro-tasks. However, as demonstrated above. This is a false assumption. The reality is more inconsistent and depends whether or not the caller is inside our outside a micro tick.
It's not entirely clear to me what is the expected output in the OP, did you mean to say that you expect a ### setImmediate ### Error log in addition to the queueMicrotask one? Or did you mean to expect to see just the setImmediate one?
It's not entirely clear to me what is the expected output in the OP, did you mean to say that you expect a ### setImmediate ### Error log in addition to the queueMicrotask one? Or did you mean to expect to see just the setImmediate one?
You mean neither of them should log anything or they should both log or either is fine as long as they are the same? I am guessing from the OP (in the context of error handling) that you want both to log?
Hello @ronag, I would like to continue working on this issue, however, as mentioned on your PR, I guess a simple search and replace won't work?
I am actually new to contributing to NodeJS, so I am a little confused as to what we can implement here. If you could give me some pointers, it would be of great help.
This issue has been marked as stale due to 210 days of inactivity.
It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.
Activity
mcollina commentedon Dec 14, 2023
@jasnell what do you think?
ronag commentedon Dec 14, 2023
Just to expand a bit. There is an assumption that
process.nextTicktasks always run before micro-tasks. However, as demonstrated above. This is a false assumption. The reality is more inconsistent and depends whether or not the caller is inside our outside a micro tick.ronag commentedon Dec 18, 2023
I'm adding this to the TSC agenda for visibility and help with triaging the severity of the issue.
ronag commentedon Dec 18, 2023
Would probably be a good idea to update https://nodejs.org/en/guides/event-loop-timers-and-nexttick to also include promises and microticks.
joyeecheung commentedon Dec 18, 2023
It's not entirely clear to me what is the expected output in the OP, did you mean to say that you expect a
### setImmediate ### Errorlog in addition to thequeueMicrotaskone? Or did you mean to expect to see just thesetImmediateone?ronag commentedon Dec 18, 2023
The expectation is that:
queueMicrotask(run.bind(null, 'queueMicrotask'))and
setImmediate(run.bind(null, 'setImmediate'))Should have the same output, which they don't.
joyeecheung commentedon Dec 18, 2023
You mean neither of them should log anything or they should both log or either is fine as long as they are the same? I am guessing from the OP (in the context of error handling) that you want both to log?
ronag commentedon Dec 18, 2023
I think you could argue that either behavior is correct (or at least not incorrect).
The problem here is that they are not consistent.
NiharPhansalkar commentedon Dec 19, 2023
Hello @ronag, I would like to continue working on this issue, however, as mentioned on your PR, I guess a simple search and replace won't work?
I am actually new to contributing to NodeJS, so I am a little confused as to what we can implement here. If you could give me some pointers, it would be of great help.
ronag commentedon Dec 19, 2023
@NiharPhansalkar I don't think this is a good starter issue. Not even us in the technical steering are sure what to do about this issue.
mcollina commentedon Dec 20, 2023
@benjamingr here is the patch that fixes the behavior... but it breaks a lot of tests:
26 remaining items
doc: mark process.nextTick legacy
doc: mark process.nextTick legacy
doc: mark process.nextTick legacy
doc: mark process.nextTick legacy
github-actions commentedon May 24, 2026
This issue has been marked as stale due to 210 days of inactivity.
It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.
ronag commentedon May 24, 2026
@mcollina completed?
mcollina commentedon May 25, 2026
I just pressed a single button on mobile.