Skip to content

Weird behavior of async_hooks in server request events #32195

Closed

Description

@yenkn
  • 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!

Activity

  1. added
    async_hooks
    on Mar 11, 2020
  2. addaleax commented on Mar 11, 2020

    @addaleax
    Member

    @nodejs/async_hooks

  3. Flarna commented on Mar 12, 2020

    @Flarna
    Member

    To my understanding the difference is because setTimeout() (and also setImmediate() schedules it's callback in next event loop run wheres Promise.resolve().then() (and also process.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 data is registered.
    In setTimeout() variant registration of the data cb happens after lower layers have done all processing to know that incoming data is finished. Therefore the end callback is triggered directly after emitting the data event.
    In Promise variant registration on the data cb 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.

  4. yenkn commented on Mar 19, 2020

    @yenkn
    Author

    To my understanding the difference is because setTimeout() (and also setImmediate() schedules it's callback in next event loop run wheres Promise.resolve().then() (and also process.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 data is registered.
    In setTimeout() variant registration of the data cb happens after lower layers have done all processing to know that incoming data is finished. Therefore the end callback is triggered directly after emitting the data event.
    In Promise variant registration on the data cb 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.

    That's the answer I need, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    async_hooks

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions