Commit 4ebe1f8
fix: isolation when running multiple notebooks in an app server (marimo-team#8611)
**Summary.** This PR introduces process-level isolation when serving
multiple apps from the same server (`marimo run directory/`,
`create_asgi_app()`); clients of any given app are still run in threads
in the app's process for efficiency. This fixes a critical bug in which
different apps shared the same Python globals, leading to undefined
behavior such as collisions in `sys.modules. It does make multi-app
servers consume slightly more RAM.
Process isolation also applies to multi-app servers started with
`--sandbox`. (This PR also fixes a bug in which `--sandbox` run servers
silently dropped stdout/stderr.)
**Dependency on pyzmq.** The proposed implementation also adds a
dependency on `pyzmq` for multi-app servers, to pave the way for
allowing sandboxed multi-app servers . It would be possible to design a
different solution that used multiprocessing instead of `pyzmq`, at the
cost of not supporting package sandboxes. It is perhaps worth discussing
whether we are okay with making pyzmq a required dependency of marimo.
**Feature flag.** App isolation is feature-flagged, currently opt-in.
**Context.** When marimo was originally designed, `marimo run` only ever
served a single notebook. A single process could safely serve multiple
clients of the same notebook since they all share the same code.
When multiple app serving was introduced, we continued serving all
clients from a single process, even though the clients were potentially
running different programs. When two notebooks both `import utils` but
expect different implementations from different directories, whichever
app loads first wins, and the second app silently gets the wrong module.
Similar problems may exist for other Python globals.
**This PR.** This PR fixes the problem by running each app in its own OS
process. Multiple clients of the *same* app still share a process (as
kernel threads), which allows for fast and cheap sessions. The isolation
boundary is per-app, not per-client.
```
Before (shared process — sys.modules collisions):
┌──────────────── Main Process ─────────────────┐
│ │
│ Kernel(app1, client A) ← all kernels │
│ Kernel(app1, client B) share one │
│ Kernel(app2, client C) sys.modules │
│ Kernel(app2, client D) │
└───────────────────────────────────────────────┘
After (per-app process isolation):
┌──────────────── Main Process ─────────────────┐
│ (HTTP, WebSocket, routing) │
└──────────────────┬──────────────┬──────────────┘
│ ZMQ │ ZMQ
┌─────────────▼──┐ ┌──────▼──────────────┐
│ App Process │ │ App Process │
│ (app1.py) │ │ (app2.py) │
│ │ │ │
│ Kernel: cl. A │ │ Kernel: cl. C │
│ Kernel: cl. B │ │ Kernel: cl. D │
└─────────────────┘ └─────────────────────┘
isolated sys.modules isolated sys.modules
```
**IPC.** Each app process communicates with the main process over 4
shared ZeroMQ sockets (not per-client). Kernel commands and stream
output are multiplexed over these shared channels using session ID
tagging:
```
IPC channels (4 shared ZMQ sockets per app process):
Main Process App Subprocess
──────────── ──────────────
mgmt [PUSH] ─────────────────────▶ [PULL] mgmt loop
response [PULL] ◀───────────────────── [PUSH] (create/stop kernel)
cmd [PUSH] ──[sid, channel, msg]─▶ [PULL] dispatcher ──▶ kernel queues
stream [PULL] ◀──[sid, msg]───────── [PUSH] collector ◀── kernel output
```
```
Per session (main-process side):
AppProcessQueueManager
control_queue ──┐
ui_element_queue ──┤──▶ cmd socket (tagged with session_id)
completion_queue ──┤
input_queue ──┘
stream_queue ◀──── stream receiver thread (regular Queue)
```
**When this path is activated.**
- `create_asgi_app()`: always enables process isolation (the whole point
is multi-app)
- `marimo run app1.py app2.py` / directory serving: auto-enables when
multiple files detected
- `marimo run app.py` (single file): no change, uses existing
thread-based kernels
- `marimo edit`: no change, uses existing process-based kernels
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>1 parent 9e7f318 commit 4ebe1f8
31 files changed
Lines changed: 2930 additions & 28 deletions
File tree
- marimo
- _cli
- _config
- _ipc
- _server
- _session
- app_host
- managers
- _smoke_tests/process_isolation
- app1_modules
- app2_modules
- tests
- _session
- app_host
- managers
- snapshots
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1189 | 1189 | | |
1190 | 1190 | | |
1191 | 1191 | | |
| 1192 | + | |
| 1193 | + | |
| 1194 | + | |
| 1195 | + | |
| 1196 | + | |
| 1197 | + | |
| 1198 | + | |
| 1199 | + | |
1192 | 1200 | | |
1193 | 1201 | | |
1194 | 1202 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
568 | 568 | | |
569 | 569 | | |
570 | 570 | | |
| 571 | + | |
571 | 572 | | |
572 | 573 | | |
573 | 574 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
194 | 194 | | |
195 | 195 | | |
196 | 196 | | |
197 | | - | |
198 | | - | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
462 | 462 | | |
463 | 463 | | |
464 | 464 | | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
465 | 471 | | |
466 | 472 | | |
467 | 473 | | |
| |||
521 | 527 | | |
522 | 528 | | |
523 | 529 | | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
524 | 533 | | |
525 | 534 | | |
526 | 535 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| 35 | + | |
35 | 36 | | |
36 | 37 | | |
37 | 38 | | |
| |||
83 | 84 | | |
84 | 85 | | |
85 | 86 | | |
| 87 | + | |
86 | 88 | | |
87 | 89 | | |
88 | 90 | | |
| |||
97 | 99 | | |
98 | 100 | | |
99 | 101 | | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
100 | 111 | | |
101 | 112 | | |
102 | 113 | | |
| |||
222 | 233 | | |
223 | 234 | | |
224 | 235 | | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
225 | 241 | | |
226 | 242 | | |
227 | 243 | | |
| |||
393 | 409 | | |
394 | 410 | | |
395 | 411 | | |
| 412 | + | |
| 413 | + | |
396 | 414 | | |
397 | 415 | | |
398 | 416 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
248 | 248 | | |
249 | 249 | | |
250 | 250 | | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
251 | 256 | | |
252 | 257 | | |
253 | 258 | | |
| |||
262 | 267 | | |
263 | 268 | | |
264 | 269 | | |
| 270 | + | |
265 | 271 | | |
266 | 272 | | |
267 | 273 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
0 commit comments