Chakracore: Memory leak in JsSetCurrentContext

Created on 19 Apr 2018  路  12Comments  路  Source: chakra-core/ChakraCore

Hi folks,

I've noticed that JsSetCurrentContext seems to leak small amounts of memory; see the screenshot below for a backtrace from Instruments on macOS. We're using ChakraCore in a server process, and during stress testing yesterday I noticed that we were leaking memory. Instruments shows that JsSetCurrentContext calls appear to be the cause; all roads lead to ThreadContextTLSEntry in the backtraces.

Is it possible that there's some required cleanup that I'm missing here? In our current design, request threads "borrow" a runtime from a pool, so we end up calling JsSetCurrentContext at least once per request. Are we supposed to "un-set" the context when a thread is finished using a runtime?

image

Bug Linux 3

Most helpful comment

Well, by design may be a bit aggressive, since the design seems to be "we don't know how to do this on xplat right now". If there is a comment in the code base acknowledging that there is an issue, I think there should be an actual open issue for it.

All 12 comments

FYI, I did a quick spike of a different architecture, where each runtime is confined to a single thread. I didn't change any code that calls ChakraCore functions, just which threads do the calling.

The leak disappeared - makes me think that TLS entries are not freed when their associated thread dies. I don't know if this is by-design, but this approach seems to fix my immediate problem. Thanks again!

I believe all access to ChakraCore runtimes is supposed to happen on the same thread on which it was created (literally "the script thread" or, in browsers, "the UI thread"), so I am actually surprised this is the only issue that you ran into by calling Js* functions from thread A for contexts/runtimes in thread B

Interesting! Somehow, I missed that point; thanks for making it clear.

Regardless, I've worked around my problem; if you all consider this to be working as designed, please feel free to close the issue.

Ill wait until someone with more knowledge on the JsRT chimes in before closing it to make sure.

Nah you can definitely move runtime around and use it from different threads. The threading model is called rental threading.

I see. So then this leak sounds like a bug on our end?

On windows, the TLS entry will be deleted when the thread exits. That is by design.

@leirocks @curtisman Since this is by design, should we close this issue?

Seems to be a xplat only issue.

Well, by design may be a bit aggressive, since the design seems to be "we don't know how to do this on xplat right now". If there is a comment in the code base acknowledging that there is an issue, I think there should be an actual open issue for it.

I agree with @jackhorton - this definitely seems to be a known deficiency, rather than intended behavior. If you all decide that this isn't worth fixing, "wontfix" would be more appropriate than "By Design".

...I'd still vote for fixing, though 馃檹 馃檹 馃檹

Was this page helpful?
0 / 5 - 0 ratings

Related issues

YiWen-y picture YiWen-y  路  4Comments

luxiao6620217 picture luxiao6620217  路  6Comments

atefshehata picture atefshehata  路  3Comments

Kureev picture Kureev  路  5Comments

tommyZZM picture tommyZZM  路  5Comments