neon-bindings / neon

Rust bindings for writing safe and fast native Node.js modules.
https://www.neon-bindings.com/
Apache License 2.0
8k stars 283 forks source link

Relax Send constraints #979

Closed kjvalencik closed 1 year ago

kjvalencik commented 1 year ago

Currently, Neon pessimistically assumes that JavaScript runtimes may choose to execute from different threads. The specification only defines serial execution and not necessarily single threaded execution.

In practice, this will always be one thread. V8 makes heavy usage of TLS and it would be a large lift to start doing things differently.

This PR changes Neon to optimistically assume the runtime will always execute from the same thread. As a small level of protection, I added SendWrapper when debug_assertions are enabled. This will panic if the single-thread invariant is violated.