{"record":{"id":"4fbc6ecc798c2e1c","repo":"t8y2/dbx","slug":"driver-token-registered","errorCode":null,"errorMessage":"driver token registered","messagePattern":"driver token registered","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/dbx-core/src/agent_service.rs","lineNumber":829,"sourceCode":"    }\n\n    // Register a per-driver token for every driver in the batch, keyed by the\n    // batch operation id so per-driver cancels target this batch's driver even\n    // when the same db_type is being installed concurrently elsewhere. The\n    // batch token lets one click abort the whole upgrade; each driver token\n    // lets the user cancel a single driver while the rest continue.\n    let mut driver_cancellations = std::collections::HashMap::new();\n    for db_type in &updatable {\n        let key = batch_driver_cancellation_key(effective_operation_id, db_type);\n        let token = am.begin_install_cancellation(&key).await;\n        driver_cancellations.insert(db_type.clone(), token);\n    }\n\n    // Run independent driver installs concurrently, with a fixed upper bound\n    // so a large registry cannot saturate download and file-system resources.\n    let installs = updatable.into_iter().enumerate().map(|(index, db_type)| {\n        let key = batch_driver_cancellation_key(effective_operation_id, &db_type);\n        let token = driver_cancellations.remove(&db_type).expect(\"driver token registered\");\n        let batch_token = Arc::clone(&active_batch_arc);\n        async move {\n            let result = if batch_token.is_cancelled() {\n                Err(AGENT_DOWNLOAD_CANCELED_ERROR.to_string())\n            } else {\n                install_agent_driver_from_registry_locked(\n                    am,\n                    registry,\n                    source,\n                    &db_type,\n                    progress,\n                    Some((index + 1) as u32),\n                    Some(total),\n                    // Observe BOTH the row token (per-driver cancel) and the\n                    // batch token (cancel-all): a batch cancel must interrupt a\n                    // driver whose download already started, not just one that\n                    // is still waiting to begin.\n                    &[&token, &batch_token],","sourceCodeStart":811,"sourceCodeEnd":847,"githubUrl":"https://github.com/t8y2/dbx/blob/c0390bff16418b651f4728520d99adf8ce48829a/crates/dbx-core/src/agent_service.rs#L811-L847","documentation":"Before spawning per-driver installs, upgrade_all_agent_drivers_with_registry pre-registers one cancellation token per updatable driver in driver_cancellations. The .expect(\"driver token registered\") asserts each driver being iterated has a token in that map; a missing entry means the pre-registration and the install loop got out of sync, so it panics.","triggerScenarios":"The set of drivers enumerated in the install loop (updatable.into_iter()) differs from the set for which driver_cancellations was populated earlier in the same function — e.g. tokens registered before a filter/re-check of updatable drivers, or remove() called twice for the same db_type across a retry within the same run.","commonSituations":"Concurrent modification of the updatable list between token registration and the install map; a code change inserts or reorders filtering after token registration; batch tests exercise a path where the registration loop was skipped.","solutions":["Register driver cancellation tokens from the exact same iterator (same snapshot) that feeds the install loop.","If a token is genuinely missing, fall back to creating one: driver_cancellations.entry(db_type).or_insert_with(...) or begin_install_cancellation with the per-driver key.","Use entry().and_expect-style removal only after asserting key presence, or restructure to carry tokens alongside drivers in one collection.","Add a test that every db_type in updatable has a token before the loop."],"exampleFix":"// before\nlet token = driver_cancellations.remove(&db_type).expect(\"driver token registered\");\n// after\nlet token = match driver_cancellations.remove(&db_type) {\n    Some(t) => t,\n    None => am.begin_install_cancellation(&batch_driver_cancellation_key(effective_operation_id, &db_type)).await,\n};","handlingStrategy":"validation","validationCode":"// before the install loop, verify registration coverage\nfor db_type in &updatable {\n    assert!(driver_cancellations.contains_key(db_type), \"missing token for {db_type}\");\n}","typeGuard":"fn tokens_cover<'a>(tokens: &HashMap<DbType, AgentInstallCancellation>, drivers: impl IntoIterator<Item = &'a DbType>) -> bool {\n    drivers.into_iter().all(|d| tokens.contains_key(d))\n}","tryCatchPattern":"let token = match driver_cancellations.remove(&db_type) {\n    Some(t) => t,\n    None => return Err(format!(\"internal error: no cancellation token registered for {db_type}\")),\n};","preventionTips":["Register tokens from the same snapshot of `updatable` used by the install loop.","Carry each driver's token alongside the driver in one struct so they cannot diverge.","Avoid calling remove() twice for the same db_type in one run.","Test the batch path end-to-end with multiple drivers."],"tags":["rust","panic","invariant","map-lookup","cancellation"],"backgroundTag":"missing-map-entry-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"}