chakra-core / chakra-core/ChakraCore

Multi-threaded synchronization

Open
#5,599 5 comments 0 reactions 0 assignees View on GitHub
Discussion
Dominant language
JavaScript
Stars
9.3k
Forks
1.2k
PR merge metrics
No merged PRs in 30d

Description

I started playing with ChakraCore in a multi-threaded environment (Rust, using a multi-threaded HTTP server) and stumbled into frequent `JsErrorWrongThread` errors. It seems to happen when trying to `JsSetCurrentContext` in a thread while it's active in another thread.

After chatting on gitter, I came to understand there's no synchronization going on in ChakraCore and it's the host's responsibility to handle that.

Coming from V8, they have a `v8::Locker` RAII which takes care of locking a `v8::Isolate` (eq to `JsRuntime`) for a given thread. I believe it works like:
- If the isolate is already locked to the current thread, increment a counter (a bit like a `JsAdd`)
- Once the Locker unlocks (instance drops out of scope), decrement the counter (like `JsRelease`)
- At `0`, this Isolate is ready to be locked onto a different thread (or the same, really)

This allows efficient usage within the same thread, but also a seamless experience across threads (when using the Locker.)

If `JsSetCurrentContext` could do that work, it'd be easy to use ChakraCore in multi-threaded environments. I think a different set of functions would be fine too.

Our use case is a multi-threaded HTTP server and also a multi-threaded event loop. Async operations might complete in a different thread.

Related question: What's the cost of switching a context to a different thread?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.