async_hooks: AsyncLocalStorage losing context
还没有人认领这个 Issue。
- 主要语言
- JavaScript
- 星标
- 122k
- 派生
- 37.3k
- 平均合并
- 4 天 2 小时
- 30 天内合并 PR
- 283
描述
Version
node v17.3.0 / 16.13.0
Platform
linux (likely all)
Subsystem
async_hooks
What steps will reproduce the bug?
Run
'use strict'
const { AsyncLocalStorage } = require('async_hooks')
const { createServer } = require('http')
const storage = new AsyncLocalStorage()
let counter = 0
createServer((req, res) => {
const id = counter++;
console.log('In Middleware with id ' + id);
storage.run({ id }, function () {
req.resume()
req.on('end', onEnd(res))
});
}).listen(3000)
function onEnd (res) {
return () => {
const store = storage.getStore()
console.log('store is', store)
res.end(JSON.stringify(store))
}
}
Then:
curl -d '{}' -H 'Content-Type: application/json' localhost:3000
You'd note that the store is empty.
How often does it reproduce? Is there a required condition?
All of them. Unfortunately the documentation is quite misleading and it assumes the above would work / we do not document the edge cases.
The problem originates on the fact that receiving an HTTP body is part of the http request AsyncRecource. Calling .resume() on it does not imply we are attaching the new storage to that AsyncResource. However our documentation states:
Runs a function synchronously within a context and returns its return value. The store is not accessible outside of the callback function. The store is accessible to any asynchronous operations created within the callback.
Unless somebody knows that calling resume() is not creating a new asynchronous operation, they will be expecting the context to be preserved.
As a further confirmation, the following code works:
'use strict'
const { AsyncLocalStorage } = require('async_hooks')
const { createServer } = require('http')
const storage = new AsyncLocalStorage()
let counter = 0
createServer((req, res) => {
const id = counter++;
console.log('In Middleware with id ' + id);
storage.enterWith({ id })
req.resume()
req.on('end', onEnd(res))
}).listen(3000)
function onEnd (res) {
return () => {
const store = storage.getStore()
console.log('store is', store)
res.end(JSON.stringify(store))
}
}
What is the expected behavior?
We might decide this is a bug that should be fixed or we might decide to stabilize enterWith and document this case.
The problem is that the current documentation is misleading folks into thinking storage.run() is the right API in all cases but it is not as things are.
What do you see instead?
No response
Additional information
No response
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 HTTP 服务器复现中的 AsyncLocalStorage run/enterWith 行为开始,重点关注 req.resume() 和 req.on('end', ...)。将观察到的上下文传播与文档中针对异步操作的保证进行比较。当项目决定是否应更改传播行为,或记录该边界情况及适当的 API 行为时,即视为完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- javascript, node.js
- 领域
- backend
- Issue 类型
- 缺陷
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 冷清
- 描述清晰度
- 基本清楚
- 新手友好度
- 42/100