php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #39992 proc_terminate() leaves children of child running
Submitted: 2006-12-30 15:52 UTC Modified: 2021-02-16 13:11 UTC
Votes:44
Avg. Score:4.4 ± 0.9
Reproduced:40 of 43 (93.0%)
Same Version:3 (7.5%)
Same OS:29 (72.5%)
From: nlopess@php.net Assigned:
Status: No Feedback Package: Program Execution
PHP Version: 6CVS-2006-12-30 (CVS) OS: linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: nlopess@php.net
New email:
PHP Version: OS:

 

 [2006-12-30 15:52 UTC] nlopess@php.net
Description:
------------
With the short reproduce code below, PHP fork()s a new process (sh), that itself forks a new one (php). proc_terminate() kill()s the sh process, but the php one doesn't get killed.
I'm not really sure what is causing this problem, but it is breaking run-tests.php on timeout.

Reproduce code:
---------------
<?php

$cmd='php -r "while(1){}" 2>&1';
$proc = proc_open($cmd, array(), $pipes);
var_dump(proc_terminate($proc));

?>


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-12-30 15:54 UTC] nlopess@php.net
Ah I forgot: removing the "2>&1" part everything works fine.
 [2008-08-12 16:28 UTC] jani@php.net
Nuno, is this still valid bug? I can't reproduce it even with PHP 5.2.6..
 [2008-08-12 22:03 UTC] nlopess@php.net
Yes, I'm still able to reproduce it. In some systems you may need higher proviledges in order to reproduce this bug.
 [2010-02-17 08:23 UTC] gryp at dakin dot be
We're having the same problem by starting java.
(the java process starts as root and forks itself to another userid)

the 'sh' gets killed but our java process keeps on running
 [2010-03-13 20:19 UTC] mast at imast dot ru
Well I've also just found this problem.
I run sh script via proc_open and if this script runs child processes that could 
hang, they will not be killed if you run proc_terminate().

To fix it I use:

// As soon proc is started
$status = proc_get_status($proc);
posix_setpgid($status['pid'], $status['pid']);

// When I need to kill it
posix_kill(-$status['pid'], 9); // sends SIGKILL to all processes inside group
proc_close($proc);

Why not to add this into proc_terminate() implementation?
 [2014-10-17 12:12 UTC] alanevans+phpnet at gmail dot com
I've happened on this same issue also, though I also find that proc_get_status returns only the pid of some /bin/sh wrapper.

I've tried on PHP 5.5 and 5.6,

Interesting to note is that the behaviour on OSX is quite easy to handle: there is no /bin/sh wrapper process at all in that case, but on Ubuntu (currently tested on lucid), you only ever have access to the /bin/sh proc: the process you actually started and the one that you care about is left running.

I'm going to work around this by having child processes write their own pids to a file and use those for signalling.
 [2016-03-06 02:29 UTC] php at mattwhitlock dot name
An easy workaround...

Instead of:

  $proc = proc_open('cat', ...);

Use this:

  $proc = proc_open('exec cat', ...);

It tells the shell to replace its own process with the command being executed rather than forking a child process. This way you can properly kill the command using proc_terminate($proc).
 [2020-03-06 10:38 UTC] nikic@php.net
-Status: Open +Status: Feedback
 [2020-03-06 10:38 UTC] nikic@php.net
I believe this should be addressed in PHP 7.4 by writing:

$proc = proc_open(
    ['php', '-r', 'while(1){}'],
    [2 => ['redirect', 1]],
    $pipes);
var_dump(proc_terminate($proc));

This will not go through a shell, and as such the created process will be terminated directly.

Please let me know if there are still issues when going through the shell-free interface.
 [2020-03-15 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 [2021-02-16 12:23 UTC] machitgarha at outlook dot com
The bug is reproducible on 7.4.15, Fedora Workstation 33. Any fixes?
 [2021-02-16 13:11 UTC] nikic@php.net
@machitgarha: To clarify, you're still seeing this issue when passing an *array* command to proc_open()? It is expected that this doesn't work when passing a *string* command, because those are invoked through a shell.
 [2023-02-28 22:16 UTC] crrodriguez at opensuse dot org
I cannot reproduce this on linux with PHP 8.x git master.. even when run thousands of times through a shell.. Im almost certainly missing something oe it was a bug in the shell...
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 16:01:29 2024 UTC