@iso4/sandbox 0.9.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,13 +27,20 @@ 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
33
40
  import { createSandbox } from '@iso4/sandbox'
34
41
  import { createSafeFetch } from '@iso4/fetch'
35
42
 
36
- const sandbox = await createSandbox({ memoryMb: 128 }) // per-isolate heap cap
43
+ const sandbox = await createSandbox({ memoryMb: 128 }) // per-isolate memory cap (JS heap + buffers)
37
44
 
38
45
  // Store host setup once; it is evaluated on each cold start
39
46
  const prefix = await sandbox.prepare({
@@ -52,7 +59,7 @@ const result = await prefix.execute({
52
59
  const res = await fetch(config.apiBase + '/users')
53
60
  export default { count: res.length }
54
61
  `,
55
- limits: { cpuTimeMs: 200, wallTimeMs: 5_000 }, // heap cap: prepare({ memoryMb }) or the sandbox default
62
+ limits: { cpuTimeMs: 200, wallTimeMs: 5_000 }, // memory cap: prepare({ memoryMb }) or the sandbox default
56
63
  })
57
64
 
58
65
  if (result.ok) {
@@ -231,30 +238,33 @@ Residency is bounded by memory, not by a count:
231
238
 
232
239
  ```ts
233
240
  const sandbox = await createSandbox({
234
- memoryMb: 128, // default heap cap per isolate (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
241
+ memoryMb: 128, // default memory cap per isolate, JS heap + buffers (override per prefix at prepare())
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
 
240
247
  `memoryMb` means two different things depending on whether the isolate is
241
248
  reused.
242
249
 
243
- **On a prefix**, the number is the retirement line: an instance whose heap sits
250
+ **On a prefix**, the number is the retirement line: an instance whose memory sits
244
251
  above it when two runs in a row finish takes no new runs and drops once the
245
252
  runs it is already serving complete. Nothing fails. The terminating line sits a
246
253
  headroom band above it (`round(sqrt(8 × memoryMb))` MB, so 128 → 160); crossing
247
254
  _that_ kills the run and fails its co-residents, and only a runaway allocation
248
255
  gets there. The band is what lets a grown instance be replaced instead of
249
- killing a run at the number — an optimization for reuse.
256
+ killing a run at the number — an optimization for reuse. Both lines count the
257
+ JS heap and buffer memory (ArrayBuffers, typed arrays, `resize()` growth, stream
258
+ chunks) together.
250
259
 
251
260
  ```ts
252
261
  await sandbox.prepare({ code, memoryMb: { soft: 96, hard: 256 } }) // exact lines
253
262
  await sandbox.prepare({ code, memoryMb: { hard: 128 } }) // terminate at 128, never retire
254
263
  ```
255
264
 
256
- **On a one-off `run()`**, the number is enforced exactly — `memoryMb: 128`
257
- dies at 128. A fresh isolate is never reused, so there is nothing to retire
265
+ **On a one-off `run()`**, the number is the terminating line — `memoryMb: 128`
266
+ ends the run at 128 (checked every ~10 ms and at every turn end, so it can
267
+ overshoot briefly). A fresh isolate is never reused, so there is nothing to retire
258
268
  and no band. It takes a plain number for the same reason: a soft line would
259
269
  be inert.
260
270
 
@@ -265,9 +275,9 @@ back to 80 % of the mark. `maxConcurrentRuns` caps concurrent runs; this caps
265
275
  memory.
266
276
  The default is derived from the memory available to the process
267
277
  (container-aware), so most hosts never set it: 80 % of the container limit
268
- minus `hostReserveMb`, which is headroom for this host process to grow into
269
- (its current usage is already metered) — raise it when the host caches
270
- 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.
271
281
 
272
282
  Above the budget sits the admission line, and a run that needs a new isolate
273
283
  whose heap ceiling would cross it fails with `ERR_CAPACITY_MEMORY` rather than
@@ -275,15 +285,19 @@ risking the container. That refusal is honest, not a bug to route around:
275
285
  nothing ran, the telemetry is zero, and a retry a moment later usually
276
286
  succeeds. The runtime does not evict warm instances to squeeze the refused
277
287
  run in — eviction hands the freed pages to V8's pool, which the _next_
278
- isolate draws from, so the measured usage the line compares against does not
279
- 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
280
290
  gradually while the line holds, and V8 returns what it no longer needs to the
281
291
  operating system on its own. If refusals are routine rather than occasional,
282
292
  the container is genuinely too small for `maxConcurrentRuns × memoryMb` —
283
293
  `createSandbox` warns at startup when those two contradict each other.
284
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
+
285
299
  `sandbox.stats()` reports the live picture — active runs, queue depth, warm and
286
- idle instance counts, summed idle heap, `budgetBytes` / `rssBytes`, whether the
300
+ idle instance counts, summed idle memory, `budgetBytes` / `rssBytes`, whether the
287
301
  runtime is currently `underPressure`, and per-prefix counts. It answers on a
288
302
  dedicated connection, so it works even when every run slot is busy.
289
303
 
@@ -292,11 +306,12 @@ dedicated connection, so it works even when every run slot is busy.
292
306
  How many runs execute at once is the runtime's decision, not a constant. It
293
307
  derives the number from the shape of the runs it is serving —
294
308
  `5 × cores × wall^0.7 ÷ cpu^1.2` over the uncontended minimum wall and CPU
295
- time of each prefix, recomputed every 32 completions — so a prefix that
296
- waits 200 ms on an upstream call is granted thousands of slots while one
297
- that burns a core for 3 ms is granted a handful. Callers beyond the current
298
- number queue FIFO, `stats()` reports it as `slotLimit`, and a run that had
299
- 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.
300
315
 
301
316
  ```ts
302
317
  const sandbox = await createSandbox({
@@ -414,9 +429,9 @@ array, so an oversized batch turns a per-event cost into a per-run timeout.
414
429
  ## Result shape
415
430
 
416
431
  ```ts
417
- type RunResult
418
- = | { ok: true, exports: SandboxExports, skippedExports: string[], stdout: string[], stderr: string[], durationMs: number, cpuTimeMs: number, bridgeCalls: BridgeCallEntry[] }
419
- | { 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[] }
420
435
 
421
436
  // prefix.call() / run({ code, call }) resolve to a CallResult instead:
422
437
  // the success arm carries `value` (the function's return value) in place of