Behavioral inconsistency in AsyncLocalStorage between Node.js versions requires investigation.
The issue reports inconsistent behavior in AsyncLocalStorage between Node.js 20 and 24 when combining exit() and enterWith() methods. The expected behavior in Node.js 20 differs from Node.js 24, causing issues during major version upgrades. The problem requires investigation into the AsyncLocalStorage implementation changes between versions.
24.14.0
I've encountered an inconsistency in AsyncLocalStorage behavior between Node.js 20 and 24 when using a combination of exit() and enterWith() methods. This change caused our major version upgrade to be blocked.
No response
const { AsyncLocalStorage } = require("async_hooks");
const http = require("http");
const storage = new AsyncLocalStorage();
// Set global store
storage.enterWith({ test1: 'test1' });
const server = http.createServer(async (req, res) => {
storage.exit(() => {
console.log('store 0----', storage.getStore()); // undefined (expected)
storage.enterWith({ test2: 'test2' });
const _handler = () => {
console.log('store 2----', storage.getStore());
};
console.log('store 1----', storage.getStore()); // { test5: 'test2' } (expected)
res.on("finish", () => _handler());
});
res.end('Hello World from port 8083\n');
});
server.listen(8084, () => {
console.log('Example: curl http://localhost:8084');
});
Node.js 20 output: Example: curl http://localhost:8084 store 0---- undefined store 1---- { test2: 'test2' } store 2---- { test2: 'test2' }
Node.js 24 output: Example: curl http://localhost:8084 store 0---- undefined store 1---- { test2: 'test2' } store 2---- { test1: 'test1' }
no
no
no
no
Claim this issue to let others know you're working on it. You'll earn 20 points when you complete it!