{"record":{"id":"e21933f787dd98f9","repo":"xai-org/grok-build","slug":"spawn-mermaid-render-thread","errorCode":null,"errorMessage":"spawn mermaid-render thread","messagePattern":"spawn mermaid-render thread","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/codegen/xai-grok-pager/src/app/mermaid_worker.rs","lineNumber":245,"sourceCode":"                        if writes_since_sweep >= SWEEP_EVERY_N_WRITES {\n                            writes_since_sweep = 0;\n                            if let Some(dir) = job.out_path.parent() {\n                                // Off the draw path (worker thread); keeps the session's mermaid/ dir bounded within a session\n                                sweep_session_cache(dir, SESSION_DISK_CAP_BYTES);\n                            }\n                        }\n                    }\n                    let result = MermaidResult {\n                        key: job.key,\n                        outcome,\n                    };\n                    if result_tx.send(result).is_err() {\n                        return; // Receiver dropped; the view is gone.\n                    }\n                }\n            }\n        })\n        .expect(\"spawn mermaid-render thread\");\n\n    (job_tx, result_rx)\n}\n\n/// Coalesce `first` plus every job already queued on `rx` into a per-[`MermaidCacheKey`] map where the latest job for each key wins.\n/// `IndexMap` keeps FIFO order across distinct keys, so independent diagrams still render in order.\nfn drain_coalesced(\n    first: MermaidJob,\n    rx: &Receiver<MermaidJob>,\n) -> IndexMap<MermaidCacheKey, MermaidJob> {\n    let mut pending: IndexMap<MermaidCacheKey, MermaidJob> = IndexMap::new();\n    pending.insert(first.key.clone(), first);\n    while let Ok(job) = rx.try_recv() {\n        pending.insert(job.key.clone(), job);\n    }\n    pending\n}\n","sourceCodeStart":227,"sourceCodeEnd":263,"githubUrl":"https://github.com/xai-org/grok-build/blob/bc7f02eddd3d84085849dc19ed216f11c23b0571/crates/codegen/xai-grok-pager/src/app/mermaid_worker.rs#L227-L263","documentation":"`spawn_worker` spawns the dedicated mermaid-render thread and panics with 'spawn mermaid-render thread' if `std::thread::Builder::spawn` fails. The worker thread renders mermaid diagrams to PNG off the UI thread, so without it rendering cannot proceed at all.","triggerScenarios":"`std::thread::Builder::spawn` returning Err — the OS refused to create the thread, almost always due to resource limits.","commonSituations":"Hitting the process/thread limit (ulimit -u, cgroup pids.max); insufficient stack memory to reserve the worker stack under memory pressure; running inside a restricted sandbox/container with thread creation blocked.","solutions":["Check and raise thread limits: `ulimit -u`, container `pids.max` cgroup setting.","Free memory or reduce the configured worker stack size so allocation succeeds.","Check for thread leaks elsewhere in the process (join or terminate long-lived workers).","Propagate the spawn error to the caller and degrade gracefully (render inline or report mermaid unavailable)."],"exampleFix":"// before\n.expect(\"spawn mermaid-render thread\");\n// after\n.map_err(|e| MermaidError::WorkerSpawn(e.to_string()))?;","handlingStrategy":"try-catch","validationCode":"// preflight thread-creation capability\nmatch std::thread::Builder::new().stack_size(64 * 1024).spawn(|| {}) {\n    Ok(h) => { h.join().ok(); }\n    Err(e) => eprintln!(\"thread limits exhausted: {e}\"),\n}","typeGuard":null,"tryCatchPattern":"match thread::Builder::new().name(\"mermaid-render\").spawn(worker_loop) {\n    Ok(handle) => handle,\n    Err(e) => return Err(MermaidError::WorkerSpawn(e.to_string())),\n}","preventionTips":["Raise ulimit -u / container pids.max in deployment configs","Watch for thread leaks; join or reap finished worker threads","Keep worker stack size modest to survive memory-constrained hosts"],"tags":["rust","threading","panic","mermaid","resource-limits"],"backgroundTag":"thread-spawn-failed","analyzedSha":"bc7f02eddd3d84085849dc19ed216f11c23b0571","analyzedAt":"2026-08-31T04:59:42.031Z","schemaVersion":2},"datasetVersion":"2026-08-31T09:17:48.483Z"}