Repository navigation
CLS store gets reset on the first callback in http scenario #32060
Description
Activity
Thanks @HarshithaKP , it's weird. I'll take a look. But It seems you are using an older version of the API. Did you compile Node.js from sources or did you use the userland module I created to test the API?
- addedasync_hooksIssues and PRs related to the async hooks subsystem.Issues and PRs related to the async hooks subsystem.
on Mar 3, 2020 @vdeturckheim, thanks for the quick response. I am on Node.js master built from source. My HEAD is at d4e4480093319f6d8f3a26be6aad8c02eb7d589a, were there recent changes went it ?
@HarshithaKP I managed to isolate the bug in
executionAsyncResource()(which is the underlying API used byAsyncLocalStorage:I updated the test
test/async-hooks/test-async-exec-resource-http.js:'use strict'; require('../common'); const assert = require('assert'); const { executionAsyncResource, executionAsyncId, createHook, } = require('async_hooks'); const http = require('http'); const hooked = {}; createHook({ init(asyncId, type, triggerAsyncId, resource) { hooked[asyncId] = resource; } }).enable(); const server = http.createServer((req, res) => { res.write('hello'); setTimeout(() => { res.end(' world!'); }, 1000); }); server.listen(0, () => { assert.strictEqual(executionAsyncResource(), hooked[executionAsyncId()]); http.get({ port: server.address().port }, (res) => { assert.strictEqual(executionAsyncResource(), hooked[executionAsyncId()]); res.on('data', () => { assert.strictEqual(executionAsyncResource(), hooked[executionAsyncId()]); }); res.on('end', () => { assert.strictEqual(executionAsyncResource(), hooked[executionAsyncId()]); server.close(); }); }); });
and now it fails.
CC @mcollina @Qard and @Flarna who understand this mechanism better than me:
basically the resource in the second call tores.on('data', () => {does not seem to be gne through the hook even if it has the same id as the resource on the line before.Reacted by Harshitha K P- addedconfirmed-bugIssues and PRs for confirmed bugs.Issues and PRs for confirmed bugs.
on Mar 3, 2020 I did a fast look and it seems there is still an issue related to reuse of HTTPParser. On my local machine above test fails on second
dataevent becauseexecutionAsyncResource()returns anHTTPParserbuthooked[executionAsyncId()]returns an instance ofHTTPClientAsyncResource.Reacted by Vladimir de TurckheimPretty sure this is an oversight in the PR that added
executionAsyncResource(). I’ll open a PR.- added a commit that references this issue
on Mar 3, 2020 #32063, with the test copied from above.
Thanks a lot @addaleax !
- added a commit that references this issue
on Mar 4, 2020 - added a commit that references this issue
on Mar 4, 2020 - added a commit that references this issue
on Mar 13, 2020 3 remaining items
- added a commit that references this issue
on Mar 24, 2020 - added a commit that references this issue
on Jul 27, 2026
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:
In this simple client-server program, the server is sending two chunks of data, forcing the
ondatahandler 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
undefinedafter the first invocation:Additional information
If I replace this with a simple timer code, this logic works fine:
$ node timer.js
Am I missing something?
Ping @vdeturckheim