Skip to content

Commit bd60c1b

Browse files
authored
Merge pull request #58 from clue-labs/restore
Restore blocking mode before closing and restore TTY mode on unclean shutdown
2 parents 32a5b99 + 4c84374 commit bd60c1b

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/Stdin.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public function __construct(LoopInterface $loop)
3232
// Disable icanon (so we can fread each keypress) and echo (we'll do echoing here instead)
3333
shell_exec('stty -icanon -echo');
3434
}
35+
36+
// register shutdown function to restore TTY mode in case of unclean shutdown (uncaught exception)
37+
// this will not trigger on SIGKILL etc., but the terminal should take care of this
38+
register_shutdown_function(array($this, 'close'));
3539
}
3640

3741
public function close()
@@ -52,6 +56,11 @@ private function restore()
5256
shell_exec(sprintf('stty %s', $this->oldMode));
5357
$this->oldMode = null;
5458
}
59+
60+
// restore blocking mode so following programs behave normally
61+
if (defined('STDIN') && is_resource(STDIN)) {
62+
stream_set_blocking(STDIN, true);
63+
}
5564
}
5665

5766
/**

0 commit comments

Comments
 (0)