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

Most upvoted comments

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!