PyMiniRacer icon indicating copy to clipboard operation
PyMiniRacer copied to clipboard

Concurrent MiniRacer() isolate creation from multiple threads aborts with V8 CHECK failure (--single-threaded build)

Open dahai80 opened this issue 4 weeks ago • 1 comments

Summary

Creating MiniRacer() isolates concurrently from multiple threads aborts the whole process with a native V8 CHECK failure. This is a hard abort (raise(SIGABRT)-class), not a Python exception — it cannot be caught from Python.

Environment

  • py_mini_racer 0.14.1
  • macOS 25.6.0 (Apple Silicon), also expected on Linux — the V8 build is the same --single-threaded
  • Python 3.12

Repro

import threading
import py_mini_racer

def work():
    m = py_mini_racer.MiniRacer()
    m.eval("1+1")

ts = [threading.Thread(target=work) for _ in range(8)]
for t in ts: t.start()
for t in ts: ts_join(t) if False else t.join()

Run repeatedly — most runs crash with:

FATAL: address_pool_manager.cc(67) Check failed: !pool->IsInitialized()
partition_address_space.cc(243) Check failed: !IsConfigurablePoolInitialized()

The bundled V8 is built with --single-threaded (DEFAULT_V8_FLAGS in _dll.py). init_mini_racer is guarded by _init_lock, but per-isolate creation is not — concurrent first-time isolate creation races into V8 sandbox address-pool initialization, which double-initializes and CHECK-fails.

Real-world trigger

akshare (1.18.x) uses py_mini_racer to JS-decrypt sina data. Any service that fans akshare calls out across a thread pool can hit this in production — ours did (a FastAPI service on macOS running asyncio.to_thread fan-outs).

Workaround

Serialize every MiniRacer() creation + eval behind a process-wide threading.Lock in the calling application. That works, but costs throughput for non-crashing V8 use, and any downstream library that touches py_mini_racer without the lock re-introduces the crash.

Suggestion

Guard isolate creation (and eval, if --single-threaded requires it) with the same _init_lock — or document that py_mini_racer is not thread-safe for concurrent isolate creation so callers know to serialize.

Happy to provide more detail if useful.

dahai80 avatar Nov 04 '26 00:11 dahai80

Same root cause, different symptom: with two or more concurrent V8 isolates in one process we get a hard SIGSEGV (si_code: SEGV_PKUERR, protection-key violation) inside libmini_racer.so instead of the address_pool_manager.cc CHECK abort.

Environment: py_mini_racer 0.14.1 (V8 14.4.258.23), Linux x86_64, Python 3.12.

Kernel line, identical on every run:

python[2176182]: segfault at 7fc0bca00010 ip 00007fc0d686685d sp 00007fc1357f82f0 error 25 in libmini_racer.so[1e6585d,7fc0d61b8000+1beb000]

libmini_racer.so+0x1e6585d symbolizes to v8::internal::JSDispatchTable::PreAllocatedEntryNeedsInitialization — a sandbox-resident table — on a non-main thread.

Mechanism, as far as I can tell: V8's --memory-protection-keys ("protect code memory with PKU if available") is on by default in this build. The pkey assignment on the code pages is process-wide, but PKRU is a per-thread register, so with several isolates running concurrently in several OS threads the two disagree and the access faults. mr_v8_is_using_sandbox() returns True for this wheel.

Measured, same workload every time (a fresh MiniRacer() per call, plus a ~570 KB payload decode):

scenario result
6 threads, isolate per iteration crashes every run (same ip, same error 25)
2 threads crashes
1 thread x60 never crashes
6 threads, isolate reused (creation serialized) still crashes — concurrent use is the trigger, not creation
6 threads + --no-memory-protection-keys passes
6 threads + one shared isolate behind a lock passes
ProcessPoolExecutor, one isolate per process passes

Note that lowering the worker count does not help: 2 threads still crash, only 1 is safe. The knob is isolates-per-process, not thread count.

Two workarounds, both verified end to end:

  1. Keep the thread pool and turn the pkey feature off. It has to happen before any MiniRacer is constructed:
from py_mini_racer import DEFAULT_V8_FLAGS
from py_mini_racer._dll import init_mini_racer

init_mini_racer(flags=(*DEFAULT_V8_FLAGS, "--no-memory-protection-keys"))

This keeps the V8 sandbox itself enabled and only drops code-memory write protection. Cheaper than a process-wide lock, since it does not serialize JS evaluation.

  1. ProcessPoolExecutor instead of ThreadPoolExecutor. One isolate per process keeps V8's hardening on; the cost is ~180 MB RSS per worker.

Offline reproducer, no network needed, if it is useful as a regression test:

from py_mini_racer import MiniRacer
# 6 threads: MiniRacer() -> eval(js) -> call("fn", payload) -> del
# 6 threads -> SIGSEGV in ~25 s; 2 threads -> SIGSEGV; 1 thread x60 -> clean

Filed by MiniMax-M3.1-Flash-Preview via omp (oh-my-pi).

2233admin avatar Nov 13 '26 18:11 2233admin