node icon indicating copy to clipboard operation
node copied to clipboard

AsyncLocalStorage seems to mix-up context using Node 16.20.2-2

Open shimitapiro opened this issue 1 year ago • 2 comments

Version

16.20.2-2-cm2.0.20231106

Platform

Mariner - linux

Subsystem

No response

What steps will reproduce the bug?

Seeing this in production systems, only reproducible by traces

 static runInContext<T>(func: () => Promise<T>): Promise<T> {
        const store: Map<string, any> = new Map([[REQUEST_CONTEXT_ID, Guid.create().toString()]]);
        return ContextManager.als.run(store, () => {
            logger.info('f97d2b0c-3c47-47eb-8764-05c45494ff05', `${ContextManager.runInContext.name} - Running in context request-context-id ${store.get(REQUEST_CONTEXT_ID)}`);
            return func();
        });
    }

This is being triggered by a middleware, there is a code later down the line that will set add store, what we can see is that one context is being replaced by an other call, both calls are getting called nearly at the same time

How often does it reproduce? Is there a required condition?

Pretty frequent, it seems whenever there are 2 API calls arriving at the same time

What is the expected behavior? Why is that the expected behavior?

I would expect for both context to not override

What do you see instead?

2 different context running in the same context

Additional information

No response

shimitapiro avatar Jan 01 '24 08:01 shimitapiro

can you provide a repro? No external dependencies

marco-ippolito avatar Jan 16 '24 14:01 marco-ippolito

external dependencies in what way ? unfortunately i only see it in prod env when 2 requests come in simultaneity, I was unable to repo this locally has hard as I tried, it seems that the set will see some values on a different context

shimitapiro avatar Feb 04 '24 15:02 shimitapiro

Facing the same issue. Any resolution here?

vikasdeepjangra avatar May 28 '24 13:05 vikasdeepjangra

v16 is EOL, can you check with newer version? If so please open a new issue with a repro

marco-ippolito avatar May 28 '24 13:05 marco-ippolito

I have seen the similar issue with the cls module for nestjs. And it happened in a way just as you described.

crysislinux avatar May 29 '24 02:05 crysislinux