fix: reap zombie worker processes in containerized environments#2184
Open
MetaAlms wants to merge 1 commit into
Open
fix: reap zombie worker processes in containerized environments#2184MetaAlms wants to merge 1 commit into
MetaAlms wants to merge 1 commit into
Conversation
TEN workers are spawned via tman, which forks a [main] subprocess. When tman receives SIGKILL during stop(), it has no chance to wait() for [main]. [main] becomes an orphan and accumulates as zombie. Register a SIGCHLD handler to automatically reap all orphaned child processes via waitpid(-1, WNOHANG).
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.
Problem
In containerized environments without a proper init system (PID 1), TEN Agent Server spawns [main] worker processes via
tman run start. When the worker is stopped,tmanexits without callingwait()on its child [main], leaving zombie processes that accumulate over time.Root Cause
The server starts
tmanwithSetpgid: true, then kills the process group viasyscall.Kill(-pid, SIGKILL).tmanis terminated before it can reap its child [main], so the kernel re-parents the child to PID 1. In a container, PID 1 is the Go server itself, which does not callwaitpid()on child processes outside its owncmd.Wait()goroutine.Fix
Register a
SIGCHLDhandler inworker_linux.gothat callswaitpid(-1, WNOHANG)in a loop to reap any zombie children re-parented to PID 1.Verification
go build ./...passesRelated
This is a standard Linux pattern for init-less containers. The handler is placed in
worker_linux.go(linux || darwinbuild tag) since it relies onsyscall.SIGCHLDandsyscall.Wait4.