Repository navigation
Closed
Labels
async_hooksIssues and PRs related to the async hooks subsystem.
Description
- Version: v10.15.1
- Platform: Darwin MacBookPro.local 19.2.0 Darwin Kernel Version 19.2.0: Sat Nov 9 03:47:04 PST 2019; root:xnu-6153.61.1~20/RELEASE_X86_64 x86_64
- Subsystem: http, async_hooks
What steps will reproduce the bug?
const http = require('http')
const ah = require('async_hooks')
const fs = require('fs')
ah.createHook({
init(asyncId, type, triggerAsyncId) {
fs.writeSync(1, `from ${triggerAsyncId} to ${asyncId} [${type}]\n`)
}
}).enable()
const server = http.createServer((req, res) => {
fs.writeSync(1, `request ${ah.executionAsyncId()}\n`)
Promise.resolve(123).then(() => {
fs.writeSync(1, `promise ${ah.executionAsyncId()}\n`)
req.on('data', (chunk) => {
fs.writeSync(1, `data ${ah.executionAsyncId()}\n`)
})
req.on('end', () => {
fs.writeSync(1, `end ${ah.executionAsyncId()}\n`)
})
res.end('hello')
})
})
server.listen(3001, () => {
const req = http.request('http://localhost:3001', {
method: 'POST'
})
req.end('hello from client')
})What is the expected behavior?
Output:
...
request 17
from 17 to 21 [PROMISE]
from 21 to 22 [PROMISE]
promise 22
from 22 to 23 [TickObject] <---- from promise(22) to data callback(23)
from 14 to 24 [TickObject]
from 22 to 25 [TickObject]
data 23
from 24 to 26 [TickObject]
from 14 to 27 [SHUTDOWNWRAP]
from 17 to 28 [TickObject] <---- expect `from 22 to 28`
like output above, from promise(22) to end callback(28)
end 28
...
but if we change Promise.resolve(123).then to setTimeout, the output becomes:
request 17
from 17 to 21 [Timeout]
from 17 to 22 [TIMERWRAP]
from 17 to 23 [TickObject]
promise 21
from 21 to 24 [TickObject] <---- from promise(21) to data callback(24)
from 14 to 25 [TickObject]
from 21 to 26 [TickObject]
from 24 to 27 [TickObject] <---- from data callback(24) to end callback(27)
data 24
from 24 to 28 [TickObject]
from 24 to 29 [TickObject]
from 25 to 30 [TickObject]
end 27
Can someone explain this? Thanks!
Metadata
Metadata
Assignees
Labels
async_hooksIssues and PRs related to the async hooks subsystem.
Activity
addaleax commentedon Mar 11, 2020
@nodejs/async_hooks
Flarna commentedon Mar 12, 2020
To my understanding the difference is because
setTimeout()(and alsosetImmediate()schedules it's callback in next event loop run wheresPromise.resolve().then()(and alsoprocess.nextTick()run their callback in the same even loop run (e.g. not sure if micro tick is the right word here).In your sample the whole HTTP message from client arrives in one TCP/IP packet. The HTTP/Stream modules split this up to first signal the request (incl. headers) and start signaling body once
datais registered.In
setTimeout()variant registration of thedatacb happens after lower layers have done all processing to know that incoming data is finished. Therefore theendcallback is triggered directly after emitting thedataevent.In
Promisevariant registration on thedatacb happens earlier. Therefore the end callback is triggered not at the time the data callback is triggered. It happens when http parser signals then stream has ended.yenkn commentedon Mar 19, 2020
That's the answer I need, thank you!