Shell: Don't hang on empty stdin
authorBrad Jorsch <bjorsch@wikimedia.org>
Thu, 22 Feb 2018 22:13:28 +0000 (17:13 -0500)
committerBrad Jorsch <bjorsch@wikimedia.org>
Thu, 22 Feb 2018 22:13:28 +0000 (17:13 -0500)
If the write buffer for a file descriptor is empty, don't try to write
to it. Just close it and continue on.

Bug: T188019
Change-Id: Ie5b5ac1ef1aec4ae763cf4d0d58d3a28e42b7d2a

includes/shell/Command.php
tests/phpunit/includes/shell/CommandTest.php

index d6f9578..d9fa82d 100644 (file)
@@ -463,6 +463,12 @@ class Command {
                                $isWrite = array_key_exists( $fd, $readPipes );
 
                                if ( $isWrite ) {
+                                       // Don't bother writing if the buffer is empty
+                                       if ( $buffers[$fd] === '' ) {
+                                               fclose( $pipes[$fd] );
+                                               unset( $pipes[$fd] );
+                                               continue;
+                                       }
                                        $res = fwrite( $pipe, $buffers[$fd], 65536 );
                                } else {
                                        $res = fread( $pipe, 65536 );
index 3862cc2..2e03163 100644 (file)
@@ -170,5 +170,12 @@ class CommandTest extends PHPUnit\Framework\TestCase {
                $command->input( str_repeat( '!', 1000000 ) );
                $result = $command->execute();
                $this->assertSame( 1000000, strlen( $result->getStdout() ) );
+
+               // And try it with empty input
+               $command = new Command();
+               $command->params( 'cat' );
+               $command->input( '' );
+               $result = $command->execute();
+               $this->assertSame( '', $result->getStdout() );
        }
 }