@iso4/sandbox 0.10.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -27,6 +27,13 @@ images such as Alpine (`node:*-alpine`) are **not supported** — the binary
27
27
  won't start there; use a `node:*-slim` image instead ([#186](https://github.com/schplitt/iso4/issues/186)
28
28
  tracks musl support).
29
29
 
30
+ On Linux the V8 child sandboxes itself with Landlock and a seccomp syscall
31
+ filter, so it needs Linux 5.13 or newer with `landlock` in the kernel's LSM
32
+ list, and, in a container, a seccomp profile that allows the `landlock_*` and
33
+ `seccomp` calls. Where Landlock or seccomp is unavailable the child
34
+ refuses to start and says why on stderr. `ISO4_NO_OS_SANDBOX=1` in the host's
35
+ environment turns this OS sandbox off; use it only as an emergency switch.
36
+
30
37
  ## Quick start
31
38
 
32
39
  ```ts
@@ -232,8 +239,8 @@ Residency is bounded by memory, not by a count:
232
239
  ```ts
233
240
  const sandbox = await createSandbox({
234
241
  memoryMb: 128, // default memory cap per isolate, JS heap + buffers (override per prefix at prepare())
235
- memoryBudgetMb: 2048, // RSS mark for the whole runtime process (0 = off)
236
- hostReserveMb: 128, // container memory held back for this host process
242
+ memoryBudgetMb: 2048, // RSS mark for the whole runtime process (> 0)
243
+ hostReserveMb: 128, // container memory held back for this host process (what Node uses)
237
244
  })
238
245
  ```
239
246
 
@@ -268,9 +275,9 @@ back to 80 % of the mark. `maxConcurrentRuns` caps concurrent runs; this caps
268
275
  memory.
269
276
  The default is derived from the memory available to the process
270
277
  (container-aware), so most hosts never set it: 80 % of the container limit
271
- minus `hostReserveMb`, which is headroom for this host process to grow into
272
- (its current usage is already metered) — raise it when the host caches
273
- heavily, `0` to hand the sandbox the whole limit.
278
+ minus `hostReserveMb`, the memory this host process uses (Node itself is not
279
+ metered) — set it to the host's peak, raise it when the host caches heavily,
280
+ `0` to hand the sandbox the whole limit.
274
281
 
275
282
  Above the budget sits the admission line, and a run that needs a new isolate
276
283
  whose heap ceiling would cross it fails with `ERR_CAPACITY_MEMORY` rather than
@@ -278,13 +285,17 @@ risking the container. That refusal is honest, not a bug to route around:
278
285
  nothing ran, the telemetry is zero, and a retry a moment later usually
279
286
  succeeds. The runtime does not evict warm instances to squeeze the refused
280
287
  run in — eviction hands the freed pages to V8's pool, which the _next_
281
- isolate draws from, so the measured usage the line compares against does not
282
- move for seconds either way. Instead the budget mark sheds idle warmth
288
+ isolate draws from, so the process's measured RSS the line compares against
289
+ does not move for seconds either way. Instead the budget mark sheds idle warmth
283
290
  gradually while the line holds, and V8 returns what it no longer needs to the
284
291
  operating system on its own. If refusals are routine rather than occasional,
285
292
  the container is genuinely too small for `maxConcurrentRuns × memoryMb` —
286
293
  `createSandbox` warns at startup when those two contradict each other.
287
294
 
295
+ CPU has a similar refusal. A run that needs a new thread fails with
296
+ `ERR_CAPACITY_CPU` when 1,024 runs are already waiting for a CPU. Nothing ran,
297
+ so retry it later. A run that reuses or joins a warm instance is never refused.
298
+
288
299
  `sandbox.stats()` reports the live picture — active runs, queue depth, warm and
289
300
  idle instance counts, summed idle memory, `budgetBytes` / `rssBytes`, whether the
290
301
  runtime is currently `underPressure`, and per-prefix counts. It answers on a
@@ -295,11 +306,12 @@ dedicated connection, so it works even when every run slot is busy.
295
306
  How many runs execute at once is the runtime's decision, not a constant. It
296
307
  derives the number from the shape of the runs it is serving —
297
308
  `5 × cores × wall^0.7 ÷ cpu^1.2` over the uncontended minimum wall and CPU
298
- time of each prefix, recomputed every 32 completions — so a prefix that
299
- waits 200 ms on an upstream call is granted thousands of slots while one
300
- that burns a core for 3 ms is granted a handful. Callers beyond the current
301
- number queue FIFO, `stats()` reports it as `slotLimit`, and a run that had
302
- to wait carries `queueWaitMs` on its result.
309
+ time of each prefix, recomputed after completions 1, 2, 4, 8 and 16, then
310
+ every 32 — so a prefix that waits 200 ms on an upstream call is granted
311
+ thousands of slots while one that burns a core for 3 ms is granted a
312
+ handful. Callers beyond the current number queue FIFO, `stats()` reports it
313
+ as `slotLimit`, and a run that had to wait carries `queueWaitMs` on its
314
+ result.
303
315
 
304
316
  ```ts
305
317
  const sandbox = await createSandbox({
@@ -417,9 +429,9 @@ array, so an oversized batch turns a per-event cost into a per-run timeout.
417
429
  ## Result shape
418
430
 
419
431
  ```ts
420
- type RunResult
421
- = | { ok: true, exports: SandboxExports, skippedExports: string[], stdout: string[], stderr: string[], durationMs: number, cpuTimeMs: number, bridgeCalls: BridgeCallEntry[] }
422
- | { ok: false, error: RunError, stdout: string[], stderr: string[], durationMs: number, cpuTimeMs: number, bridgeCalls: BridgeCallEntry[] }
432
+ type RunResult =
433
+ | { ok: true, exports: SandboxExports, skippedExports: string[], stdout: string[], stderr: string[], durationMs: number, cpuTimeMs: number, bridgeCalls: BridgeCallEntry[] }
434
+ | { ok: false, error: RunError, stdout: string[], stderr: string[], durationMs: number, cpuTimeMs: number, bridgeCalls: BridgeCallEntry[] }
423
435
 
424
436
  // prefix.call() / run({ code, call }) resolve to a CallResult instead:
425
437
  // the success arm carries `value` (the function's return value) in place of