{"record":{"id":"cd718b5b3a606a6b","repo":"nautechsystems/nautilus_trader","slug":"dataactor-must-be-registered-before-accessing-cloc","errorCode":null,"errorMessage":"DataActor must be registered before accessing clock","messagePattern":"DataActor must be registered before accessing clock","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/common/src/actor/data_actor.rs","lineNumber":206,"sourceCode":"            .unwrap_or_else(|| {\n                panic!(\n                    \"DataActor {} must be registered before calling `clock_mut()` - trader_id: {:?}\",\n                    core.actor_id, core.trader_id\n                )\n            })\n            .borrow_mut()\n    }\n\n    /// Returns a clone of the reference-counted clock.\n    ///\n    /// # Panics\n    ///\n    /// Panics if the actor has not yet been registered.\n    fn clock_rc(&self) -> Rc<RefCell<dyn Clock>> {\n        self.core()\n            .clock\n            .as_ref()\n            .expect(\"DataActor must be registered before accessing clock\")\n            .clone()\n    }\n\n    /// Returns a read-only cache borrow.\n    ///\n    /// # Panics\n    ///\n    /// Panics if the actor has not yet been registered.\n    fn cache_ref(&self) -> Ref<'_, Cache> {\n        self.core()\n            .cache\n            .as_ref()\n            .expect(\"DataActor must be registered before accessing cache\")\n            .borrow()\n    }\n\n    /// Returns a clone of the reference-counted cache.\n    ///","sourceCodeStart":188,"sourceCodeEnd":224,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/common/src/actor/data_actor.rs#L188-L224","documentation":"DataActor::clock_rc returns a clone of the actor's Rc<RefCell<dyn Clock>>, which is populated only during registration with a Trader/Node. If called before registration, the Option is None and this panic fires. It exists to give callers a cheap clock handle while enforcing that the actor is only used after being registered.","triggerScenarios":"Calling clock_rc() (directly or via any actor method that needs the clock, e.g. scheduling timers via request or emitting log timestamps) before the actor has been registered — e.g. in the actor's constructor, in new(), or before trader.add_actor / node registration completes.","commonSituations":"Starting timers or touching clock-dependent APIs in custom actor initialization instead of in on_start; registering the actor with a kernel but invoking handler code immediately in the same thread before registration finishes; tests constructing a DataActor standalone and calling clock-backed methods.","solutions":["Move clock/timer usage into on_start (or later lifecycle callbacks), which run only after registration","Register the actor (trader.add_actor / node registration) before calling any clock-dependent method","If a clock handle is needed pre-registration, pass one explicitly into the actor's constructor rather than using clock_rc()"],"exampleFix":"// before\nimpl DataActor for MyActor {\n    fn new(&mut self) { self.clock_rc(); } // panics: not registered yet\n}\n// after\nimpl DataActor for MyActor {\n    fn on_start(&mut self) {\n        self.clock_rc(); // safe: registered before on_start runs\n    }\n}","handlingStrategy":"validation","validationCode":"fn actor_is_registered(actor_is_registered_flag: bool) {\n    assert!(actor_is_registered_flag, \"register actor before using clock APIs\");\n}","typeGuard":null,"tryCatchPattern":"// Rust panics are not catchable in normal code; guard via lifecycle ordering:\nfn on_start(&mut self) {\n    // guaranteed registered here\n    let clock = self.clock_rc();\n}","preventionTips":["Use clock APIs only inside on_start and later lifecycle callbacks","Never touch clock_rc in constructors or before trader.add_actor","In tests, register actors through the standard harness before exercising handlers"],"tags":["rust","panic","actor","lifecycle","not-registered"],"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"}