13.4 Packages and Namespaces Across Interpreters
This is the hardest unsolved part of the apartment model. On 2026-10-21, 5,569 of the 25,474 packages on CRAN needed compilation (the NeedsCompilation field in tools::CRAN_package_db()), and that code was written for a process with one interpreter in it. The trouble starts when two interpreters load the same package. threaded-r has a working default for that case, described below, and no general answer yet.
13.4.1 The problem stated
A native package that keeps process-global state can look like this:
static int global_flag = 0;
static double* workspace = NULL;
void init_package() {
global_flag = 1;
workspace = malloc(1000 * sizeof(double));
}
SEXP query_flag() {
return ScalarInteger(global_flag);
}The pattern is common in real packages: memory allocated at load time, cached symbols and attribute lookups, registered callbacks. Matrix and mgcv, for example, both had C statics that threaded-r had to move into per-interpreter state before it could declare them capable.
In single-interpreter R, this works. The package loads once per process, and its statics initialize once.
In multi-interpreter R, ambiguity arises. Interpreter-1 calls library(pkg). The package’s init_package() runs. global_flag = 1. Interpreter-2 then calls library(pkg).
Three options exist:
Option A: One shared copy. Both interpreters link to the same compiled .so file. The same global_flag exists. When interpreter-1 calls query_flag(), it returns 1. When interpreter-2 calls it, the same static variable, also 1.
Advantage: memory efficient. One copy of the code.
Disadvantage: not thread-safe. If interpreter-1’s code mutates workspace while interpreter-2 reads it, behavior is undefined.
Option B: Two independent copies. Each interpreter gets its own copy of the .so. Two instances of global_flag, two workspaces. Interpreter-1’s package sees 1. Interpreter-2’s package sees 0 or a separate 1, if init_package() runs again.
Advantage: thread-safe, as far as the package’s own statics go. Each interpreter’s package is isolated.
Disadvantage: a plain dlopen cannot do it, as the section on r-in-rust below explains. Each copy needs its own link namespace or its own file, carries its own data segment, and registers its routines again.
Option C: Explicit isolation. The package author writes code aware of multiple interpreters. The package keeps its state per interpreter, either in R-level environments or in C storage tied to the interpreter (the route threaded-r’s patches for Matrix and mgcv take), and creates that state each time an interpreter loads it.
Advantage: memory efficient, thread-safe.
Disadvantage: requires auditing thousands of packages.
Which option should hold for packages the project has not audited? threaded-r’s default is a fourth: option A, with each shared object confined to the first interpreter that loads it, and option C for any package that declares itself capable. Whether CRAN packages can be brought to option C at scale is unsolved.
13.4.2 The meaning of “loading”
Before answering the question, clarify what loading does.
In GNU R, library(pkg) does several things, in this order (loadNamespace in src/library/base/R/namespace.R):
- Load the package’s imports, then its R code from the installed lazy-load database
- Load each shared object the NAMESPACE names in
useDynLibwithdyn.load(), which callsdlopen()and then runs the package’sR_init_<pkg>(), where it registers its native routines - Call the package’s
.onLoad()hook if it exists - Attach the package to the search path
The question is about step 2: whether the shared object is loaded once per process or once per interpreter.
In threaded-r, the first interpreter to dyn.load() a shared object claims it for the life of the process. Any other interpreter that tries to load the same path is refused, with an error naming the interpreter that holds it. A package lifts the restriction by calling R_RegisterThreadedCapability once its C state is safe for several interpreters. Every base and recommended package now has a recorded status; tcltk is left undeclared on purpose, because it needs a process-wide event loop.
13.4.3 Symbol registration and the ABI
Packages export C functions through the R C API. A package registers symbols like this:
static R_NativePrimitiveArgType my_func_types[2] = {INTSXP, REALSXP};
static const R_CMethodDef cMethods[] = {
{"my_func", (DL_FUNC) &my_func, 2, my_func_types},
{NULL, NULL, 0, NULL}
};
void R_init_mypackage(DllInfo *dll) {
R_registerRoutines(dll, cMethods, NULL, NULL, NULL);
R_useDynamicSymbols(dll, FALSE);
}When the package loads, R_init_mypackage() runs. It calls R_registerRoutines(). This adds the function to a registry.
In multi-interpreter R, which registry? Interpreter-local or process-global?
If interpreter-local, each interpreter has its own copy of the registered functions. Calling .Call("my_func", ...) looks up the registry in the current interpreter. This requires no sharing.
If process-global, both interpreters see the same registered functions. Either way, the functions themselves are C code in the .so, and that code assumes process-global state, which leads back to option A or B.
Per-interpreter symbol registration would isolate the R-side bookkeeping: each interpreter keeps its own table of the routines it has loaded. It would not isolate the package’s C statics, because the routines still run the same code against the same globals.
13.4.4 Native library globals and thread safety
Some packages link native libraries. A package for linear algebra can link BLAS or LAPACK. These libraries often have globals.
OpenBLAS has a thread count setting. If interpreter-1 sets it to 4 and interpreter-2 sets it to 8, which wins? The last call wins. They interfere.
This is a different problem from package statics. It is library-level state that the package does not control.
How must native packages coordinate access to library globals? Some options: use thread-local storage, wrap all library calls in locks, or accept that only one interpreter can use the library at a time.
Threads exacerbate this. If interpreter-1 and interpreter-2 run on different threads and both call BLAS, the library sees simultaneous calls. Depending on the library’s thread safety, this can work or crash.
13.4.5 Prior implementations
Five earlier R implementations met compiled packages as a compatibility problem: Renjin, FastR, Rho, pqR and Riposte. They met it in different ways, and two of them shipped real systems.
Renjin, an R interpreter for the JVM, compiles a package’s C, C++ and Fortran to JVM bytecode with its own GCC-Bridge toolchain, and maps calls into GNU R’s C API onto Renjin’s Java classes. Its announcement of GCC-Bridge gives the problem this page is about as a reason not to load native binaries directly: “the widespread use of global variables in package native code” would “prevent users from running multiple, concurrent Renjin sessions in the same JVM process.”
FastR, built on GraalVM’s Truffle framework, runs a package’s compiled code natively, and its README warns that code calling the C API in hot paths can be slow “due to the high cost of transitions between the native and managed code.” An LLVM backend, offered as a preview, runs the package’s code on GraalVM’s LLVM runtime instead.
Rho started from GNU R’s own sources with the goal of refactoring the interpreter into an efficient VM, with an optional LLVM JIT. Its README now says development “has been suspended indefinitely.”
pqR, Radford Neal’s “pretty quick” R, is based on R 2.15.0 and does some numerical computations in parallel helper threads, while keeping GNU R’s one interpreter per process.
Riposte was a research VM for vector code in R (Talbot, DeVito and Hanrahan, 2012), built to study tracing compilation and parallel execution of vector operations.
They split two ways. Renjin and FastR reimplemented R on the JVM and had to answer the C API from outside, by compiling package code to a new target or by bridging to native code. Rho and pqR started from GNU R’s own sources and kept its C API as it was.
My own r-in-rust shows the boundary can be crossed from the reimplementation side. It reimplements R from scratch and still loads compiled CRAN packages, because it presents R’s function-accessor headers instead of trying to emulate a struct layout. In its census of 2026-08-18, 28 of the 30 most downloaded compiled packages installed and all 28 loaded (r-in-rust names the two that failed).
13.4.6 r-in-rust’s approach
r-in-rust isolates everything it owns from the start: each interpreter has its own heap, namespaces and package environments. Compiled code is a different matter. Its loader (crates/rrs-host/src/dynload.rs) maps a package’s shared object with an ordinary dlopen, so a second interpreter that loads the same package gets the same mapped library and the same C statics.
The tempting answer is to load the shared object once per interpreter and let each get its own statics. That is not what happens: a plain dlopen() of an object that is already loaded returns the same handle and the same statics, because the loader recognizes the object and reference-counts it. Separate copies need dlmopen and its own link namespace, or literally a second file on disk. Neither is free, glibc supports at most 16 link namespaces, and both break anything that assumes one copy of the library per process.
Calling dlopen twice on the same path returns the same mapped library and the same statics. Only dlmopen gives each interpreter its own copy.
So for compiled code, r-in-rust is where threaded-r was before it added confinement: R-level state is isolated by construction, and a package’s own C state is shared by every interpreter that loads it. Giving each interpreter its own statics would cost a link namespace or a file copy per interpreter. Even then, a crashing package would still take down the whole process, because every interpreter lives in it.
13.4.7 threaded-r’s requirements
threaded-r has committed to native package compatibility, and it has made the first decision. Shared objects are loaded once per process (option A) and confined to the interpreter that loads them first, until the package declares itself capable. What remains:
- Audit packages beyond base and recommended. Each one whose C state is unsafe needs that state moved into per-interpreter storage, as Matrix and mgcv did.
- Keep the tools that tell a package author where a package stands:
parallel:::threadCapabilityStatus()and the fork’s own guide to writing threaded R extensions. - Decide what to offer packages that will never be audited, which today stay confined to one interpreter.
Confinement could be relaxed for a package that is never declared capable. Instead of refusing a second interpreter, threaded-r could load the package again in a separate link namespace with dlmopen, so each interpreter gets its own statics. The 16-namespace limit in glibc caps how far that goes.
13.4.8 The view from R code
From the R programmer’s perspective, the question is mostly invisible. R code is isolated. Interpreter-1’s global environment does not see interpreter-2’s variables. Package namespaces are separate.
The issue is at the C level. R code that calls .Call() or .External() to invoke C functions relies on that boundary being thread-safe. If the C code crashes, the whole process goes down, with every interpreter in it.
Isolation at the R level does not guarantee isolation at the C level.
13.4.9 Consequences
Much of R’s useful code lives in compiled packages, so a fork that cannot run them in its workers has little to offer. Confinement turns the failure into a refused load with an error message that names the cause, instead of a crash in a second interpreter.
Further reading
- Writing R Extensions, “Registering native routines” <https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Registering-native-routines>, R Core Team.
- R Internals <https://cran.r-project.org/doc/manuals/r-release/R-ints.html>, R Core Team.
dlopen(3)<https://man7.org/linux/man-pages/man3/dlopen.3.html>, Linux man page, on reference counting of loaded objects and ondlmopennamespaces.- Alex Bertram, “Introducing GCC-Bridge: A C/Fortran compiler targeting the JVM” <https://www.renjin.org/blog/2016-01-31-introducing-gcc-bridge.html>, Renjin blog, 2016. Why Renjin compiles package C and Fortran to JVM bytecode.
- FastR <https://github.com/oracle/fastr>, the GraalVM implementation of R, including its notes on native code.
- Rho <https://github.com/rho-devel/rho>, the suspended project to refactor GNU R into an efficient VM.
- Justin Talbot, Zachary DeVito and Pat Hanrahan, “Riposte: A Trace-Driven Compiler and Parallel VM for Vector Code in R” <https://doi.org/10.1145/2370816.2370825>, PACT 2012.
- PEP 684, “A Per-Interpreter GIL” <https://peps.python.org/pep-0684/>, Eric Snow. How CPython handles the same problem for C extensions.
Where this appears
- threaded-r The hardest open question: native packages assume process-global state, so each shared object stays confined to one interpreter unless its package declares itself capable.
- r-in-rust (rrs) Each interpreter has independent R-level package state from the start; compiled code is loaded once per process, as in GNU R.