diff --git a/src/danog/MadelineProto/APIFactory.php b/src/danog/MadelineProto/APIFactory.php index 5cc33cd0..56d3a8da 100644 --- a/src/danog/MadelineProto/APIFactory.php +++ b/src/danog/MadelineProto/APIFactory.php @@ -133,7 +133,7 @@ class APIFactory extends AsyncConstruct public function __call($name, $arguments) { - $yielded = $this->__call_async($name, $arguments); + $yielded = $this->call($this->__call_async($name, $arguments)); $async = $this->lua === false && (is_array(end($arguments)) && isset(end($arguments)['async']) ? end($arguments)['async'] : ($this->async && $name !== 'loop')); if ($async) { return $yielded; @@ -158,15 +158,7 @@ class APIFactory extends AsyncConstruct yield $this->initAsync(); } if (Magic::is_fork() && !Magic::$processed_fork) { - throw new Exception("Forking not supported"); - /* - \danog\MadelineProto\Logger::log('Detected fork'); - $this->API->reset_session(); - foreach ($this->API->datacenter->sockets as $datacenter) { - yield $datacenter->reconnect(); - } - Magic::$processed_fork = true; - */ + throw new Exception("Forking not supported, use async logic, instead: https://docs.madelineproto.xyz/docs/ASYNC.html"); } if (isset($this->session) && !is_null($this->session) && time() - $this->serialized > $this->API->settings['serialization']['serialization_interval']) { Logger::log("Didn't serialize in a while, doing that now..."); @@ -189,7 +181,7 @@ class APIFactory extends AsyncConstruct $aargs['datacenter'] = $this->API->datacenter->curdc; $args = isset($arguments[0]) && is_array($arguments[0]) ? $arguments[0] : []; - return yield $this->API->method_call_async_read($name, $args, $aargs); + return yield $this->API->method_call_async_read($name, $args, $aargs); } else { return yield $this->methods[$lower_name](...$arguments); } diff --git a/src/danog/MadelineProto/CombinedAPI.php b/src/danog/MadelineProto/CombinedAPI.php index 6327eb7c..1c0ed809 100644 --- a/src/danog/MadelineProto/CombinedAPI.php +++ b/src/danog/MadelineProto/CombinedAPI.php @@ -122,6 +122,7 @@ class CombinedAPI if (\danog\MadelineProto\Magic::$has_thread && is_object(\Thread::getCurrentThread()) || Magic::is_fork()) { return; } + $this->serialize(); } diff --git a/src/danog/MadelineProto/Coroutine.php b/src/danog/MadelineProto/Coroutine.php index 01cf5c40..e656b5b1 100644 --- a/src/danog/MadelineProto/Coroutine.php +++ b/src/danog/MadelineProto/Coroutine.php @@ -62,8 +62,34 @@ final class Coroutine implements Promise */ public function __construct(\Generator $generator) { + /* + $this->generator = new class($generator) { + private $s = ''; + private $g; + private $trace; + public function __construct($g) { + $this->g = $g; + $this->s .= spl_object_hash($this).', '; + } + public function __call($a, $args) { + $this->s .= "$a, "; + try { + $res = $this->g->{$a}(...$args); + if (is_array($res) && isset($res['my_trace'])) { + $this->trace = $res; + $res = $this->g->{$a}(...$args); + } + return $res; + } catch (\Throwable $e) { + $this->s .= $e->getMessage(); + $this->s .= ', '; + var_dump($this->s, $this->trace); + throw $e; + } + } + //public function __destruct() { var_dump($this->s); } + };*/ $this->generator = $generator; - try { $yielded = $this->generator->current(); while (!$yielded instanceof Promise) { diff --git a/src/danog/MadelineProto/Logger.php b/src/danog/MadelineProto/Logger.php index 00885c1e..98ce2eb7 100644 --- a/src/danog/MadelineProto/Logger.php +++ b/src/danog/MadelineProto/Logger.php @@ -23,6 +23,9 @@ namespace danog\MadelineProto; use Amp\ByteStream\ResourceOutputStream; +use Amp\Failure; +use function \Amp\ByteStream\getStdout; +use function \Amp\ByteStream\getStderr; class Logger { @@ -96,18 +99,18 @@ class Logger $this->newline = PHP_EOL; if ($this->mode === 3) { - $this->stdout = new ResourceOutputStream(STDOUT); + $this->stdout = getStdout(); if (php_sapi_name() !== 'cli') $this->newline = '
'.$this->newline; } elseif ($this->mode === 2) { $this->stdout = new ResourceOutputStream(fopen($this->optional, 'a+')); } elseif ($this->mode === 1) { $result = @ini_get('error_log'); if ($result === 'syslog') { - $this->stdout = new ResourceOutputStream(STDERR); + $this->stdout = getStderr(); } elseif ($result) { $this->stdout = new ResourceOutputStream(fopen($result, 'a+')); } else { - $this->stdout = new ResourceOutputStream(STDERR); + $this->stdout = getStderr(); } } } @@ -142,9 +145,6 @@ class Logger if (\danog\MadelineProto\Magic::$has_thread && is_object(\Thread::getCurrentThread())) { $prefix .= ' (t)'; } - if (\danog\MadelineProto\Magic::is_fork()) { - $prefix .= ' (p)'; - } if (!is_string($param)) { $param = json_encode($param, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); } @@ -157,12 +157,15 @@ class Logger $this->stdout->write($param.$this->newline); break; default: - $this->stdout->write(Magic::$isatty ? "\33[".$this->colors[$level].'m'.$param."\33[0m".$this->newline : $param.$this->newline); + $param = Magic::$isatty ? "\33[".$this->colors[$level].'m'.$param."\33[0m".$this->newline : $param.$this->newline; + if ($this->stdout->write($param) instanceof Failure) { + echo "(closed) $param"; + } break; } } public function __destruct() { - $this->wait($this->stdout->write('')); + //$this->wait($this->stdout->write('')); } } diff --git a/src/danog/MadelineProto/Loop/Impl/Loop.php b/src/danog/MadelineProto/Loop/Impl/Loop.php index ba325015..6fe3da9d 100644 --- a/src/danog/MadelineProto/Loop/Impl/Loop.php +++ b/src/danog/MadelineProto/Loop/Impl/Loop.php @@ -55,6 +55,7 @@ abstract class Loop implements LoopInterface private function loopImpl() { + //yield ['my_trace' => debug_backtrace(0, 1)[0], (string) $this]; $this->startedLoop(); $this->API->logger->logger("Entered $this", Logger::ULTRA_VERBOSE); try { @@ -62,6 +63,7 @@ abstract class Loop implements LoopInterface } finally { $this->exitedLoop(); $this->API->logger->logger("Exited $this", Logger::ULTRA_VERBOSE); + return null; } } diff --git a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php index 1ec386bb..dc425e7c 100644 --- a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php @@ -140,7 +140,7 @@ trait PeerHandler public function cache_pwr_chat($id, $full_fetch, $send) { - $this->callFork((function () use ($id, $full_fetch, $send) { + $this->callFork((function () use ($id, $full_fetch, $send) { try { yield $this->get_pwr_chat_async($id, $full_fetch, $send); } catch (\danog\MadelineProto\Exception $e) { @@ -148,7 +148,7 @@ trait PeerHandler } catch (\danog\MadelineProto\RPCErrorException $e) { $this->logger->logger("While caching: ".$e->getMessage(), \danog\MadelineProto\Logger::WARNING); } - })()); + })()); } public function peer_isset_async($id) diff --git a/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php b/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php index 1f06beb1..682417a2 100644 --- a/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php @@ -200,6 +200,8 @@ trait ResponseHandler break; case 'msg_detailed_info': $this->datacenter->sockets[$datacenter]->check_in_seq_no($current_msg_id); + unset($this->datacenter->sockets[$datacenter]->new_incoming[$current_msg_id]); + $only_updates = false; if (isset($this->datacenter->sockets[$datacenter]->outgoing_messages[$this->datacenter->sockets[$datacenter]->incoming_messages[$current_msg_id]['content']['msg_id']])) { if (isset($this->datacenter->sockets[$datacenter]->incoming_messages[$this->datacenter->sockets[$datacenter]->incoming_messages[$current_msg_id]['content']['answer_msg_id']])) { diff --git a/src/danog/MadelineProto/Wrappers/Events.php b/src/danog/MadelineProto/Wrappers/Events.php index 0b6724c3..902d3fab 100644 --- a/src/danog/MadelineProto/Wrappers/Events.php +++ b/src/danog/MadelineProto/Wrappers/Events.php @@ -61,7 +61,6 @@ trait Events $this->settings['updates']['callback'] = [$this, 'event_update_handler']; $this->settings['updates']['handle_updates'] = true; $this->settings['updates']['run_callback'] = true; - if (!$this->asyncInitPromise) { $this->startUpdateSystem(); } diff --git a/src/danog/MadelineProto/Wrappers/Loop.php b/src/danog/MadelineProto/Wrappers/Loop.php index 101b1577..6f87c27a 100644 --- a/src/danog/MadelineProto/Wrappers/Loop.php +++ b/src/danog/MadelineProto/Wrappers/Loop.php @@ -36,10 +36,12 @@ trait Loop public function loop_async($max_forks = 0) { if (is_callable($max_forks)) { + $this->logger->logger('Running async callable and exiting from loop'); return yield $max_forks(); } if (in_array($this->settings['updates']['callback'], [['danog\\MadelineProto\\API', 'get_updates_update_handler'], 'get_updates_update_handler'])) { - return true; + $this->logger->logger('Getupdates event handler is enabled, exiting from loop', \danog\MadelineProto\Logger::FATAL_ERROR); + return false; } if (!is_callable($this->loop_callback) || (is_array($this->loop_callback) && $this->loop_callback[1] === 'onLoop' && !method_exists(...$this->loop_callback))) { $this->loop_callback = null;