Giter Club home page Giter Club logo

Comments (1)

clue avatar clue commented on May 16, 2024 2

@corosion Thanks for posting this excellent question!

I can indeed reproduce the problem you're seeing with the above example code.

If you look at the process tree, you will find that every child process is always wrapped by an additional shell process:

me         11771  0.0  0.0  21368  5656 pts/5    Ss   14:40   0:00      \_ -bash
me         12169  0.2  0.1  93332 25780 pts/5    S+   14:50   0:00      |   \_ php parent.php
me         12170  0.0  0.0   2608   604 pts/5    S+   14:50   0:00      |       \_ sh -c php child.php
me         12172  0.0  0.1  93332 25004 pts/5    S+   14:50   0:00      |       |   \_ php child.php
me         12171  0.0  0.0   2608   600 pts/5    S+   14:50   0:00      |       \_ sh -c php child.php
me         12173  0.2  0.1  93332 24724 pts/5    S+   14:50   0:00      |           \_ php child.php

This is covered in the documentation as per https://github.com/reactphp/child-process#command.

This means that when you send a signal to the child process, it will actually be sent to the wrapping shell instead of the php script. Additionally, most shells will not usually forward any signals to their child processes (see trap). In other words, your actual child never receives a signal and accordingly won't be able to stop executing.

This can be avoided by making sure your desired child process replaces the wrapping shell by prefixing the command with exec:

$process = new \React\ChildProcess\Process('exec php child.php');

With this in place, the process tree now looks as expected:

me         11771  0.0  0.0  21368  5656 pts/5    Ss   14:40   0:00      \_ -bash
me         12185  0.3  0.1  93332 25212 pts/5    S+   14:51   0:00      |   \_ php parent.php
me         12186  0.3  0.1  93332 24848 pts/5    S+   14:51   0:00      |       \_ php child.php
me         12187  0.0  0.1  93332 24988 pts/5    S+   14:51   0:00      |       \_ php child.php

Accordingly, sending a SIGINT to the child processes now works as expected and makes sure they terminate successfully.


Here's why the program did indeed terminate after a while if you occasionally print some data even when using the wrapping shell. The echo PHP_EOL; will perform an underlying write call on STDOUT, which happens to be a process pipe that's shared with the parent process. If the parent terminates, this pipe will be in a state that does not allow any more writes. The write call will result in an EPIPE/ SIGPIPE which will cause your program to terminate if not otherwise handled.

Welcome to the wonderful world of Unix subtleties ✌️

from child-process.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.