{"record":{"id":"afcdbfa300676130","repo":"rayon-rs/rayon","slug":"the-global-thread-pool-has-not-been-initialized","errorCode":null,"errorMessage":"The global thread pool has not been initialized.","messagePattern":"The global thread pool has not been initialized\\.","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"rayon-core/src/registry.rs","lineNumber":169,"sourceCode":"// ////////////////////////////////////////////////////////////////////////\n// Initialization\n\nstatic mut THE_REGISTRY: Option<Arc<Registry>> = None;\nstatic THE_REGISTRY_SET: Once = Once::new();\n\n/// Starts the worker threads (if that has not already happened). If\n/// initialization has not already occurred, use the default\n/// configuration.\npub(super) fn global_registry() -> &'static Arc<Registry> {\n    set_global_registry(default_global_registry)\n        .or_else(|err| {\n            // SAFETY: we only create a shared reference to `THE_REGISTRY` after the `call_once`\n            // that initializes it, and there will be no more mutable accesses at all.\n            debug_assert!(THE_REGISTRY_SET.is_completed());\n            let the_registry = unsafe { &*ptr::addr_of!(THE_REGISTRY) };\n            the_registry.as_ref().ok_or(err)\n        })\n        .expect(\"The global thread pool has not been initialized.\")\n}\n\n/// Starts the worker threads (if that has not already happened) with\n/// the given builder.\npub(super) fn init_global_registry<S>(\n    builder: ThreadPoolBuilder<S>,\n) -> Result<&'static Arc<Registry>, ThreadPoolBuildError>\nwhere\n    S: ThreadSpawn,\n{\n    set_global_registry(|| Registry::new(builder))\n}\n\n/// Starts the worker threads (if that has not already happened)\n/// by creating a registry with the given callback.\nfn set_global_registry<F>(registry: F) -> Result<&'static Arc<Registry>, ThreadPoolBuildError>\nwhere\n    F: FnOnce() -> Result<Arc<Registry>, ThreadPoolBuildError>,","sourceCodeStart":151,"sourceCodeEnd":187,"githubUrl":"https://github.com/rayon-rs/rayon/blob/ee0a00bdb1ab039e178a215ad5712fb7fa58e58f/rayon-core/src/registry.rs#L151-L187","documentation":"Rayon operations that need the global thread pool (e.g. `current_num_threads`, `in_worker` contexts) panic when no global pool has been initialized and registry construction failed or was never triggered. The registry is created lazily via `call_once`; if it's absent/unavailable, this expect fires.","triggerScenarios":"Calling parallel code from a context where the global registry can't be built — notably inside a process where thread spawning fails, or when a previous `ThreadPoolBuilder::build_global()` error left THE_REGISTRY as None (e.g. unusable thread count = 0).","commonSituations":"Configuring `num_threads(0)` or hitting OS thread-creation limits in containers with low pid limits; running rayon in restricted environments (some WASM/embedded targets) where threads can't spawn.","solutions":["Check the Result of `ThreadPoolBuilder::new().num_threads(n).build_global()` at startup and log the error","Pass a positive thread count (>= 1) to the builder","Raise the container's process/thread limits (ulimit -u, cgroup pids.max)","Fall back to sequential iterators (`for` loops) on platforms without thread support"],"exampleFix":"// before\nrayon::spawn(task); // may panic if registry init failed earlier\n// after\nrayon::ThreadPoolBuilder::new().num_threads(4).build_global()\n    .expect(\"failed to init rayon global pool\");\nrayon::spawn(task);","handlingStrategy":"validation","validationCode":"fn init_rayon(threads: usize) -> Result<(), rayon::ThreadPoolBuildError> {\n    rayon::ThreadPoolBuilder::new().num_threads(threads.max(1)).build_global()\n}","typeGuard":null,"tryCatchPattern":"match rayon::ThreadPoolBuilder::new().build_global() {\n    Ok(()) => {},\n    Err(e) => log::error!(\"rayon init failed: {e}\"),\n}","preventionTips":["Always check build_global() result at startup","Use thread count >= 1","Raise pid/thread limits in containers","Provide sequential fallback on restricted platforms"],"tags":["rayon","panic","thread-pool","initialization"],"backgroundTag":"module-init-failed","analyzedSha":"ee0a00bdb1ab039e178a215ad5712fb7fa58e58f","analyzedAt":"2026-09-07T23:28:47.588Z","contentChangedAt":"2026-09-07T23:28:47.588Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}