{"record":{"id":"b17100820379643e","repo":"rust-embedded/rust-raspberrypi-OS-tutorials","slug":"transition-to-single-core-main-called-while-stat","errorCode":null,"errorMessage":"transition_to_single_core_main() called while state != Init","messagePattern":"transition_to_single_core_main\\(\\) called while state != Init","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"20_timer_callbacks/kernel/src/state.rs","lineNumber":89,"sourceCode":"\n    /// Return if the kernel is init state.\n    pub fn is_init(&self) -> bool {\n        self.state() == State::Init\n    }\n\n    /// Transition from Init to SingleCoreMain.\n    pub fn transition_to_single_core_main(&self) {\n        if self\n            .0\n            .compare_exchange(\n                Self::INIT,\n                Self::SINGLE_CORE_MAIN,\n                Ordering::Acquire,\n                Ordering::Relaxed,\n            )\n            .is_err()\n        {\n            panic!(\"transition_to_single_core_main() called while state != Init\");\n        }\n    }\n}\n","sourceCodeStart":71,"sourceCodeEnd":93,"githubUrl":"https://github.com/rust-embedded/rust-raspberrypi-OS-tutorials/blob/644474cc09f755249f9c55d99a5d1e07a2562fc7/20_timer_callbacks/kernel/src/state.rs#L71-L93","documentation":"transition_to_single_core_main() performs an atomic compare_exchange from INIT to SINGLE_CORE_MAIN; if the exchange fails the current state was not INIT, so the kernel panics. This enforces a one-way, exactly-once state transition: the kernel may enter single-core-main mode only once, from the init state. Calling it twice, or after another transition, triggers this panic.","triggerScenarios":"Calling transition_to_single_core_main() twice (e.g. re-running boot logic, a second CPU entering the same path); calling it after transition_to_multi_core_main(); calling it on an already-initialized kernel in a restart/re-init attempt.","commonSituations":"Boot code executed on multiple cores racing to transition (only the INIT->SINGLE_CORE_MAIN winner succeeds); a test harness or reboot path re-invoking init routines; duplicate initialization of the kernel driver; state already advanced by a prior subsystem init.","solutions":["Call the transition exactly once, from a single designated boot path/core.","Guard the call: check `state() == State::Init` (or use is_init()) before attempting the transition, and skip if already transitioned.","If multiple cores race, elect one main core (core id check) before calling.","If re-init is genuinely needed, reset the state atom back to INIT first (only where safe/designed)."],"exampleFix":"// before\nkernel_state.transition_to_single_core_main();\n// after\nif kernel_state.is_init() {\n    kernel_state.transition_to_single_core_main();\n}","handlingStrategy":"validation","validationCode":"fn can_transition_to_single_core_main(current: State) -> bool {\n    current == State::Init\n}","typeGuard":"fn is_init_state(raw: u8) -> bool {\n    raw == INIT\n}","tryCatchPattern":"// The transition panics on failure; check first:\nif kernel_state.is_init() {\n    kernel_state.transition_to_single_core_main();\n} else {\n    log::warn!(\"skipping transition, already initialized\");\n}","preventionTips":["Call state transitions from exactly one boot path/core.","Use is_init() as a precondition check before any transition.","Elect a single main core before boot-time transitions.","Do not attempt re-initialization of an already-initialized kernel."],"tags":["rust","kernel","state-machine","initialization"],"backgroundTag":"invalid-state-transition","analyzedSha":"644474cc09f755249f9c55d99a5d1e07a2562fc7","analyzedAt":"2026-09-06T09:25:56.584Z","contentChangedAt":"2026-09-06T09:25:56.584Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}