13.2 What State Belongs to an Interpreter
An interpreter is a collection of runtime state. To isolate interpreters from each other, I must inventory what state exists and decide where each piece lives.
The three-level state model from Multi-Interpreter Architecture shows the answer: process-global, per-interpreter, or per-thread. This page turns that into a reusable checklist.
13.2.1 The inventory problem
GNU R holds approximately 1,574 mutable global variables. Retrofitting isolation means sorting them. Some truly must stay process-global. Most can move to per-interpreter structs.
The census found 1,574 mutable globals in upstream R. All 35 per-thread globals now live in R_ThreadState, and R_InterpreterState had 556 members after patch 0127 (2026-08-22). The retrofit is incomplete and this is the working state.
13.2.2 The state categories
Process-global state
These must stay global. No interpreter owns them.
Thread pool and scheduling. If the runtime keeps a pool of worker threads, the pool and its queue of pending work belong to the process: built once, used by every interpreter. A global lock around it is cheap next to the work it schedules.
Memory allocator state. The malloc or jemalloc allocator metadata. When threads allocate concurrently from the C heap, they coordinate through the allocator’s internal locks and per-thread caches.
Platform constants. The operating system version, CPU count, pagesize. These never change. Read-only shared state needs no synchronization.
Signal handlers. Registered handlers for SIGINT, SIGTERM, and other signals. A handler is installed for the whole process. A process-directed signal such as SIGINT runs it on whichever thread the kernel picks among those not blocking the signal, so the handler must be async-signal-safe and must not assume which interpreter it interrupted.
Every process-global value must be immutable, read-only, or protected by a global lock. Unprotected mutable process-global state creates data races.
Per-interpreter state
Everything that makes one interpreter unique lives here.
Symbol table. The names the interpreter knows. Symbols are interned (each distinct name exists once). When a package loads, it adds symbols to this table. Two interpreters load the same package independently and get two separate symbol tables. Name lookup does not collide.
Namespace and environment hierarchy. The base environment, the global environment, and the search path. ls() returns names from the global environment of the current interpreter. <- assigns in that interpreter’s environment.
Heap and garbage collection state. All allocated SEXPs belong to one interpreter. The heap is per-interpreter in both r-in-rust and threaded-r; the only objects threaded-r interpreters share are the immortal ones: the builtin constants, R_EmptyEnv and 38 symbols the C API exports.
GC bookkeeping. Mark bits, age information, free lists, and generation state. An interpreter that owns its heap owns this state too.
Options. getOption("digits") returns the interpreter’s digits setting. options(digits = 5) changes it locally. Options are never shared.
Random number generator seed and stream state. Each interpreter has its own RNG stream. set.seed(42) in apartment A does not affect apartment B’s stream. The stream position is per-interpreter.
Connections and file table. Open files, sockets, pipes. Apartment A can write to file1 while apartment B reads from file2. In the full design, connection numbers are allocated per interpreter; the connection table example below shows where threaded-r stands.
Graphics devices. The active plotting device. In the full design each interpreter tracks its own device. threaded-r has done this for file devices such as pdf(), and interactive devices stay with the main interpreter because they need a process-wide event loop.
Condition handler stack. The tryCatch() or withCallingHandlers() stack for error handling. threaded-r keeps it with the thread, in R_ThreadState, and each interpreter has exactly one thread, so it behaves as per-interpreter state. When apartment A’s code throws an error, it walks apartment A’s stack, not B’s.
Interrupt flag. A request to stop computation. Stopping apartment A’s work leaves apartment B running. In threaded-r, Ctrl+C itself reaches only the main interpreter; a worker is stopped through a cancel request in its own interpreter state.
Per-thread state
These belong to the thread running the interpreter.
Current interpreter pointer. The interpreter this thread is executing, kept in thread-local storage and set when the thread creates its interpreter.
Call stack and frame pointers. The active C function frames. Each thread has its own stack. Per-interpreter state can be reached through the thread’s current interpreter pointer.
Error state and exception flag. Where an error should jump to is kept in the thread’s error flag and jump buffer, so this thread can longjmp out of nested calls.
C stack frame base and limit. The bottom and top of this thread’s stack. Stack overflow detection reads this.
Protect stack. A temporary root list for GC. C code calls PROTECT() to anchor SEXPs so the collector does not free them. The protect stack is per-thread because each thread protects its own SEXPs.
Call context stack. The R-level call contexts (formal arguments, error handlers, on.exit() callbacks). Per-thread because the stack itself is per-thread.
Every global in R has to land at one of three levels. Process-global state is shared and must be safe to share, per-interpreter state belongs to one interpreter, and per-thread state belongs to the thread running it.
13.2.3 Decision grid
For each global variable in upstream R, ask these questions in order:
Does every interpreter need independent copies, or can all interpreters see the same value?
If independent copies needed, it is per-interpreter. If all see the same value and it never changes, it is process-global. If all see the same value and it changes, it needs synchronization.
Does this state depend on the current thread?
If yes, it is per-thread. Examples: call stack, error flag, protect stack.
Can this state outlive a single computation?
If yes, it is per-interpreter or process-global. If no, it can be stack-local in the function that uses it.
Does this state coordinate across interpreters?
If yes, it stays process-global and protected by a lock. Examples: thread pool, malloc.
This grid sorts almost every variable. The hard cases are native package globals, which the packages and namespaces page addresses.
13.2.4 Practical example: RNG state
GNU R keeps its RNG state in C globals spread over src/main/RNG.c and src/nmath/snorm.c: the generator kinds, the table of generators, the Mersenne-Twister state array and its position, and caches for the normal generator. An interpreter needs an independent RNG stream, so every one of these is per-interpreter.
R also mirrors the state into the global environment as .Random.seed, a SEXP vector that lives in the interpreter’s heap. Mersenne-Twister is a large generator: after set.seed(42), .Random.seed has length 626, which is 624 state words plus the generator kind and the current index. The C-side state sits in the interpreter’s R_InterpreterState struct. When this interpreter runs set.seed(42), only this interpreter’s stream resets.
13.2.5 Practical example: Connection table
GNU R tracks open files and sockets in one process-global table (Connections in src/main/connections.c). Slots 0 to 2 hold stdin, stdout and stderr, so the first connection a session opens is number 3, and every connection number is unique across the process.
In the full apartment model this is per-interpreter. Each interpreter maintains its own connection table, and numbers are unique only within that interpreter, so two interpreters can both have a connection number 3. The cost is that C code in a package cannot pass a connection number to C code in another interpreter, which is acceptable because a connection belongs to one R session.
threaded-r has not split the table yet. It keeps GNU R’s process-wide table, guards the code that claims and frees slots with a lock, and records which interpreter owns each slot.
13.2.6 Practical example: Options and getOption()
GNU R stores options as a pairlist bound to the symbol .Options in the base environment. options() and getOption() read and replace that binding (src/main/options.c). In threaded-r, each interpreter has its own.
Interpreter A calls options(digits = 5). This affects only interpreter A. Interpreter B’s getOption("digits") still returns the default.
13.2.7 Consequences
State placement determines synchronization cost. Process-global state needs a lock or must never change. Per-interpreter state needs no lock, because one thread owns it. Per-thread state lives in thread-local storage, which no other thread contends for.
Further reading
- threaded-r, my fork of GNU R, whose source audit sorted this state into the three levels. Its repository is not public yet.
- R Internals <https://cran.r-project.org/doc/manuals/r-release/R-ints.html>, R Core Team.
v8::Isolateclass reference <https://v8.github.io/api/head/classv8_1_1Isolate.html>, V8 API documentation.- Lua 5.4 Reference Manual,
lua_newstate<https://www.lua.org/manual/5.4/manual.html#lua_newstate>.
Where this appears
- threaded-r The core challenge of multi-interpreter design: deciding which globals become per-interpreter fields and which stay shared.
- r-in-rust (rrs) A from-scratch design enforces these boundaries by making interpreter ownership explicit in the type system.