mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-06 20:07:09 +00:00
Partially revert "ConsoleReaderChildProcess: Commit suicide in more cases"
This reverts commit cbe0f44c4f7bc3715acbf148f981bd93111c4c8f. This achieves the same result as the reverted commit wrt. process in the same manner (writing a keepalive into the socket and checking if it failed to send). However, it does _not_ allow the process to die on reaching pipe EOF, since this can cause many spams of subprocesses when stdin is actually not a tty (e.g. in a Docker container).
This commit is contained in:
parent
882df94bcb
commit
6f8f460a6c
@ -23,38 +23,49 @@ declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\console;
|
||||
|
||||
use pocketmine\utils\AssumptionFailedError;
|
||||
use function fclose;
|
||||
use function fgets;
|
||||
use function fopen;
|
||||
use function is_resource;
|
||||
use function stream_select;
|
||||
use function trim;
|
||||
use function usleep;
|
||||
|
||||
final class ConsoleReader{
|
||||
/** @var resource */
|
||||
private $stdin;
|
||||
|
||||
public function __construct(){
|
||||
$stdin = fopen("php://stdin", "r");
|
||||
if($stdin === false) throw new AssumptionFailedError("Opening stdin should never fail");
|
||||
$this->stdin = $stdin;
|
||||
$this->initStdin();
|
||||
}
|
||||
|
||||
private function initStdin() : void{
|
||||
if(is_resource($this->stdin)){
|
||||
fclose($this->stdin);
|
||||
}
|
||||
|
||||
$this->stdin = fopen("php://stdin", "r");
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a line from the console and adds it to the buffer. This method may block the thread.
|
||||
* @throws ConsoleReaderException
|
||||
*/
|
||||
public function readLine() : ?string{
|
||||
if(!is_resource($this->stdin)){
|
||||
$this->initStdin();
|
||||
}
|
||||
|
||||
$r = [$this->stdin];
|
||||
$w = $e = null;
|
||||
if(($count = stream_select($r, $w, $e, 0, 200000)) === 0){ //nothing changed in 200000 microseconds
|
||||
return null;
|
||||
}elseif($count === false){ //stream error
|
||||
throw new ConsoleReaderException("Unexpected EOF on select()");
|
||||
return null;
|
||||
}
|
||||
|
||||
if(($raw = fgets($this->stdin)) === false){ //broken pipe or EOF
|
||||
throw new ConsoleReaderException("Unexpected EOF on fgets()");
|
||||
usleep(200000); //prevent CPU waste if it's end of pipe
|
||||
return null; //loop back round
|
||||
}
|
||||
|
||||
$line = trim($raw);
|
||||
|
@ -45,14 +45,7 @@ if($socket === false){
|
||||
}
|
||||
$consoleReader = new ConsoleReader();
|
||||
while(!feof($socket)){
|
||||
try{
|
||||
$line = $consoleReader->readLine();
|
||||
}catch(ConsoleReaderException $e){
|
||||
//Encountered unexpected EOF. This might be because the user did something stupid, or because the parent process
|
||||
//has died. In either case, commit suicide. If the parent process is still alive, it will start a new console
|
||||
//reader.
|
||||
break;
|
||||
}
|
||||
$line = $consoleReader->readLine();
|
||||
if(@fwrite($socket, ($line ?? "") . "\n") === false){
|
||||
//Always send even if there's no line, to check if the parent is alive
|
||||
//If the parent process was terminated forcibly, it won't close the connection properly, so feof() will return
|
||||
|
@ -1,28 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\console;
|
||||
|
||||
final class ConsoleReaderException extends \RuntimeException{
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user