Closed averri closed 4 years ago
@averri, it would be helpful to be able to see the code to understand what is happening. I'm unable to recreate the issue locally.
I cannot reproduce on 13.11.0 either. Are you sure that's the version you're running (you can add a console.log(process.version)
to your script)?
Thanks @jasnell and @cjihrig. There was an issue with nvm
which was not setting the environment with Nodejs 13.11.0 as I was expecting. I reinstalled nvm, and got it working correctly.
I was using the code inspired by Nodejs 13 documentation (ref: What steps will reproduce the bug?):
const {AsyncLocalStorage} = require('async_hooks')
const http = require('http')
console.log(process.version)
const kReq = 'CURRENT_REQUEST'
const asyncLocalStorage = new AsyncLocalStorage()
function log(...args) {
const store = asyncLocalStorage.getStore()
// Make sure the store exists and it contains a request.
if (store && store.has(kReq)) {
const req = store.get(kReq)
// Prints `GET /items ERR could not do something
console.log(req.method, req.url, ...args)
} else {
console.log(...args)
}
}
http.createServer((request, response) => {
asyncLocalStorage.run(new Map(), () => {
const store = asyncLocalStorage.getStore()
store.set(kReq, request)
new Promise(resolve => {
log('INFO', 'sleeping...')
setTimeout(() => {
log('INFO', 'resolving...')
resolve()
}, 1000)
}).then(() => {
log('INFO', 'finished')
response.statusCode = 200
response.write('ok')
response.end()
})
})
})
.listen(8080)
Running this code with Nodejs 13.11.0 and invoking GET at /index
, I'm able to see the logs:
v13.11.0
GET /index INFO sleeping...
GET /index INFO resolving...
GET /index INFO finished
GET /favicon.ico INFO sleeping...
GET /favicon.ico INFO resolving...
GET /favicon.ico INFO finished
... which demonstrates that there are no issues.
What steps will reproduce the bug?
Run the demo code provided in https://nodejs.org/api/all.html#async_hooks_class_asynclocalstorage
How often does it reproduce? Is there a required condition?
Always.
What is the expected behavior?
Nodejs should recognize all the API functions and run the example code with success.
What do you see instead?