{"record":{"id":"a81f633f2aeb24f8","repo":"nautechsystems/nautilus_trader","slug":"spawn","errorCode":null,"errorMessage":"spawn","messagePattern":"spawn","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/event_store/src/writer/mod.rs","lineNumber":1131,"sourceCode":"        let appends_seen = Arc::new(AtomicUsize::new(0));\n        let backend = BlockingBackend::new(\n            Arc::clone(&inner),\n            Arc::clone(&gate),\n            Arc::clone(&appends_seen),\n        );\n\n        let halt_threshold = Duration::from_millis(50);\n        let config = WriterConfig {\n            channel_capacity: 1,\n            max_batch_entries: 1,\n            max_batch_latency: Duration::from_millis(1),\n            halt_threshold,\n        };\n\n        let clock = get_atomic_clock_static();\n        let boxed = Box::new(backend);\n\n        let writer = EventStoreWriter::spawn(boxed, clock, halt, config).expect(\"spawn\");\n\n        // First submit fits in the channel; the writer thread takes it and blocks.\n        writer.submit(entry_draft(10)).expect(\"first submit fits\");\n\n        // Wait long enough for the writer to dequeue and become blocked at the gate.\n        std::thread::sleep(Duration::from_millis(20));\n\n        // Second and third submits saturate the slot; the channel buffer holds one,\n        // the next one stalls past the halt threshold.\n        let _ = writer.submit(entry_draft(11));\n        let stalled = writer.submit(entry_draft(12)).expect_err(\"must stall\");\n\n        match stalled {\n            SubmitError::HaltSignaled { .. } => {}\n            SubmitError::Closed => panic!(\"expected HaltSignaled, was Closed\"),\n        }\n        let captured_reasons = captured.lock();\n        assert_eq!(","sourceCodeStart":1113,"sourceCodeEnd":1149,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/event_store/src/writer/mod.rs#L1113-L1149","documentation":"EventStoreWriter::spawn starts the background writer thread that drains the submit channel and commits batches to the backend. The expect at writer/mod.rs:1131 (test submit_signals_halt_when_stalled_past_threshold) panics if spawn fails. Spawn errors when the run/backend is not in an open, writable state, so the thread could not be started safely.","triggerScenarios":"Calling EventStoreWriter::spawn(boxed, clock, halt, config) with a backend whose run was never opened, a backend that fails initialization (manifest/high_watermark calls error), or invalid WriterConfig values.","commonSituations":"Constructing a writer for halt-threshold tests with channel_capacity=1 and a gated backend before opening the run; passing a failing backend (e.g. manifest() -> Err(Backend(\"disk failure\"))) to spawn; reusing a backend whose run is already sealed.","solutions":["Call backend.open_run(manifest(...)) before EventStoreWriter::spawn and check its Result","Ensure the backend's manifest()/high_watermark() return Ok during initialization","Confirm the run status is Open, not already Sealed/Ended","Validate WriterConfig (channel_capacity, thresholds) before spawn"],"exampleFix":"// before\nlet writer = EventStoreWriter::spawn(boxed, clock, halt, config).expect(\"spawn\");\n// after\nlet writer = EventStoreWriter::spawn(boxed, clock, halt, config)\n    .unwrap_or_else(|e| panic!(\"writer spawn failed: {e:?}\"));","handlingStrategy":"validation","validationCode":"let m = backend.manifest().map_err(|e| format!(\"backend init query failed: {e}\"))?;\nassert_eq!(m.status, RunStatus::Open, \"spawn requires an open run\");","typeGuard":"fn can_spawn_writer(m: &RunManifest) -> bool {\n    m.status == RunStatus::Open\n}","tryCatchPattern":"match EventStoreWriter::spawn(boxed, clock, halt, config) {\n    Ok(writer) => { /* use writer */ }\n    Err(e) => eprintln!(\"spawn failed: {e:?}\"),\n}","preventionTips":["Open the run before spawning","Ensure backend manifest()/high_watermark() succeed at init","Validate WriterConfig before spawn"],"tags":["rust","panic","event-store","test-setup"],"backgroundTag":"invalid-state-transition","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}