Comments (1)
@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)
- Don't replace generated command from \ to \\ HOT 2
- Next steps for ChildProcess with ReactPHP v3
- support $cmd as array in Process HOT 3
- STDIO streams should be readable or writable, but not duplex HOT 1
- Roadmap to v1.0.0 HOT 5
- Update stream dependency to 0.7 HOT 1
- Segmentation Fault (core dumped) HOT 19
- Spawn child process and creatie server in it HOT 1
- Persistent SSH Child Process HOT 6
- [FYI] Child processes inherit open file handles from parent HOT 10
- What is the correct way to obtain data from a subprocess HOT 1
- Improper detection of sigchld compile flag HOT 4
- PHPUnit Code Coverage for runned process HOT 1
- Clean up API HOT 3
- Consider adding specific libuv adapter HOT 2
- Trying to access array offset on value of type null HOT 4
- is it possible to debug a child process? HOT 2
- Fatal error when phpinfo is disabled HOT 4
- Callback as child processes HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from child-process.