diff --git a/src/danog/MadelineProto/API.php b/src/danog/MadelineProto/API.php index afefbdcb..accd0954 100644 --- a/src/danog/MadelineProto/API.php +++ b/src/danog/MadelineProto/API.php @@ -173,8 +173,12 @@ class API extends APIFactory if ($this->asyncInitPromise) { $this->init(); } - $this->API->logger('Shutting down MadelineProto (normally or due to an exception, idk)'); - $this->API->destructing = true; + if ($this->API) { + $this->API->logger('Shutting down MadelineProto (normally or due to an exception, idk)'); + $this->API->destructing = true; + } else { + Logger::log('Shutting down MadelineProto (normally or due to an exception, idk)'); + } $this->destructing = true; $this->wait($this->serialize()); //restore_error_handler(); diff --git a/src/danog/MadelineProto/Magic.php b/src/danog/MadelineProto/Magic.php index d1a10b55..a3661910 100644 --- a/src/danog/MadelineProto/Magic.php +++ b/src/danog/MadelineProto/Magic.php @@ -20,6 +20,7 @@ namespace danog\MadelineProto; use function Amp\Promise\wait; +use Amp\Loop; class Magic { @@ -130,9 +131,9 @@ class Magic self::$can_getcwd = true; } catch (\danog\MadelineProto\Exception $e) { } - if (function_exists('\pcntl_async_signals')) { - pcntl_async_signals(true); - } + // Even an empty handler is enough to catch ctrl+c + Loop::onSignal(SIGINT, static function () { die(); }); + Loop::onSignal(SIGTERM, static function () { die(); }); self::$inited = true; } }