php-shellcommand: PHP hanging indefinitely
I was testing some html conversions using the mikehaertl/phpwkhtmltopdf, when I noticed one made PHP hang indefinitely.
Doing some debug, I found out that it was happening in the php-shellcommand/src/Command.php file at line number 317:
$this->_stdOut = stream_get_contents($pipes[1]);
After some research, I found a similar problem in this stackoverflow question and an apparent solution. http://stackoverflow.com/questions/31194152/proc-open-hangs-when-trying-to-read-from-a-stream
According to the stackoverflow answer, there is a possibility of an execution not outputting anything to stdout (pipe[1]) when an error occurs, so stream_get_contents waits for a stream that never comes.
As pointed out in the stackoverflow answer, reading the stderr (pipe[2]) before stdout (pipe[1]) did the trick for me, with the PHP finishing execution and the error message being outputted.
$this->_stdErr = stream_get_contents($pipes[2]);
$this->_stdOut = stream_get_contents($pipes[1]);
My system config:
- Ubuntu 14.04.1 LTS running as a headless VM on Virutalbox
- PHP Version 5.5.22-1+deb.sury.org~trusty+1
- Apache/2.4.12
- wkhtmltopdf 0.12.2 (with patched qt)
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 27 (16 by maintainers)
Commits related to this issue
- Issue #20 Read stderr before stdout to avoid hanging processes — committed to mikehaertl/php-shellcommand by mikehaertl 7 years ago
- Revert "Issue #20 Read stderr before stdout to avoid hanging processes" This reverts commit a628505cd99b201375dd6bcd0b062ee07c8ba556. — committed to mikehaertl/php-shellcommand by mikehaertl 7 years ago
- Issue #20 Add stream_set_blocking() calls for proc_open() — committed to mikehaertl/php-shellcommand by mikehaertl 5 years ago
- Issue #20 Implement read/write loop in non-blocking mode — committed to mikehaertl/php-shellcommand by mikehaertl 5 years ago
- Issue #20 Add more explaining comments — committed to mikehaertl/php-shellcommand by mikehaertl 5 years ago
- Issue #20 Tweak handling of input streams — committed to mikehaertl/php-shellcommand by mikehaertl 5 years ago
- Merge pull request #41 from mikehaertl/20-add-stream_set_blocking-calls Issue #20 Add stream_set_blocking() calls for proc_open() — committed to mikehaertl/php-shellcommand by mikehaertl 5 years ago
- Issue #20 Implement timeout feature — committed to mikehaertl/php-shellcommand by mikehaertl 5 years ago
Release 1.5.0 now contains this new logic. Thanks everyone for the help and feedback!
Cool! I want to wait for some more feedback from @schmunk42 , too. He also had issues and I think he was using input streams (i.e. open file handles).
If things go well I’ll create a new release very soon. Thanks again for your help which brought me on the right track.
@mikehaertl oh wow, well done! What a convoluted situation to have to handle!
I reviewed your changes and everything looks good. I grabbed a copy of the branch and ran it in the same environment I was using previously when we hit the original issue with the process hanging, and everything worked perfectly.
Well done!