Skip to content

Commit 861d9fe

Browse files
committed
Run V8 on separate thread
Rationale, implementation and known bugs are documented in DESIGN.md but the elevator pitch is that Ruby and V8 don't like sharing the same system stack. mini_racer_extension.cc has been split into mini_racer_extension.c and mini_racer_v8.cc. The former deals with Ruby, the latter with JS. This work has been sponsored by Discourse.
1 parent 8d1e66c commit 861d9fe

File tree

11 files changed

+3193
-2591
lines changed

11 files changed

+3193
-2591
lines changed

DESIGN.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
rationale
2+
=========
3+
4+
Before the commit that added this document, Ruby and V8 shared the same system
5+
stack but it's been observed that they don't always co-exist peacefully there.
6+
7+
Symptoms range from unexpected JS stack overflow exceptions and hitting debug
8+
checks in V8, to outright segmentation faults.
9+
10+
To mitigate that, V8 runs on separate threads now.
11+
12+
implementation
13+
==============
14+
15+
Each `MiniRacer::Context` is paired with a native system thread that runs V8.
16+
17+
Multiple Ruby threads can concurrently access the `MiniRacer::Context`.
18+
MiniRacer ensures mutual exclusion. Ruby threads won't trample each other.
19+
20+
Ruby threads communicate with the V8 thread through a mutex-and-condition-variable
21+
protected request/response memory buffer.
22+
23+
The wire format is V8's native (de)serialization format. An encoder/decoder
24+
has been added to MiniRacer.
25+
26+
Requests and (some) responses are prefixed with a single character
27+
that indicates the desired action: `'C'` is `context.call(...)`,
28+
`'E'` is `context.eval(...)`, and so on.
29+
30+
A response from the V8 thread either starts with:
31+
32+
- `'\xff'`, indicating a normal response that should be deserialized as-is
33+
34+
- `'c'`, signaling an in-band request (not a response!) to call a Ruby function
35+
registered with `context.attach(...)`. In turn, the Ruby thread replies with
36+
a `'c'` response containing the return value from the Ruby function.
37+
38+
Special care has been taken to ensure Ruby and JS functions can call each other
39+
recursively without deadlocking. The Ruby thread uses a recursive mutex that
40+
excludes other Ruby threads but still allows reentrancy from the same thread.
41+
42+
The exact request and response payloads are documented in the source code but
43+
they are almost universally:
44+
45+
- either a single value (e.g. `true` or `false`), or
46+
47+
- a two or three element array (ex. `[filename, source]` for `context.eval(...)`), or
48+
49+
- for responses, an errback-style `[response, error]` array, where `error`
50+
is a multi-line string that contains the error message on the first line,
51+
and, optionally, the stack trace. If not empty, the error string is turned
52+
into a Ruby exception and raised.
53+
54+
deliberate changes & known bugs
55+
===============================
56+
57+
- Forking is no longer supported. `fork(2)` and threads don't go well together.
58+
After the fork, only the calling thread remains; the V8 thread is gone and
59+
likely left mutexes and other resources in an undefined state.
60+
61+
- The `Isolate` class is gone. Maintaining a one-to-many relationship between
62+
isolates and contexts in a multi-threaded environment had a bad cost/benefit
63+
ratio. `Isolate` methods like `isolate.low_memory_notification` have been
64+
moved to `Context`, ex., `context.low_memory_notification`.
65+
66+
- The `marshal_stack_depth` argument is still accepted but ignored; it's no
67+
longer necessary.
68+
69+
- The `timeout` argument no longer interrupts long-running Ruby code. Killing
70+
or interrupting a Ruby thread executing arbitrary code is fraught with peril.
71+
72+
- Returning an invalid JS `Date` object (think `new Date(NaN)`) now raises a
73+
`RangeError` instead of silently returning a bogus `Time` object.
74+
75+
- Not all JS objects map 1-to-1 to Ruby objects. Typed arrays and arraybuffers
76+
are currently mapped to `Encoding::ASCII_8BIT`strings as the closest Ruby
77+
equivalent to a byte buffer.
78+
79+
- Not all JS objects are serializable/cloneable. Where possible, such objects
80+
are substituted with a cloneable representation, else a `MiniRacer::RuntimeError`
81+
is raised.
82+
83+
Promises, argument objects, map and set iterators, etc., are substituted,
84+
either with an empty object (promises, argument objects), or by turning them
85+
into arrays (map/set iterators.)
86+
87+
Function objects are substituted with a marker so they can be represented
88+
as `MiniRacer::JavaScriptFunction` objects on the Ruby side.
89+
90+
SharedArrayBuffers are not cloneable by design but aren't really usable in
91+
`mini_racer` in the first place (no way to share them between isolates.)

ext/mini_racer_extension/extconf.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
require 'mkmf'
22

3+
$srcs = ["mini_racer_extension.c", "mini_racer_v8.cc"]
4+
35
if RUBY_ENGINE == "truffleruby"
46
File.write("Makefile", dummy_makefile($srcdir).join(""))
57
return

0 commit comments

Comments
 (0)