{"record":{"id":"bceef6b6d29c2cf8","repo":"nautechsystems/nautilus_trader","slug":"failed-to-create-tokio-runtime","errorCode":null,"errorMessage":"Failed to create tokio runtime","messagePattern":"Failed to create tokio runtime","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"crates/common/src/live/runtime.rs","lineNumber":88,"sourceCode":"    {\n        crate::python::runtime::initialize_python();\n    }\n\n    let worker_threads = std::env::var(NAUTILUS_WORKER_THREADS)\n        .ok()\n        .and_then(|val| val.parse::<usize>().ok())\n        .unwrap_or_default();\n\n    let mut builder = Builder::new_multi_thread();\n\n    if worker_threads > 0 {\n        builder.worker_threads(worker_threads);\n    }\n\n    builder\n        .enable_all()\n        .build()\n        .expect(\"Failed to create tokio runtime\")\n}\n\n/// Sets a custom pre-built Tokio runtime as the global Nautilus runtime.\n///\n/// Must be called before the first [`get_runtime`] invocation (i.e. before\n/// `LiveNode::build()` or any adapter/client usage). This gives callers who\n/// own `main()` full control over worker threads, blocking threads, thread\n/// names, stack sizes, and any other [`tokio::runtime::Builder`] options.\n///\n/// # Runtime Requirements\n///\n/// The supplied runtime must be multi-threaded and have all Tokio drivers\n/// enabled with `tokio::runtime::Builder::enable_all()`.\n///\n/// # Errors\n///\n/// Returns `Err(runtime)` if a runtime was already initialized.\npub fn set_runtime(runtime: tokio::runtime::Runtime) -> Result<(), tokio::runtime::Runtime> {","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/common/src/live/runtime.rs#L70-L106","documentation":"`initialize_runtime` builds the global tokio runtime via `Builder::enable_all().build()` and panics if tokio fails to construct it. Tokio's `build` fails only in degenerate conditions such as an invalid worker_threads/core configuration or resource/OS-level failure creating the runtime's I/O and timer drivers.","triggerScenarios":"Calling `initialize_runtime` (directly or via `get_runtime` first-use) when tokio cannot build a multi-thread runtime, e.g. worker_threads set to 0 or invalid, or environment/resource constraints preventing driver setup.","commonSituations":"Passing an invalid `worker_threads` value (0) from config; embedding Nautilus in a process that forbids creating reactor/timer drivers; conflicting prior runtime setup.","solutions":["Ensure `worker_threads` is at least 1 (or omit it to use num_cpus default)","Check config/env that feeds worker_threads for bad values","If a runtime must already exist, use `set_runtime` with a pre-built runtime before `get_runtime`","Verify the target platform supports tokio's enabled drivers (enable_all) and the process isn't resource-starved"],"exampleFix":"// before\ninitialize_runtime(0);\n// after\ninitialize_runtime(4); // >= 1 worker thread","handlingStrategy":"validation","validationCode":"let workers = worker_threads.max(1);\ninitialize_runtime(workers);","typeGuard":"fn valid_worker_threads(n: usize) -> bool { n >= 1 }","tryCatchPattern":"// Panics cannot be caught in Rust without catch_unwind; validate inputs beforehand\nstd::panic::catch_unwind(|| initialize_runtime(4)).is_err() // last resort only","preventionTips":["Never pass 0 for worker_threads","Prefer the default runtime (omit worker_threads) unless tuning is required","Use set_runtime with a pre-built runtime when you need custom configuration","Call initialize_runtime once, early, before any get_runtime usage"],"tags":["rust","panic","tokio","runtime","initialization"],"backgroundTag":"module-init-failed","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}