fix(backgroundjobs): resolve status race condition and subprocess pipe hang#3833
fix(backgroundjobs): resolve status race condition and subprocess pipe hang#3833Piyush0049 wants to merge 1 commit into
Conversation
|
Hi @Piyush0049 Please provide more details about the issue you are trying to solve. Is it something which happens often? What are the symptoms? Right now the PR doesn't provide enough context to understand if you are fixing a bug or if these are just code reviews by an LLM which could easily hallucinate bugs. Thanks |
|
Hi @aheritier,
While reproducing this, I noticed two separate issues: 1.
|
Summary
This pull request resolves a critical status race condition in background jobs and eliminates process hangs caused by inherited I/O pipes from background subprocesses. It also ensures cross-platform test reliability on Windows and silences platform-specific linter false-positives.
Root Cause & Changes
1. Atomic Status Transitions (
CompareAndSwap)monitorJobused blind status writes (job.status.Store(...)). IfStopBackgroundJoborToolSet.Stopterminated a job at the exact moment a process was exiting naturally,monitorJobcould overwritestatusStoppedwithstatusCompletedorstatusFailed. This caused erroneous recall steering messages to be sent back to the AI model for manually aborted jobs.monitorJobwith atomic Compare-And-Swap (CAS) transitions (job.status.CompareAndSwap(statusRunning, newStatus)). Once a job transitions out ofstatusRunning, subsequent background monitoring completions cannot overwrite the terminated state.2. Piped Subprocess Cleanup (
cmd.WaitDelay)stdout/stderrfile descriptors (such as backgrounded scripts or daemons), Go'scmd.Wait()would block indefinitely waiting for anEOFon the pipes even after the primary process terminated.cmd.WaitDelay = 1 * time.Secondon all executed background jobs to automatically force-close orphaned I/O pipes if child processes hold them open after the primary process exits.3. Cross-Platform Compatibility & Linting
TestResolveWorkDirinbackgroundjobs_test.goto usefilepath.FromSlashand conditional OS root prefixes (C:\...on Windows) to prevent false-positive path resolution failures on Windows.exec_windows.goto useexec.CommandContextand wrapped secondary fallback errors with%w. Added,nolintlintto cross-platform silencing directives (//nolint:gosecinsnapshot.goand//nolint:bodycloseintransport_test.go) so that runninggolangci-linton Windows does not flag them as unused.