async_hooks: AsyncLocalStorage losing context
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- JavaScript
- Star
- 122k
- Fork
- 37.3k
- Merge trung bình
- 4 ngày 2 giờ
- Pull request đã merge (30 ngày)
- 283
Mô tả
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
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu với hành vi run/enterWith của AsyncLocalStorage trong bản tái hiện máy chủ HTTP, tập trung vào req.resume() và req.on('end', ...). So sánh việc truyền ngữ cảnh quan sát được với đảm bảo đã được ghi chép cho các thao tác bất đồng bộ. Được xem là hoàn tất khi dự án quyết định liệu việc truyền ngữ cảnh có nên thay đổi hay không, hoặc trường hợp biên và hành vi API phù hợp được ghi chép.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- javascript, node.js
- Lĩnh vực
- backend
- Loại issue
- Lỗi
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức độ hoạt động
- Ít trao đổi
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 42/100