Closed
Description
- Version: master
- Platform: Linux
- Subsystem: async_hooks
What steps will reproduce the bug?
I have been trying to write some new scenarios for AsyncLocalStorage class, as mentioned in #31978, and came across this issue:
const { AsyncLocalStorage } = require('async_hooks');
const http = require('http')
const cls = new AsyncLocalStorage();
const server = http.createServer((req, res) => {
res.write('hello')
setTimeout(() => {
res.end(' world!')
}, 1000)
})
server.listen(12000, () => {
cls.run(() => {
const req = http.get('http://localhost:12000', (res) => {
const store = cls.getStore()
store.set('foo', '')
res.on('data', (d) => { console.log(cls.getStore()) });
})
req.end()
})
})
In this simple client-server program, the server is sending two chunks of data, forcing the ondata
handler to be invoked twice.
How often does it reproduce? Is there a required condition?
every time
What is the expected behavior?
I get an empty Map every time in the ondata callback
What do you see instead?
Unfortunately, the store is undefined
after the first invocation:
$ node foo.js
Map(1) { 'foo' => '' }
undefined
^C
$
Additional information
If I replace this with a simple timer code, this logic works fine:
const { AsyncLocalStorage } = require('async_hooks');
const cls = new AsyncLocalStorage();
cls.run(() => {
const store = cls.getStore()
store.set('foo', 'bar')
setInterval(() => {
console.log(cls.getStore().get('foo'))
}, 1000)
})
$ node timer.js
bar
bar
bar
bar
^C
Am I missing something?
Ping @vdeturckheim