-
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
fix(context): fixed runPromise mem leak and incorrect propagation #73
base: master
Are you sure you want to change the base?
Conversation
But if you actually have a promise in your e.g. if you had:
wouldn't that log 'outer'? I'm not sure if we can have our cake and eat it here without a much larger change to how cls is handling the 'current' context and I'm not sure what that would even have to look like. I stumbled upon this because I was looking to see if there was an issue/PR because I'm definitely running into a similar issue to what you're trying to fix; I'm trying to use this in conjunction with nestJS and Sequelize to get automatic transaction handling, but I'm running into issues where multiple http requests are using the same context and overwriting each others values because I don't think the way this is setup is built to handle async/parallel usage... The scenario I'm personally struggling with essentially boils down to:
Without your fix, I get: But with your fix, I get: |
Hi, @eliabecardoso @AJKarnitis
I finally found the place where this was mentioned. Yes. I had this problem too. It manifests itself as:
Have you solved this problem yet? |
Hello @AJKarnitis @Jef-Z. Sorry for the long time without answering. I also had the same problem, but in my case it was with async lib (v1) in legacy codes. // global.js
// ...
/**
* Async lib in its v1 has loss of reference and binding issues.
* Something that doesn't happen in v2 and v3, but we have differences in the way we use the methods that prevent us from upgrading the version.
* This is a forced rebind was created to palliatively solve the problem.
*/
const waterfall = async.waterfall.bind(async);
const parallel = async.parallel.bind(async);
function bindContextWrap(tasks, callback) {
const mustBind = process.versions.node < '16.17';
const storage = ContextStore.getInstance().storage;
const refTasks = tasks.map(task => (mustBind ? storage.bind(task) : task));
const refCallback = mustBind ? storage.bind(callback) : callback;
return this(refTasks, refCallback);
}
async.waterfall = bindContextWrap.bind(waterfall);
async.parallel = bindContextWrap.bind(parallel); Note:
My PR is mainly focused on fixing memory leaks and bad propagation in functions that don't lose their context. Therefore, in some cases, you will need to correct in ways similar to the above code. I hope this explanation brings a light at the end of the tunnel to the problem you are facing. |
fe3264a
to
3471d3a
Compare
This PR is fixing:
runPromise
method.exit
method was being called after the promise was resolved/rejected, it looks like it was not clearing theactive
property in time for the nextcreateContext
called on the nextrunPromise
.No side effects occured after this fix.
Tests:
Before fix:
After fix:
Inner Propagation fixed: