Skip to content

Restore blocking mode before closing and restore TTY mode on unclean shutdown #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 1, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Stdin.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public function __construct(LoopInterface $loop)
// Disable icanon (so we can fread each keypress) and echo (we'll do echoing here instead)
shell_exec('stty -icanon -echo');
}

// register shutdown function to restore TTY mode in case of unclean shutdown (uncaught exception)
// this will not trigger on SIGKILL etc., but the terminal should take care of this
register_shutdown_function(array($this, 'close'));
}

public function close()
Expand All @@ -52,6 +56,11 @@ private function restore()
shell_exec(sprintf('stty %s', $this->oldMode));
$this->oldMode = null;
}

// restore blocking mode so following programs behave normally
if (defined('STDIN') && is_resource(STDIN)) {
stream_set_blocking(STDIN, true);
}
}

/**
Expand Down