From 86cfcfdbbaf67ca31b9e7fb7e71a238747a2b54a Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Thu, 22 Feb 2018 17:13:28 -0500 Subject: [PATCH] Shell: Don't hang on empty stdin 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 | 6 ++++++ tests/phpunit/includes/shell/CommandTest.php | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/includes/shell/Command.php b/includes/shell/Command.php index d6f95783f0..d9fa82dfa3 100644 --- a/includes/shell/Command.php +++ b/includes/shell/Command.php @@ -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 ); diff --git a/tests/phpunit/includes/shell/CommandTest.php b/tests/phpunit/includes/shell/CommandTest.php index 3862cc2444..2e03163885 100644 --- a/tests/phpunit/includes/shell/CommandTest.php +++ b/tests/phpunit/includes/shell/CommandTest.php @@ -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() ); } } -- 2.20.1