src: experimental green threads implementation #67288
All Vouch review confidence checks passed
Residual risk 0.30 = Change Risk (0.52) × (1 − Review Confidence (0.43)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #67288"
general
weight: 0.35
bakkot
~~This is an experiment... opening for feedback.~~ This is ready for review.
This implements green threads for Node.js. A green thread is a lightweight kind of worker. It has its own V8 isolate with no Node.js APIs. They run on a small pool of physical threads that they share, instead of each getting an OS thread and event loop like a worker thread. Each one gets work from the host through messages, RPC calls, or its module's exports, and every piece of work runs in an invocation context of its own. Each green thread can have limits on CPU time and memory, and the host can terminate it. This makes them cheap to start and to keep around in large numbers, and suited to running many small, untrusted pieces of code, such as plugins, per-tenant handlers, or user-supplied scripts.
For later, the plan is to add the following APIs (but not in this PR)
TextEncoder/TextDecoderURL/URLSearchParamsstructuredClone()<details>
Compared with worker threads
Green threads have real advantages over workers, in specific areas:
isolate-group setting. A thousand tenants take about 1 GB instead of 6.7 GB.
1000, and serve about 15–20% more requests per second.
limits.
They don't win on the speed of a single message, and they can't spread heavy
computation across all cores, since the pool is capped at 8 threads.
Setup for this performance comparison
- Release: the default build, without pointer compression. It doesn't
support isolate groups, so every green thread is in V8's default group.
- Pointer compression: supports isolate groups. By default, each green
thread is in an isolate group of its own.
kMaxPoolThreads).Each measurement runs in a fresh child process, so that memory numbers don't
include what earlier measurements left behind.
Kinds of green threads measured:
| Kind | Meaning |
|---|---|
|
green| Default options: an isolate group of its own where supported ||
green-default|isolateGroup: 'default', V8's default isolate group ||
green-shared| OneIsolateGroupshared by all instances ||
green-warm| Default options, taken from a warm pool filled beforehand |Memory and startup
1000 idle instances, started at once, measured once each has sent a message.
| | RSS per instance | Starts per second | OS threads |
|---|---|---|---|
| Worker | 6.7–7.9 MB | ~720 | 1000 |
| Green, Release build | 5.2 MB | ~750 | 8 |
| Green, pointer compression (own isolate group) | 2.8 MB | ~1,220 | 8 |
| Green, pointer compression (
isolateGroup: 'default') | 2.6 MB | ~810 | 8 || Green, shared
IsolateGroup| 1.03 MB | ~1,590 | 8 || Green, from a warm pool | same as cold | 44,000–66,000 | 8 |
worker carries a whole Node.js environment, an event loop and an OS thread
with its stack. A green thread is a V8 isolate with a small runtime.
are.
setWarmPoolSize(n)prepares isolates in advance, sostarting a green thread takes about 20 µs. Workers have no equivalent.
shared isolate group.
Speed
##### Message round trips
Sequential round trips with one instance.
| | Round trips per second | Latency |
|---|---|---|
| Green | ~57,000 | ~17.5 µs |
| Worker | ~61,000 | ~16.5 µs |
Workers are about 5% faster per message.
##### CPU-bound tasks
2000 tasks of
fib(27), each executor taking the next task once it's done.Release build.
| | Tasks per second | Time |
|---|---|---|
| Green ×8 | 2,466 | 811 ms |
| Green ×2000 (one per task) | 2,226 | 898 ms |
| Worker ×8 | 2,478 | 807 ms |
| Worker ×88 | 9,759 | 205 ms |
At equal parallelism, CPU-bound code runs at the same speed. Green threads
can't use more than 8 cores, though, so for heavy parallel computation
workers win clearly. The cap is a choice in this implementation, not
inherent, and could be made configurable.
##### Many tenants
1000 tenants, each with code and state of its own, serving 50,000 requests
spread over all of them, 64 at a time.
| | All started | Requests per second | RSS total |
|---|---|---|---|
| Worker (Release) | 1,653 ms | ~129,000 | 7,974 MB |
| Green (Release) | 1,332 ms | ~150,000 | 5,267 MB |
| Worker (pointer compression) | 1,387 ms | ~126,000 | 6,725 MB |
| Green (pointer compression) | 836 ms | ~149,000 | 2,883 MB |
| Green, shared
IsolateGroup| 632 ms | ~149,000 | 1,112 MB |Green threads serve about 15–20% more requests, since 8 pool threads don't
compete for the CPU the way 1000 OS threads do.
##### Runaway code
One of 100 tenants runs into an endless loop, while the others keep serving.
| | Stopped by | Stopped after | Others served in 200 ms |
|---|---|---|---|
| Green |
ERR_GREEN_THREAD_CPU_LIMIT(maxTaskCpuTimeMs: 50) | ~60 ms | ~35,000 || Worker | A host timer, then
terminate()| ~70 ms | ~31,500 |Both stop it. Green threads do it with a limit on CPU time, which the runtime
enforces. Workers need a timer by wall time on the host, which also stops
code that's only waiting.
What's easier to build: a plugin host
The same plugin host, built with each. It runs five plugins, which are ES
modules exporting
transform(text). Plugins can import a shared module, andcall back into the host for configuration and logging. Three of them
misbehave.
| Plugin | Green threads | Workers |
|---|---|---|
|
upper,reverse(well-behaved) | Work | Work ||
spin(endless loop) |ERR_GREEN_THREAD_CPU_LIMITafter 100 ms of CPU time | The host's own wall-clock timeout, which also stops plugins that are only waiting ||
hog(allocatesArrayBuffers) | Allocation fails at the 32 MBmaxMemoryMblimit | Not limited, sinceresourceLimitsonly cover the JS heap. Reached 91 MB within the 100 ms timeout ||
fail(throws with a code) | Error arrives withcode: 'ERR_PLUGIN'| Only with hand-written error forwarding |The green thread version is 43 lines:
The worker version is 155 lines, since it has to build what green threads
provide:
plugins are written to a temporary directory. Green threads take a
modulesbundle andresolve()/load()hooks.calls, and error forwarding, roughly what Comlink does. Green threads get
exportsandenvas RPC stubs.ArrayBufferlimit.Green threads have both.
require('fs'). The permission model applies to the whole process, so itcan't restrict a single worker. Green threads have no Node.js APIs at all:
they can only do what the host passes them. This is the biggest
qualitative difference.
When to use which
Use green threads for:
limits.
memory per instance matter.
Use workers for:
fs,net,child_process), or npmpackages that use them.
is capped at 8 threads.
Atomics.wait()and synchronous waits are disabled in greenthreads, since a blocked isolate would hold up a shared pool thread.
Caveats
f14fd2d7548,which doesn't have the ES modules and errors commits. These scenarios don't
use either.
run than the rest of its table. The first run used a setting that bypassed
the pool.
machines, the gap is likely larger.
a watchdog that abandons such isolates. Workers are affected by the same
bug when terminated.
</details>