Skip to content
gi-dellavPublic

About

Experimental JS runtime with ability to run 1k+ threads

Resources

Stars

5 stars

Watchers

0 watching

Forks

Repository files navigation

otter

Sea Otter

Experimental BEAM-like JavaScript runtime on QuickJS (via rquickjs).

Many isolated JS processes are multiplexed onto a small, fixed pool of OS worker threads; one system thread can manage thousands of engines. Each process owns its own QuickJS runtime (isolated heap, like a BEAM process) and a mailbox; processes communicate only by message passing.

Model

  • Process = one Runtime + Context + mailbox. Never shares JS state.
  • Scheduler = M worker threads pulling processes off a shared run queue. Each slice executes one pending QuickJS job, then the process is re-queued, parked, or finished. Processes migrate freely between worker threads.
  • Suspension = await recv() on an empty mailbox parks the process until a message arrives. Nothing blocks an OS thread.
  • Isolation = an uncaught error kills only its process; the exit code is non-zero if any process failed.

Install

cargo install --git https://github.com/gi-dellav/otter

Build & run

cargo build --release
otter [--workers N] script.js [more_scripts.js ...]

Every file on the command line starts as its own process (pids 0..n); the runtime exits when all processes (including spawned ones) have finished.

JSON-RPC control (RPC)

otter can expose a JSON-RPC 2.0 control socket over TCP, letting an external tool drive the scheduler exactly like the in-JS API: spawn, list, info, kill, send, rename, count, and shutdown. Enable it with --rpc-port (the runtime then stays alive until you call shutdown); frames are line-delimited, one request per line, so any CLI that speaks TCP works:

# start a runtime with a control socket on port 9000
otter --rpc-port 9000 script.js &

# drive it with netcat (or any line-based TCP client)
echo '{"jsonrpc":"2.0","id":1,"method":"list"}' | nc 127.0.0.1 9000
echo '{"jsonrpc":"2.0","id":2,"method":"spawn","params":{"code":"await recv();"}}' | nc 127.0.0.1 9000
echo '{"jsonrpc":"2.0","id":3,"method":"send","params":{"pid":1,"value":"hi"}}' | nc 127.0.0.1 9000
echo '{"jsonrpc":"2.0","id":4,"method":"shutdown"}' | nc 127.0.0.1 9000

Each request carries a numeric/string id and receives a matching response; notifications (no id) get none, per the JSON-RPC 2.0 spec.

JS API

API Description
spawn(code, opts?) Start a new process from a source string, returns its pid. Optional { sandbox: { canSpawnAndKill: false, llrtModules: false } } narrows the child's sandbox at birth.
send(pid, value) Serialize value to JSON and deliver it to pid's mailbox. Messages to dead pids are dropped silently.
await recv(timeoutMs?) Suspend until a message arrives; resolves with the parsed value. With a timeoutMs it rejects with a TimeoutError if no message arrives in time (a message that races the deadline stays in the mailbox).
await sleep(ms) Suspend the process for at least ms milliseconds; does not touch the mailbox.
await yieldNow() Voluntarily give up the current slice and rejoin the back of the run queue. (Not named yield because that word is reserved inside async-function bodies, which is how top-level-await scripts are parsed.)
self() The current process's pid.
killProcess(pid) Request termination of a live process; returns true if pid is live. Best-effort: the process is reaped at its next scheduling boundary (parked/sleeping processes immediately). Killing an unknown pid returns false.
listProcesses() Array of {pid, name, status} for every live process, sorted by pid.
isProcessAlive(pid) true while pid is live.
processInfo(pid) {pid, name, status} for a live pid, or null.
processCount() Number of live processes.
setName(name) Rename the current process; visible in listProcesses()/processInfo().
selfSandbox() Snapshot {canSpawnAndKill, llrtModules} of the current process's sandbox policy.
restrictSandbox(policy?, opts?) Narrow a sandbox at runtime (self by default, or {pid}). Monotonic/irrevocable; returns the post-state. Dropping canSpawnAndKill/llrtModules requires holding it; on another process only real narrowing (never widening) is allowed.
console.log/error Line-oriented output prefixed with the pid.

Scripts support top-level await. Full API docs live in docs/.

LLRT modules

otter can expose the Node.js-style builtins from llrt_modules (node:path, node:url, node:os, node:buffer, node:events, node:fs, node:crypto, node:net, fetch, timers, ...) behind the on-by-default llrt_modules cargo feature:

cargo build                          # with  LLRT modules (default)
cargo build --no-default-features    # without

Each process's QuickJS runtime is an async runtime driven through a shared Tokio reactor, so both the synchronous and the future-based modules work: require/import of builtins, setTimeout/setInterval/setImmediate, fs.promises, crypto, and even fetch.

A running process keeps live spawned work (for example a pending setTimeout) alive the way an event loop would: it finishes only once its async work drains. Killing a process therefore also drains its pending work before the runtime is released.

Whether a given process sees these modules is a sandbox toggle (llrtModules, see below), so a confined process gets no require, no module loader, and no Buffer/URL globals; restricting mid-process denies further imports live.

Examples

cargo run --release -- examples/ping_pong.js          # two processes volleying messages
cargo run --release -- --workers 1 examples/ring.js   # 1000 processes on ONE thread
cargo run --release -- examples/coop.js               # yieldNow()-driven interleaving
cargo run --release -- examples/timer.js              # sleep() and recv(timeoutMs) watchdog
cargo run --release -- examples/process_mgmt.js       # list, inspect, rename, and kill processes
cargo run --release -- examples/sandbox.js            # spawn confined children, self-restrict, no-escalation

Benchmarks

Release build, 4-core machine; numbers from benchmarks/ (illustrative, not a comparison suite).

Round-trip churn — two processes volleying a message as fast as possible (benchmarks/pingpong.js).

Workers Round-trips Time Throughput
1 100,000 783 ms ~128k round-trips/s
2 100,000 765 ms ~131k round-trips/s

Token-ring latency — one token around a 2,000-process ring and back, averaged over 3 trials (benchmarks/ring.js).

Workers Hops Time Throughput
1 1,999 482 ms ~4.1k hops/s
4 1,999 334 ms ~6.0k hops/s

Process lifecycle — spawn/run/reap cost, one process at a time (benchmarks/spawn.js).

Processes Time Throughput
5,000 7,455 ms ~671 procs/s

Timer accuracy — sleep()/recv(timeout) vs. wall clock (benchmarks/timers.js).

Call Requested Observed
sleep(0) 0 ms 3 ms
sleep(1) 1 ms 5 ms
sleep(5) 5 ms 6 ms
sleep(20) 20 ms 20 ms
sleep(100) 100 ms 100 ms
recv(50) 50 ms 52 ms (TimeoutError)

CPU parallelism — 8 CPU-bound processes (~300 ms each) spread over workers (benchmarks/cpu_fanout.js).

Workers Wall time Speedup
1 2,409 ms 1.0×
4 609 ms 4.0×
8 315 ms 7.6×

Limitations (v1)

  • Cooperative scheduling: a long synchronous loop without await/yieldNow() monopolizes its worker thread until it suspends (no preemption yet).
  • Killing is cooperative too: killProcess() takes effect at the target's next scheduling boundary, so a process stuck in a long synchronous loop won't die until it yields.
  • Timers are serviced by idle workers on a ~5 ms tick, so sleep()/recv() deadlines are accurate to within a tick or two, not to the millisecond.
  • At most one outstanding suspension per process: calling sleep() while a recv() is pending (or vice versa) raises a TypeError. recv()/yieldNow() after a sleep() is fine once the sleep has completed.
  • Messages must be JSON-serializable; functions, symbols and undefined are rejected.
  • Sandboxing covers two toggles today (canSpawnAndKill, llrtModules); they gate spawn, killProcess(other), and exposure of the LLRT builtins. send/recv/sleep/yieldNow are unrestricted, and there is no CPU/memory isolation. See docs/api.md.
  • Live async work keeps its process alive until it drains; a repeating setInterval therefore keeps its process (and the runtime) running, and killProcess takes effect once the target's pending work completes. See docs/api.md.

Tests

cargo test

About

Experimental JS runtime with ability to run 1k+ threads

Resources

Stars

5 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages