-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
context is lost on 'end' event of http request stream #36
Comments
@Jeff-Lewis I think this issue is related to |
@AiDirex did you find solution for your problem? |
Same here. Any solution? |
@Strate @PoslavskySV I decided to use my own naive implementation. const asyncHooks = require('async_hooks')
let contexts = {}
asyncHooks.createHook({
init (asyncId, type, triggerAsyncId, resource) {
if (contexts[triggerAsyncId] !== undefined) {
contexts[asyncId] = contexts[triggerAsyncId]
} else {
contexts[asyncId] = {}
}
},
destroy (asyncId) {
delete contexts[asyncId]
}
}).enable()
function run (callback) {
let eid = asyncHooks.executionAsyncId()
contexts[eid] = {}
callback()
}
function set (k, v) {
let eid = asyncHooks.executionAsyncId()
let ctx = contexts[eid]
if (ctx !== undefined) {
ctx[k] = v
}
}
function get (k) {
let eid = asyncHooks.executionAsyncId()
let ctx = contexts[eid]
return ctx !== undefined ? ctx[k] : undefined
}
module.exports = {get, set, run, contexts} It doesn't have the above issue, and I use it with async/await on node 8.14 without any problems. Also, I use these packages on my project: express, body-parse, Sequelize, oauth2-server, cookie-parser, and request-promise-native. |
@AiDirex Thanks! |
@AiDirex have you tried |
@Strate no. |
@Strate I tried, still bug is present. |
@Strate my code snippet worked without any problems so far. |
This is also a middleware for express which does something like the express-http-context package. const uuid = require('uuid/v4')
const {run, set} = require('./context')
module.exports = (req, res, next) => {
run(() => {
let ctxId = uuid()
set('context-id', ctxId)
next()
})
} Then in the rest of your middleware chain you can use |
@AiDirex sounds very promising! I am bit confused, that originally this library has a bit more code, especially inside async hook. I've tested your code against bluebird, context losed (I think this is bluebird's problem). Would you release your code as a package with cls-interfaces support? (for example, to just drop-replace this library?) |
@Strate it may have some issues with non-native promise implementations. I use it with async/await and native promises. It has more code because you can have multiple namespaces, my snippet is just one namespace. However, adding namespaces is easy. |
@AiDirex I think you might have found the issue/cause for why the http/net tests are failing in the dev branch of |
@Strate there's cls-bluebird that wraps |
Essentially the behavior of async_hooks changed in 8.10.0 which is described in nodejs #21078. Options we have are to either change behavior back to what it used to be and what is documented by node, or stick with existing behavior. Either way, the best option for |
@AiDirex - I'm debugging this and it appears to be an issue isolated the the |
@Jeff-Lewis am I right, that core difference between original cls-hooked mechanic and @AiDirex 's solution is |
@Strate My quick answer is that using The best option for cls-hooks is to have a workaround to the change in the behavior of Node, which is now inconsistent to the node docs, with regards to the |
I'm hoping some changes to async_hook, specifically nodejs/node#27581, would resolve this. Needs to be tested... |
Hi @Jeff-Lewis, any progress on this? |
@Jeff-Lewis There was a reply from Node maintainer side, they are OK with documenting status quo as the intended behaviour. |
@Jeff-Lewis |
@alexandruluca There are no good reasons to keep using cls-hooked on node 12.18.0, async-local-storage is fully supported there and is a recommended and actually properly supported alternative. |
Big thanks
…On Mon, 11 Jan 2021 at 20:53, Igor Savin ***@***.***> wrote:
@alexandruluca <https://github.com/alexandruluca> There are no good
reasons to keep using cls-hooked on node 12.18.0, async-local-storage is
fully supported there and is a recommended and actually properly supported
alternative.
You can check this bridge lib to see the ALS equivalents to cls-hooked:
https://github.com/kibertoad/asynchronous-local-storage/blob/master/lib/cls-fallback.ts
https://github.com/kibertoad/asynchronous-local-storage/blob/master/lib/als.ts
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#36 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABSAHNWAIR5PU3HHNPKVQDTSZNCLXANCNFSM4GJOPIYQ>
.
|
@kibertoad We use ALS of node 12.21.0, but the problem still exists. |
Here is a sample snippet:
As you can see I have created a http server and read the body by a stream. run the snippet then use curl.
curl http://localhost:8080
console output:
for simple GET requests the context is correct, but when you send data in the body the context is lost.
curl http://localhost:8080 -X POST -d aaaaaaaaaa
console output:
This issues is also related to this skonves/express-http-context#4. The problem is not the body-parser package, I have tracked it down and found that it is caused by the same issue as here, the callback of 'end' event looses context.
Node runtime: reproducible on all v8.14.0, v10.4.0, v10.3.0, and v10.14.1
OS: Ubuntu 18.04
cls-hooked: 4.2.2
The text was updated successfully, but these errors were encountered: