{"record":{"id":"094e01870a21af19","repo":"t8y2/dbx","slug":"a-cancellation-token-is-always-available","errorCode":null,"errorMessage":"a cancellation token is always available","messagePattern":"a cancellation token is always available","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/dbx-core/src/agent_service.rs","lineNumber":1137,"sourceCode":"        }\n        None => &[],\n    };\n    let _driver_guard = lock_or_cancel(&driver_lock, command_tokens).await?;\n    // Use the command-scoped token when one was registered before any awaitable\n    // setup (blocker check, lock wait, registry fetch) so a cancel fired during\n    // that window is observed here instead of being lost. Otherwise register a\n    // token owned by this call keyed by a fresh operation id so two concurrent\n    // installs of the same driver cannot replace each other's token.\n    let owned_operation_id: Option<String> =\n        if cancellation.is_some() { None } else { Some(uuid::Uuid::new_v4().to_string()) };\n    let owned_cancellation: Option<Arc<AgentInstallCancellation>> = match owned_operation_id.as_deref() {\n        Some(operation_id) => Some(am.begin_install_cancellation(&install_cancellation_key(operation_id)).await),\n        None => None,\n    };\n    let active_cancellation: &AgentInstallCancellation = owned_cancellation\n        .as_deref()\n        .or_else(|| cancellation.map(|token| token.as_ref()))\n        .expect(\"a cancellation token is always available\");\n    if active_cancellation.is_cancelled() {\n        if let (Some(operation_id), Some(token)) = (owned_operation_id.as_deref(), owned_cancellation.as_ref()) {\n            am.finish_install_cancellation(&install_cancellation_key(operation_id), token).await;\n        }\n        return Err(AGENT_DOWNLOAD_CANCELED_ERROR.to_string());\n    }\n\n    let result = install_agent_driver_with_batch_unlocked(\n        am,\n        db_type,\n        source,\n        progress,\n        current,\n        total_drivers,\n        &[active_cancellation],\n    )\n    .await;\n","sourceCodeStart":1119,"sourceCodeEnd":1155,"githubUrl":"https://github.com/t8y2/dbx/blob/c0390bff16418b651f4728520d99adf8ce48829a/crates/dbx-core/src/agent_service.rs#L1119-L1155","documentation":"In the single-driver install path, the code builds active_cancellation from either an owned token (created from operation_id via begin_install_cancellation) or a caller-supplied cancellation token. The expect(\"a cancellation token is always available\") enforces the invariant that at least one of operation_id/cancellation is present; if both are None the program panics instead of proceeding.","triggerScenarios":"Invoking the install path with neither an operation id nor a borrowed cancellation token — e.g. a new caller passes None for both, or refactoring changed the fallback order so cancellation is consumed before the expect.","commonSituations":"Call sites added after the invariant was introduced that forget to thread a cancellation token; internal callers that previously always created an operation id start passing None; test harnesses constructing the context manually with all-None fields.","solutions":["Guarantee every caller supplies an operation id or a cancellation token before entering the install path.","Create a token when missing: call begin_install_cancellation with a fresh operation id instead of expecting.","Convert the expect into an explicit error return if the path can legitimately run without cancellation.","Add an integration test covering the None/None input combination."],"exampleFix":"// before\nlet active_cancellation = owned_cancellation.as_deref()\n    .or_else(|| cancellation.map(|t| t.as_ref()))\n    .expect(\"a cancellation token is always available\");\n// after\nlet Some(active_cancellation) = owned_cancellation.as_deref()\n    .or_else(|| cancellation.map(|t| t.as_ref())) else {\n    return Err(\"no cancellation token provided\".to_string());\n};","handlingStrategy":"validation","validationCode":"// validate inputs before the single-driver install path\nassert!(operation_id.is_some() || cancellation.is_some(),\n    \"install requires an operation id or a cancellation token\");","typeGuard":"fn has_install_cancellation(op_id: Option<&OperationId>, token: Option<&AgentInstallCancellation>) -> bool {\n    op_id.is_some() || token.is_some()\n}","tryCatchPattern":"let active = owned_cancellation.as_deref()\n    .or_else(|| cancellation.map(|t| t.as_ref()))\n    .ok_or_else(|| \"no cancellation token provided for install\".to_string())?;","preventionTips":["Thread a cancellation token through every install call site.","Generate an operation id (and thus a token) when a caller has none.","Replace expect with ok_or/else returning an error for optional token resolution.","Cover all-None inputs with a regression test."],"tags":["rust","panic","invariant","cancellation","agent-install"],"backgroundTag":"missing-cancellation-token-invariant","analyzedSha":"c0390bff16418b651f4728520d99adf8ce48829a","analyzedAt":"2026-09-05T23:05:10.900Z","contentChangedAt":"2026-09-05T23:05:10.900Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}