Why Python's subprocess.terminate() Often Fails to Kill Child Processes
When a deployment script exits cleanly, child processes it spawned can silently keep running, holding ports or file locks and breaking subsequent deploys. Python's process.terminate() sends a SIGTERM signal, which is merely a request that the target process can ignore, defer, or handle in unexpected ways. The process.kill() method sends SIGKILL, which the kernel enforces unconditionally, but it must be used as a fallback after SIGTERM times out. A further complication arises when shell=True is used or when child processes spawn their own subprocesses, since the signal reaches only the direct child and not the entire process tree. A reliable termination pattern requires sending SIGTERM first, waiting with a timeout, escalating to SIGKILL if needed, and accounting for grandchild processes to ensure nothing is left running.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in