Skip to content

Add isolated context mode: CPython in a child OS process - #72

Merged
benoitc merged 1 commit into
mainfrom
isolated-mode
Aug 29, 2026
Merged

Add isolated context mode: CPython in a child OS process#72
benoitc merged 1 commit into
mainfrom
isolated-mode

Conversation

@benoitc

@benoitc benoitc commented Aug 29, 2026

Copy link
Copy Markdown
Owner

Adds mode => isolated: the Python interpreter of a context runs in a child OS process. worker and owngil are unchanged.

The goal is a mode where Python code cannot take the node down or run forever, without giving up the C extension ecosystem. The embedded interpreter cannot offer that: an interrupt only lands at the next bytecode, memory cannot be capped soundly, and a segfault in any extension kills the VM. A child process gets all of it from the OS: a signal lands wherever the child is, SIGKILL is the backstop, rlimits and cgroups bound it, and a crash costs one child, which is restarted within a budget.

The child is driven over a Unix socket with the framing of the existing callback pipe, and needs no native code on the Python side, only a small ETF codec with the NIF's type mapping. The public API is the same; where the child cannot match an embedded behaviour it fails loudly instead of diverging. The context process is a gen_statem that serves one caller at a time and lets callbacks re-enter, and a timeout cancels only its own request. On macOS the child enforces the memory limit itself, since the kernel ignores RLIMIT_AS; the cgroup option is refused outside Linux. Listen sockets are handed to children over the control socket, so the worker loop shape works out of process.

The cost is about 10 us per call over worker mode, one process per context, and payloads copied through the socket. Shared memory through erlang-iommap is left for a follow-up.

Validated on macOS and FreeBSD. Guide: docs/isolated.md. Version 4.2.0.

@benoitc
benoitc force-pushed the isolated-mode branch 5 times, most recently from 6c42d5a to a2108b8 Compare August 29, 2026 09:09
A mode where Python code cannot take the node down or run forever:
one child process per context over a Unix socket, signals with
SIGKILL as backstop, rlimits and cgroups, restart on crash. Same
public API; the context process is a gen_statem serving one caller
at a time. Validated on macOS and FreeBSD. Version 4.2.0.
@benoitc
benoitc merged commit 2c759dd into main Aug 29, 2026
39 of 40 checks passed
@benoitc
benoitc deleted the isolated-mode branch August 29, 2026 09:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant