Jeff-Lewis / cls-hooked

cls-hooked : CLS using AsynWrap or async_hooks instead of async-listener for node 4.7+

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Question] How do contexts get persisted?

niklasR opened this issue · comments

Hello,

I've been trying to get this to work in combination with the aws-xray-sdk (for aws/aws-xray-sdk-node#12). It's my first time working with CLS and async hooks.

For some reason, my context is not present anymore when the destroy hook for that asyncId is called.

In this specific example, when entering the context, I have currentUid:146.

So, when setting a key in my namespace, for the context with the currentUid:146, that gets set in the active field of the namespace for that id (146).

Before exiting the context, another init hook gets called (150). At this point, namespace.active is still set to the context from 146, so we get into this case, where the current namespace.active with ID 146 gets saved in namespace._contexts with the asyncID of 150.

This context now gets deleted from namespace._contexts when 150 gets destroyed as per this, even though 146 has not been destroyed yet.

Here the debug log output (with some extra debug info I've added): log.txt

I'm fairly sure I'm just not understanding the code correctly, as this seems fairly obvious.

I have attempted a very basic patch in my fork here, to illustrate the above, but that is unlikely to be a fix (and comes with other issues..)