[shell-operator] fix: track child PIDs to avoid zombie reaper race#886
Merged
Conversation
Signed-off-by: Ruslan Gorbunov <ruslan.gorbunov@flant.com>
4 tasks
Signed-off-by: Ruslan Gorbunov <ruslan.gorbunov@flant.com>
Signed-off-by: Ruslan Gorbunov <ruslan.gorbunov@flant.com>
Signed-off-by: Ruslan Gorbunov <ruslan.gorbunov@flant.com>
Signed-off-by: Ruslan Gorbunov <ruslan.gorbunov@flant.com>
Signed-off-by: Ruslan Gorbunov <ruslan.gorbunov@flant.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Adds executor-owned PID tracking so a PID-1 zombie reaper can avoid reaping processes that the executor will Wait() on, preventing “child exited already” / lost-exit-code races when running as PID 1.
Changes:
- Introduced a package-internal, thread-safe PID registry with a read-only
ProcessTrackerinterface (Tracker()). - Updated command execution paths (
Run,Output,RunAndLogLines) to useStart → registerPID → Wait → unregisterPIDto keep child PIDs tracked during their lifetime. - Added unit/integration-style tests around the registry and executor integration.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| pkg/executor/registry.go | Adds the PID registry + ProcessTracker interface and global tracker access. |
| pkg/executor/executor.go | Registers/unregisters child PIDs around Start/Wait for Run/Output/RunAndLogLines. |
| pkg/executor/executor_test.go | Adds registry tests and new executor/registry integration tests. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Signed-off-by: Ruslan Gorbunov <ruslan.gorbunov@flant.com>
ldmonster
approved these changes
May 25, 2026
…hld-handler-race-condition
ldmonster
reviewed
May 26, 2026
ldmonster
reviewed
May 26, 2026
Signed-off-by: Ruslan Gorbunov <ruslan.gorbunov@flant.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
Adds a process registry to the
executorpackage so that the PID-1 zombie reaper can distinguish between children it owns and children spawned by the executor (hooks, scripts). This prevents a race condition whereSIGCHLDhandling reaps a child thatcmd.Waitstill expects to collect, causing "signal: child exited already" errors and lost exit codes.What this PR does / why we need it
When
shell-operatorruns as PID 1 in a container, its zombie reaper loop callswaitpid(-1, ...)on everySIGCHLD. This can steal a child that was just started by the executor -cmd.Start()returns, the child exits,SIGCHLDfires, and the reaper reaps the process beforecmd.Wait()gets to it. The result is a spuriousexec: signal: child exited already or missing exit-code propagation.This PR introduces:
processRegistry (registry.go)— a thread-safe, package-internal map that tracks PIDs of processes started by the executor. Exposes a read-onlyProcessTrackerinterface viaTracker()for external consumers (zombie reaper) and package-privateregisterPID/unregisterPIDhelpers for the executor methods.Run,Output, andRunAndLogLinesinexecutor.go— splitcmd.Run() / cmd.Output()into explicitStart→registerPID→Wait→unregisterPIDsequences so the child PID istracked for the entire lifetime between fork and reap.
Comprehensive tests (executor_test.go)— covers registry basics, double-unregister safety, concurrent access, and integration withOutput / RunAndLogLines(including failed-start scenarios).The zombie reaper can now call
executor.Tracker().IsActive(pid)to skip PIDs that the executor will reap itself.