-
Notifications
You must be signed in to change notification settings - Fork 21
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
[BUG] "when" 3.7.7 package makes context get mixed up between concurrent HTTP requests #17
Comments
If I manage to include a solution to this issue in |
I just found a workaround to this issue!!! In @vijay22sai's code here, just replace this:
with this:
Explanation: the The The trick is to "capture" both the A good place to do this would be at the beginning of every user-defined middleware that uses the context. But this is not a trivial task. And it's not foolproof. In fact, some third-party middleware (i.e. neither one of yours, nor one from Strongloop) could lose context. And remember: once the context is lost, it's lost, no matter who wrote the middleware that lost it. Another trick is to write Same thing for Also, I called In general, if the package It's good to know that a workaround exists, even in difficult cases like this! But I need to investigate this, because it's error-prone and needs a lot of care. Maybe there is still some room for improvement? |
@josieusa Awesome!! It's great to know that a workaround exists for some third-party module like |
I
You must call
Only this way, if you call a method from a "cls-unaware" package (like Don't forget that other middlewares/connectors that you didn't write yourself could still lose context, because of course they don't know about this workaround, and therefore they don't follow this rule. Issues happening in that code (which should be open source) should be easier to track, though. Anyway, it's good to know that the loss of context won't happen anymore inside user code. |
I am adding a mention of this problem to project's README - see #18. @josieusa Thank you for investigating this problem and describing possible workarounds! To be honest, they all look a bit hacky to me and I am little bit reluctant to include them in the official documentation. I think this issue is only emphasising the fact that continuation local storage in Node.js is simply not ready for prime time yet :(
Let's see what you come up with and discuss the details in the pull request. If there is a reasonable way how to make loopback-context more robust, then I think it makes sense to implement it, even if it does not get us to 100% reliability. |
This has been discussed many times. I agree that it doesn't work out of the box. I didn't investigate enough the issues with connection pools yet, but I'll have to, sooner or later. Regarding the other issues with queues, I'm not sure that it all can't be solved (in an acceptable way, with some boilerplate) in user code; let's see how PR #19 goes. |
I believe this is fixed... just waiting for someone to take a look at my PR |
Add bind:true option to getCurrentContext Workaround issue #17 with cujojs/when v3.7.7 due to the fact that it internally uses a mechanism of user-land queuing for async-related operations, and possibly issues with similar packages using queuing (mostly Promise libraries)
Link to strongloop/loopback#2871
Description of feature (or steps to reproduce if bug)
If using the
[email protected]
package, the context may getlostmixed up between concurrent HTTP requests, as seen many, many times in similar issues, for examplestrongloop/loopback#2597strongloop/loopback#2397. Sorry if I won't repeat myself.Link to sample repo to reproduce issue (if bug)
Sorry, I'm going to reproduce the issue more properly using
loopback-sandbox
. Meanwhile, if someone wants to try to reproduce it, he/she can see this comment:strongloop/loopback#2871 (comment)
Expected result
Actual result (if bug)
Additional information (Node.js version, LoopBack version, etc)
Important: I'm using my fork of loopback-context from PR #11 and following its updated README:(PR 11 merged)https://github.com/josieusa/loopback-context/blob/feature/cls-hooked-3/README.md
One note: a similar issue with
async
package was solvedusingby my PR #11(it's tested), but the PR doesn't solve this other issue, so it's evident thatwhen
makes different things thanasync
.I don't know if the point in the code where the context gets lost is this one, but it may be:https://github.com/cujojs/when/blob/3.7.7/lib/env.js#L25
The text was updated successfully, but these errors were encountered: