Skip to content

Commit 7dedcb5

Browse files
committed
Support starting with closed STDIN
1 parent b2cbfdc commit 7dedcb5

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

examples/01-periodic.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,10 @@
2929
$stdio->end();
3030
});
3131

32+
// input already closed on program start, exit immediately
33+
if (!$stdio->isReadable()) {
34+
$loop->cancelTimer($timer);
35+
$stdio->end();
36+
}
37+
3238
$loop->run();

src/Stdin.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ public function __construct(LoopInterface $loop)
1414
{
1515
parent::__construct(STDIN, $loop);
1616

17+
// support starting program with closed STDIN ("example.php 0<&-")
18+
// the stream is a valid resource and is not EOF, but fstat fails
19+
if (fstat(STDIN) === false) {
20+
return $this->close();
21+
}
22+
1723
if ($this->isTty()) {
1824
$this->oldMode = shell_exec('stty -g');
1925

tests/FunctionalExampleTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ public function testPeriodicExampleWithSleepNoInputQuitsOnEnd()
3030
$this->assertNotContains('you just said:', $output);
3131
}
3232

33+
public function testPeriodicExampleWithClosedInputQuitsImmediately()
34+
{
35+
$output = $this->execExample('php 01-periodic.php <&-');
36+
37+
if (strpos($output, 'said') !== false) {
38+
$this->markTestIncomplete('Your platform exhibits a closed STDIN bug, this may need some further debugging');
39+
}
40+
41+
$this->assertNotContains('you just said:', $output);
42+
}
43+
3344
private function execExample($command)
3445
{
3546
chdir(__DIR__ . '/../examples/');

0 commit comments

Comments
 (0)