{"record":{"id":"a040db40df3c541e","repo":"xai-org/grok-build","slug":"cancelled-after-git-worktree-add","errorCode":null,"errorMessage":"cancelled after git worktree add","messagePattern":"cancelled after git worktree add","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"crates/codegen/xai-fast-worktree/src/worktree/execute.rs","lineNumber":953,"sourceCode":"\n    let dest_str = dest.to_string_lossy().to_string();\n\n    // Get modified files from source (for dirty state preservation or clean modes).\n    // This runs in parallel with worktree creation conceptually, but since we're sync now,\n    // we run it after worktree add for simplicity (the worktree add is typically fast).\n    git::worktree_add_no_checkout(&source, &dest_str, &git_ref)?;\n    tracing::debug!(elapsed = ?start.elapsed(), \"git worktree add --no-checkout complete\");\n\n    // `git worktree add` created dest + its `.git/worktrees/<name>` registration.\n    // From here, any early return (cancel or a hard error in copy/finalize/\n    // bad-ref) must reclaim both so a later pinned-dest fast path can't adopt a\n    // partial tree. No background threads touch dest in this path, so a drop\n    // guard is sufficient.\n    let guard = PartialWorktreeGuard::new(&dest);\n\n    // Check cancellation after git worktree add (before the expensive copy).\n    if cancellation_token.is_cancelled() {\n        anyhow::bail!(\"cancelled after git worktree add\");\n    }\n\n    // Get modified files\n    let modified_result = git::get_modified_files(&source_root)?;\n    let modified_files_in_source = Arc::new(modified_result.paths);\n    let dirty_files_report = Some(modified_result.report);\n\n    // For CleanTracked/CleanAll: we need to skip modified files during copy\n    let modified_files_for_skip = match &working_tree {\n        WorkingTreeMode::PreserveWorkingTree => None,\n        WorkingTreeMode::CleanTracked | WorkingTreeMode::CleanAll => {\n            Some(Arc::clone(&modified_files_in_source))\n        }\n    };\n\n    // Phase 2: Parallel CoW copy of unignored files.\n    // IMPORTANT: Copy from source_root (git root), not source (which might be a subdirectory).\n    let copy_start = std::time::Instant::now();","sourceCodeStart":935,"sourceCodeEnd":971,"githubUrl":"https://github.com/xai-org/grok-build/blob/bc7f02eddd3d84085849dc19ed216f11c23b0571/crates/codegen/xai-fast-worktree/src/worktree/execute.rs#L935-L971","documentation":"execute_copy_worktree installs a PartialWorktreeGuard and then checks the cancellation token immediately after `git worktree add` but before the expensive parallel file copy. If cancellation was requested during the git phase, it bails with this message; the drop guard then reclaims the partial destination tree so no half-built worktree is left behind.","triggerScenarios":"A shutdown/timeout/abort triggered the CancellationToken while `git worktree add` was still running (or between its completion and the copy start); the caller dropped or cancelled the future driving execute_create_worktree_dispatch's copy path.","commonSituations":"User hits Ctrl-C or a supervisor times out a long create; bulk job orchestrator cancels pending worktree creations; request deadline expires just as creation starts.","solutions":["This is cooperative cancellation working as designed — re-issue the create request if the worktree is still needed (destination was reclaimed).","Avoid cancelling during create; wait for completion or reserve a longer deadline.","If cancellation was accidental, ensure the token is not shared/triggered by an unrelated shutdown path.","Check the destination path is gone (guard reclaimed it) before retrying to avoid 'already exists' errors."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"match create_worktree(&plan).await {\n    Err(e) if e.to_string().contains(\"cancelled after git worktree add\") => {\n        tracing::info!(\"creation cancelled; dest reclaimed, safe to retry\");\n        // retry only if cancellation was not user-intentional\n    }\n    other => other,\n}","preventionTips":["Don't cancel tokens during short git phases; cancel at phase boundaries","Budget deadlines above expected `git worktree add` latency","Confirm dest removal before retrying (guard reclaims it automatically)","Distinguish user-cancel vs supervisor-timeout tokens"],"tags":["cancellation","worktree","async"],"backgroundTag":"operation-cancelled","analyzedSha":"bc7f02eddd3d84085849dc19ed216f11c23b0571","analyzedAt":"2026-08-31T04:59:42.031Z","schemaVersion":2},"datasetVersion":"2026-08-31T09:17:48.483Z"}