{"record":{"id":"100a8ddc25e48c32","repo":"chenhg5/cc-connect","slug":"stop-scheduled-task-s-w","errorCode":null,"errorMessage":"stop scheduled task: %s (%w)","messagePattern":"stop scheduled task: (.+?) \\(%w\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"daemon/windows.go","lineNumber":243,"sourceCode":"}\n\nfunc stopWindowsTask() error {\n\tout, err := runPowerShell(fmt.Sprintf(`\n$task = Get-ScheduledTask -TaskName %s -ErrorAction SilentlyContinue\nif ($null -eq $task) { exit 0 }\nif ($task.State -eq 'Running') {\n\tStop-ScheduledTask -TaskName %s\n}\nfor ($i = 0; $i -lt 20; $i++) {\n\t$task = Get-ScheduledTask -TaskName %s -ErrorAction SilentlyContinue\n\tif ($null -eq $task -or $task.State -ne 'Running') { exit 0 }\n\tStart-Sleep -Milliseconds 500\n}\nWrite-Error 'scheduled task did not stop within timeout'\nexit 1\n`, powerShellLiteral(windowsTaskName), powerShellLiteral(windowsTaskName), powerShellLiteral(windowsTaskName)))\n\tif err != nil {\n\t\treturn fmt.Errorf(\"stop scheduled task: %s (%w)\", out, err)\n\t}\n\treturn nil\n}\n\nfunc startWindowsTask() error {\n\tout, err := runPowerShell(fmt.Sprintf(`\n$task = Get-ScheduledTask -TaskName %s -ErrorAction SilentlyContinue\nif ($null -eq $task) { Write-Error 'scheduled task not found'; exit 1 }\nif ($task.State -ne 'Running') { Start-ScheduledTask -TaskName %s }\n`, powerShellLiteral(windowsTaskName), powerShellLiteral(windowsTaskName)))\n\tif err != nil {\n\t\treturn fmt.Errorf(\"start scheduled task: %s (%w)\", out, err)\n\t}\n\treturn nil\n}\n\nfunc deleteWindowsTask() error {\n\tout, err := runPowerShell(fmt.Sprintf(`","sourceCodeStart":225,"sourceCodeEnd":261,"githubUrl":"https://github.com/chenhg5/cc-connect/blob/4000b2338aa6e850c99df54f8b0ed6ed7460b401/daemon/windows.go#L225-L261","documentation":"stopWindowsTask wraps an error from runPowerShell running its stop-wait-timeout script. The script Stop-ScheduledTask, polls for the task to stop, and calls Write-Error 'scheduled task did not stop within timeout'; exit 1 if it keeps running. The wrapped error thus usually means the task refused to stop within the timeout, or the PowerShell itself failed.","triggerScenarios":"Called by Install, Uninstall, Stop, Restart on Windows when Stop-ScheduledTask fails or the task remains non-stopped after the polling loop (500ms sleeps) expires, causing the script's `exit 1`.","commonSituations":"The daemon process spawned by the task hangs and ignores termination; the task is stuck in 'Stopping' state in Task Scheduler; waiting on a child process or network I/O; Task Scheduler service unresponsive.","solutions":["Inspect the %s output — 'scheduled task did not stop within timeout' means the process hung; kill the cc-connect process manually (Task Manager / Stop-Process).","Check task state: `Get-ScheduledTask -TaskName <name> | Select State` — if stuck in Stopping, end the underlying process.","Re-run the operation after the process exits; stopWindowsTask is retried by Install/Uninstall flows.","Investigate why the agent process hangs (e.g., an active session blocking shutdown)."],"exampleFix":"// manually force-stop the process behind the task\nGet-ScheduledTask -TaskName cc-connect | Select State\nStop-Process -Name cc-connect -Force\nStart-ScheduledTask -TaskName cc-connect   # or re-run install/uninstall","handlingStrategy":"retry","validationCode":"out, _ := exec.Command(\"powershell\", \"-NoProfile\", \"-Command\",\n\t\"(Get-ScheduledTask -TaskName '<name>').State\").Output()\nif strings.TrimSpace(string(out)) == \"Running\" {\n\t// task still running; ensure it can stop before Stop/Uninstall\n}","typeGuard":null,"tryCatchPattern":"if err := daemon.Stop(); err != nil {\n\tif strings.Contains(err.Error(), \"did not stop within timeout\") {\n\t\t// kill the hung process, then retry once\n\t\texec.Command(\"taskkill\", \"/IM\", \"cc-connect.exe\", \"/F\").Run()\n\t\ttime.Sleep(time.Second)\n\t\terr = daemon.Stop()\n\t}\n}","preventionTips":["Ensure the agent process handles termination signals promptly (no long blocking work in shutdown).","Kill hung processes via Task Manager before retrying stop/uninstall.","Watch for tasks stuck in 'Stopping' state in Task Scheduler."],"tags":["windows","scheduled-task","timeout","process-stop"],"backgroundTag":"request-timeout","analyzedSha":"4000b2338aa6e850c99df54f8b0ed6ed7460b401","analyzedAt":"2026-09-06T11:45:09.575Z","contentChangedAt":"2026-09-06T11:45:09.575Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}