Skip to content

Fix zombie process on process destruction - #86

Open
xtrime-ru wants to merge 2 commits into
amphp:2.xfrom
xtrime-ru:fix-posix-zombie
Open

Fix zombie process on process destruction#86
xtrime-ru wants to merge 2 commits into
amphp:2.xfrom
xtrime-ru:fix-posix-zombie

Conversation

@xtrime-ru

@xtrime-ru xtrime-ru commented Aug 24, 2026

Copy link
Copy Markdown

Summary

Prevents POSIX wrapper shells created by proc_open() from remaining as zombie processes when a process is killed or destroyed.

Also handles failures returned by posix_kill() without blocking while waiting for a process that was not terminated.

Root cause

proc_get_status($proc)['pid'], stored as $shellPid, identifies the wrapper shell created by proc_open(). This shell is a direct child of PHP.

The PID stored as $pid belongs to the command started by the wrapper shell. Waiting for $pid cannot reap $shellPid.

Reaping $shellPid previously relied on an event-loop callback. If the event loop did not run after the shell exited, no subsequent waitpid() call was made and the shell remained a zombie.

Additionally, posix_kill() failures were ignored. A failed signal followed by a blocking waitpid() could make Process::kill() wait for the command to exit naturally.

Changes

  • Reap $shellPid after kill() and during handle destruction.
  • Keep the global shutdown callback non-blocking.
  • Retry waitpid() when interrupted by EINTR.
  • Throw when posix_kill() fails.
  • Treat ESRCH as successful termination because the OS process has already exited.
  • Add regression tests for kill, destruction, completed processes, shutdown, and signal failures.

This completes the POSIX shutdown handling introduced in 6c711ee.

Testing

  • Targeted regression run: 6 tests, 14 assertions passed.
  • Psalm passed.
  • PHP CS Fixer passed.

@xtrime-ru
xtrime-ru force-pushed the fix-posix-zombie branch 4 times, most recently from e455beb to 47566d0 Compare August 25, 2026 21:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant