-
Notifications
You must be signed in to change notification settings - Fork 30.2k
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
AsyncLocalStorage.disable() strange behaviour #48480
Comments
Sir , i don't use Linux Machines but still according to me for The steps that would reproduce the bug will be 1.Create a new TypeScript file (e.g., bug.ts) and copy the code into it. 2.Install the required dependencies by running the following command: 3.Compile the TypeScript code to JavaScript using the TypeScript compiler (tsc) by running: 4.Run the generated JavaScript file using Node.js with the --experimental-async-hooks flag to enable the experimental async_hooks module: node --experimental-async-hooks bug.js When executing the code, it should run without any errors, and all assertions should pass. import { AsyncLocalStorage } from 'async_hooks'; const als = new AsyncLocalStorage<{foo: string}>(); This might work ..... Just a small Thinking |
Hi, Thanks for your response. The compiled JS code is as follows: const { AsyncLocalStorage } = require('async_hooks');
const assert = require('assert');
const als = new AsyncLocalStorage();
assert(als.getStore() === undefined);
als.enterWith({ foo: 'bar' });
assert(als.getStore()?.foo === 'bar');
als.disable();
assert(als.getStore() === undefined);
als.run({ foo: 'zoo' }, () => {
assert(als.getStore()?.foo === 'zoo');
});
assert(als.getStore() === undefined); You can run it without the experimental flag (as I'm using node 18), and it still fails... Can you explain what it is you are suggesting? Also changing the assertions order might get the code to work, but that doesn't change the fact that disable doesn't work as expected, as least as far as I understand what the |
The doc says following: ==> the store is re-enabled by calling |
Hi, indeed, but it can be interpreted that it can continue to operate after you disable it, but not with the previously created contexts. i.e. instead of calling it The docs also say
Which did not happen
This makes you believe that once it is disabled, it severs all ties to the resources bound (same as the first quote I gave) What I'm trying to say is that the docs are confusing, and maybe the current behavior is as intended and in that case, the docs need to be more explicit about it |
Could you point to the location in your sample where an old context gets "reactivated"? I see a new Do you have a reproducer that the ALS instance is not garbage collected after calling |
Hi, the last line demonstrates a reused context, I added comments in the code below I hope it is clear const { AsyncLocalStorage } = require('async_hooks');
const assert = require('assert');
const als = new AsyncLocalStorage();
assert(als.getStore() === undefined);
als.enterWith({ foo: 'bar' }); // here we enter context C1
assert(als.getStore()?.foo === 'bar');
als.disable();
assert(als.getStore() === undefined); // here context C1 doesn't exist as we called disdable
als.run({ foo: 'zoo' }, () => { // We create a new context C2 but it is isolated for the callback run and does not affect the last line as expected
assert(als.getStore()?.foo === 'zoo');
});
assert(als.getStore() === undefined); // This is not undefined and resuses context C1. If we never created C1 in the first place, this would be undefined and will not access C2 I know for a fact that it gets garbage collected because if I don't call disable, I get memory issues under certain situations. I'm not saying it is not garbage collected in this issue, but that the docs are a bit misleading - saying that it can be garbage collected infers that it has nothing holding on to it to prevent it from GC, which means it can't be reused. |
The concrete store on the concrete resource is removed at the end of |
wow ok this is enlighting - ok I understand what is going on now - I still believe that the docs are a bit misleading. thanks! |
Version
v18.12.1
Platform
Linux machine 5.15.0-73-generic #80-Ubuntu SMP Mon May 15 15:18:26 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
Subsystem
No response
What steps will reproduce the bug?
How often does it reproduce? Is there a required condition?
reproduces every time
What is the expected behavior? Why is that the expected behavior?
According to the docs
I would expect that contexts will be exited
What do you see instead?
if you keep using the storage after disable, the context is restored
Additional information
No response
The text was updated successfully, but these errors were encountered: