{"record":{"id":"38cfb346839640e6","repo":"libnyanpasu/clash-nyanpasu","slug":"effect-function-failed-e","errorCode":null,"errorMessage":"effect function failed: {e:#?}","messagePattern":"effect function failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/nyanpasu-core/src/state/coordinator.rs","lineNumber":380,"sourceCode":"            },\n            None => effect_fn(new_state).await.map_err(WithEffectError::Effect),\n        };\n        match effect_result {\n            Ok(result) => {\n                if tx.commit().await.is_err() {\n                    let actual = self.sync_change_id_after_cas_mismatch();\n                    return Err(WithEffectError::State(\n                        StateChangedError::StateCasMismatch {\n                            expected: current_state.version,\n                            actual,\n                        },\n                    ));\n                }\n                self.mark_change_id_committed(next_changed_id);\n                Ok((result, report))\n            }\n            Err(e) => {\n                tx.rollback(RollbackReason::CoordinatorError(Arc::new(anyhow!(\n                    \"effect function failed: {e:#?}\"\n                ))))\n                .await;\n                Err(e)\n            }\n        }\n    }\n}\n\n// -- Builder --\n\npub struct StateCoordinatorBuilder<T: Clone + Send + Sync + 'static> {\n    notify_strategy: NotifyStrategy,\n    subscribers: IndexMap<SubscriberName<'static>, ArcStateSubscriber<T>>,\n}\n\nimpl<T: Clone + Send + Sync + 'static> Default for StateCoordinatorBuilder<T> {\n    fn default() -> Self {","sourceCodeStart":362,"sourceCodeEnd":398,"githubUrl":"https://github.com/libnyanpasu/clash-nyanpasu/blob/f7dbce2997c633e484f54788035e770b3ee99773/backend/nyanpasu-core/src/state/coordinator.rs#L362-L398","documentation":"In with_pending_state_inner, the effect function of a pending state transaction failed, so the coordinator rolls back the transaction (RollbackReason::CoordinatorError) and returns the original error. The message wraps the effect error for the rollback record.","triggerScenarios":"Calling with_pending_state / with_pending_state_timeout where the post-prepare effect_fn fails — e.g. runtime config generation, core restart, or IPC side effect returns Err.","commonSituations":"Core process unavailable during a config patch, filesystem errors writing generated config, or timeouts in downstream actor calls triggered from the effect.","solutions":["Read the wrapped inner error to find the actual failing effect step.","Retry the operation once the underlying cause (core down, disk error) is resolved; state was rolled back so it is safe to redo.","Move fragile side effects out of the effect closure or make them idempotent.","Use with_pending_state_timeout with a generous timeout if the effect is slow rather than failing."],"exampleFix":"// before\ncoordinator.with_pending_state(|tx| async {\n    restart_core().await?; // failure rolls everything back\n    Ok(())\n})\n// after\ncoordinator.with_pending_state(|tx| async {\n    if let Err(e) = restart_core().await {\n        log::error!(\"core restart failed, state kept: {e}\"); // report degraded, don't fail effect\n    }\n    Ok(())\n})","handlingStrategy":"retry","validationCode":"// pre-check downstream dependencies before running the effect\nasync fn can_run_effect() -> bool {\n    core_client.ping().await.is_ok() && config_dir_writable()\n}","typeGuard":null,"tryCatchPattern":"match coordinator.with_pending_state(effect_fn).await {\n    Err(e) => {\n        log::error!(\"effect failed, transaction rolled back: {e:#}\");\n        // state is consistent; fix cause and retry\n        retry_with_backoff(|| coordinator.with_pending_state(effect_fn)).await\n    }\n    Ok(v) => v,\n}","preventionTips":["Ensure the core/actors the effect calls are healthy before committing state","Avoid synchronous cross-actor cycles inside effects","Make effects idempotent and log the full error chain for diagnosis"],"tags":["state","transaction","rollback","effect"],"backgroundTag":"invalid-state-transition","analyzedSha":"f7dbce2997c633e484f54788035e770b3ee99773","analyzedAt":"2026-09-08T01:24:59.197Z","contentChangedAt":"2026-09-08T01:24:59.197Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}