{"record":{"id":"87c18993ecc0f96c","repo":"nautechsystems/nautilus_trader","slug":"execution-algorithm-cleanup-actor-id-not-found-w","errorCode":null,"errorMessage":"Execution algorithm {cleanup_actor_id} not found while cleaning subscriptions","messagePattern":"Execution algorithm (.+?) not found while cleaning subscriptions","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/system/src/trader.rs","lineNumber":791,"sourceCode":"                    .filter(|order| {\n                        !order.is_closed() && order.exec_algorithm_id() == Some(exec_algorithm_id)\n                    })\n                    .map(|order| order.strategy_id())\n                    .collect::<Vec<_>>()\n            };\n            strategy_ids.sort_unstable();\n            strategy_ids.dedup();\n\n            for strategy_id in strategy_ids {\n                algo.subscribe_to_strategy_events(strategy_id);\n            }\n\n            Ok(())\n        });\n        let cleanup_actor_id = actor_id;\n        let cleanup_fn: ExecutionAlgorithmSubscriptionFn = Box::new(move || {\n            let Some(mut algo) = try_get_actor_unchecked::<T>(&cleanup_actor_id) else {\n                anyhow::bail!(\n                    \"Execution algorithm {cleanup_actor_id} not found while cleaning subscriptions\"\n                );\n            };\n            algo.unsubscribe_all_strategy_events();\n            Ok(())\n        });\n        let endpoint: Ustr = format!(\"{exec_algorithm_id}.execute\").into();\n        let handler = ShareableMessageHandler::from_typed(move |command: &TradingCommand| {\n            if let Some(mut algo) = try_get_actor_unchecked::<T>(&actor_id) {\n                if let Err(e) = algo.execute(command.clone()) {\n                    log::error!(\"Error executing command on algorithm {actor_id}: {e}\");\n                }\n            } else {\n                log::error!(\"Execution algorithm {actor_id} not found in registry\");\n            }\n        });\n        msgbus::register_any(endpoint.into(), handler);\n","sourceCodeStart":773,"sourceCodeEnd":809,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/system/src/trader.rs#L773-L809","documentation":"The cleanup closure registered by add_exec_algorithm unsubscribes all strategy events for the algorithm, but first resolves the algorithm via try_get_actor_unchecked by actor id. If the actor cannot be found at cleanup time it fails with this message. This keeps subscription teardown symmetric with registration.","triggerScenarios":"cleanup_fn executes (e.g. after a start failure rolling back restored subscriptions, or on stop) while try_get_actor_unchecked::<T>(&cleanup_actor_id) returns None because the algorithm was disposed, removed, or never registered under that id.","commonSituations":"A start failure path cleaning up restored subscriptions after the algorithm itself was already disposed; mismatched component_id vs registered actor id; manual actor removal from the registry while the trader still holds the cleanup hook.","solutions":["Keep the exec algorithm registered until after the trader's stop/cleanup completes.","Verify the algorithm's component_id matches the id used when add_exec_algorithm registered the hooks.","If the algorithm is intentionally gone, remove/re-register it via add_exec_algorithm so hooks reference a live actor."],"exampleFix":"// before\ntrader.remove_actor(&algo_id); // removed too early\ntrader.stop()?; // cleanup_fn can't find actor\n\n// after\ntrader.stop()?; // cleanup runs while actor is live\ntrader.remove_actor(&algo_id);","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if let Err(e) = trader.stop() {\n    if e.to_string().contains(\"not found while cleaning subscriptions\") {\n        log::warn(\"exec algorithm already removed; cleanup skipped\");\n    } else { return Err(e); }\n}","preventionTips":["Stop the trader before removing exec algorithms so cleanup hooks run with live actors","Do not manually remove actors the trader manages","Align algorithm component_id with the id used at registration"],"tags":["rust","actor-lookup","exec-algorithm","cleanup"],"backgroundTag":"entity-not-found","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"}