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

Clean old context references

jeremyvergnas opened this issue · comments

Hi there,

I was wondering whether the Map of contexts would be automatically cleaned at some point or should we use destroy to do that?

It could lead to performance issue as the Map is becoming bigger and bigger.

Thanks for your work!

@jeremyvergnas See #11 Memory leak in _contexts which discusses it. Contexts are removed on async_hook's destroy event, although it appears sometimes to take a while for some resources to be garbage collected.
That said, there was some additional work added in node 9.3.0 for destroy event in garbage collected AsyncResources, although I think the use of AsyncResources by embedder libs is small but hopefully growing.

It should be noted that usage of an HTTP agent with maxSockets: Infinity, maxFreeSockets: 256 can also result in the same situation: A lot of sockets will be created which results in a lot of cls-hooked context objects. The socket objects will only start to get GCed at a later point.

This becomes especially problematic when adding (potentially) large objects to the CLS context.

In our specific case, we are adding additional cleanup logic on top of cls-hooked to reduce the impact and likeliness of problems.

I did a quick test with 100 concurrent HTTP requests for like a couple of minutes watching the memory usage, did not see any major leakage on the heap_used so far. :)