Browse Source

fix "Failed to invoke event callback" for php8

walkor 4 years ago
parent
commit
d19480d822

+ 6 - 12
Connection/AsyncTcpConnection.php

@@ -281,11 +281,9 @@ class AsyncTcpConnection extends TcpConnection
             try {
                 \call_user_func($this->onError, $this, $code, $msg);
             } catch (\Exception $e) {
-                Worker::log($e);
-                exit(250);
+                Worker::stopAll(250, $e);
             } catch (\Error $e) {
-                Worker::log($e);
-                exit(250);
+                Worker::stopAll(250, $e);
             }
         }
     }
@@ -349,11 +347,9 @@ class AsyncTcpConnection extends TcpConnection
                 try {
                     \call_user_func($this->onConnect, $this);
                 } catch (\Exception $e) {
-                    Worker::log($e);
-                    exit(250);
+                    Worker::stopAll(250, $e);
                 } catch (\Error $e) {
-                    Worker::log($e);
-                    exit(250);
+                    Worker::stopAll(250, $e);
                 }
             }
             // Try to emit protocol::onConnect
@@ -361,11 +357,9 @@ class AsyncTcpConnection extends TcpConnection
                 try {
                     \call_user_func(array($this->protocol, 'onConnect'), $this);
                 } catch (\Exception $e) {
-                    Worker::log($e);
-                    exit(250);
+                    Worker::stopAll(250, $e);
                 } catch (\Error $e) {
-                    Worker::log($e);
-                    exit(250);
+                    Worker::stopAll(250, $e);
                 }
             }
         } else {

+ 6 - 12
Connection/AsyncUdpConnection.php

@@ -98,11 +98,9 @@ class AsyncUdpConnection extends UdpConnection
             try {
                 \call_user_func($this->onMessage, $this, $recv_buffer);
             } catch (\Exception $e) {
-                Worker::log($e);
-                exit(250);
+                Worker::stopAll(250, $e);
             } catch (\Error $e) {
-                Worker::log($e);
-                exit(250);
+                Worker::stopAll(250, $e);
             }
         }
         return true;
@@ -152,11 +150,9 @@ class AsyncUdpConnection extends UdpConnection
             try {
                 \call_user_func($this->onClose, $this);
             } catch (\Exception $e) {
-                Worker::log($e);
-                exit(250);
+                Worker::stopAll(250, $e);
             } catch (\Error $e) {
-                Worker::log($e);
-                exit(250);
+                Worker::stopAll(250, $e);
             }
         }
         $this->onConnect = $this->onMessage = $this->onClose = null;
@@ -197,11 +193,9 @@ class AsyncUdpConnection extends UdpConnection
             try {
                 \call_user_func($this->onConnect, $this);
             } catch (\Exception $e) {
-                Worker::log($e);
-                exit(250);
+                Worker::stopAll(250, $e);
             } catch (\Error $e) {
-                Worker::log($e);
-                exit(250);
+                Worker::stopAll(250, $e);
             }
         }
     }

+ 18 - 36
Connection/TcpConnection.php

@@ -369,11 +369,9 @@ class TcpConnection extends ConnectionInterface
                         try {
                             \call_user_func($this->onError, $this, \WORKERMAN_SEND_FAIL, 'client closed');
                         } catch (\Exception $e) {
-                            Worker::log($e);
-                            exit(250);
+                            Worker::stopAll(250, $e);
                         } catch (\Error $e) {
-                            Worker::log($e);
-                            exit(250);
+                            Worker::stopAll(250, $e);
                         }
                     }
                     $this->destroy();
@@ -636,11 +634,9 @@ class TcpConnection extends ConnectionInterface
                     // Decode request buffer before Emitting onMessage callback.
                     \call_user_func($this->onMessage, $this, $parser::decode($one_request_buffer, $this));
                 } catch (\Exception $e) {
-                    Worker::log($e);
-                    exit(250);
+                    Worker::stopAll(250, $e);
                 } catch (\Error $e) {
-                    Worker::log($e);
-                    exit(250);
+                    Worker::stopAll(250, $e);
                 }
             }
             return;
@@ -659,11 +655,9 @@ class TcpConnection extends ConnectionInterface
         try {
             \call_user_func($this->onMessage, $this, $this->_recvBuffer);
         } catch (\Exception $e) {
-            Worker::log($e);
-            exit(250);
+            Worker::stopAll(250, $e);
         } catch (\Error $e) {
-            Worker::log($e);
-            exit(250);
+            Worker::stopAll(250, $e);
         }
         // Clean receive buffer.
         $this->_recvBuffer = '';
@@ -692,11 +686,9 @@ class TcpConnection extends ConnectionInterface
                 try {
                     \call_user_func($this->onBufferDrain, $this);
                 } catch (\Exception $e) {
-                    Worker::log($e);
-                    exit(250);
+                    Worker::stopAll(250, $e);
                 } catch (\Error $e) {
-                    Worker::log($e);
-                    exit(250);
+                    Worker::stopAll(250, $e);
                 }
             }
             if ($this->_status === self::STATUS_CLOSING) {
@@ -762,11 +754,9 @@ class TcpConnection extends ConnectionInterface
             try {
                 \call_user_func($this->onSslHandshake, $this);
             } catch (\Exception $e) {
-                Worker::log($e);
-                exit(250);
+                Worker::stopAll(250, $e);
             } catch (\Error $e) {
-                Worker::log($e);
-                exit(250);
+                Worker::stopAll(250, $e);
             }
         }
         return true;
@@ -859,11 +849,9 @@ class TcpConnection extends ConnectionInterface
                 try {
                     \call_user_func($this->onBufferFull, $this);
                 } catch (\Exception $e) {
-                    Worker::log($e);
-                    exit(250);
+                    Worker::stopAll(250, $e);
                 } catch (\Error $e) {
-                    Worker::log($e);
-                    exit(250);
+                    Worker::stopAll(250, $e);
                 }
             }
         }
@@ -882,11 +870,9 @@ class TcpConnection extends ConnectionInterface
                 try {
                     \call_user_func($this->onError, $this, \WORKERMAN_SEND_FAIL, 'send buffer full and drop package');
                 } catch (\Exception $e) {
-                    Worker::log($e);
-                    exit(250);
+                    Worker::stopAll(250, $e);
                 } catch (\Error $e) {
-                    Worker::log($e);
-                    exit(250);
+                    Worker::stopAll(250, $e);
                 }
             }
             return true;
@@ -930,11 +916,9 @@ class TcpConnection extends ConnectionInterface
             try {
                 \call_user_func($this->onClose, $this);
             } catch (\Exception $e) {
-                Worker::log($e);
-                exit(250);
+                Worker::stopAll(250, $e);
             } catch (\Error $e) {
-                Worker::log($e);
-                exit(250);
+                Worker::stopAll(250, $e);
             }
         }
         // Try to emit protocol::onClose
@@ -942,11 +926,9 @@ class TcpConnection extends ConnectionInterface
             try {
                 \call_user_func(array($this->protocol, 'onClose'), $this);
             } catch (\Exception $e) {
-                Worker::log($e);
-                exit(250);
+                Worker::stopAll(250, $e);
             } catch (\Error $e) {
-                Worker::log($e);
-                exit(250);
+                Worker::stopAll(250, $e);
             }
         }
         $this->_sendBuffer = $this->_recvBuffer = '';

+ 4 - 8
Events/Ev.php

@@ -59,11 +59,9 @@ class Ev implements EventInterface
             try {
                 \call_user_func($func, $fd);
             } catch (\Exception $e) {
-                Worker::log($e);
-                exit(250);
+                Worker::stopAll(250, $e);
             } catch (\Error $e) {
-                Worker::log($e);
-                exit(250);
+                Worker::stopAll(250, $e);
             }
         };
         switch ($flag) {
@@ -140,11 +138,9 @@ class Ev implements EventInterface
         try {
             \call_user_func_array($param[0], $param[1]);
         } catch (\Exception $e) {
-            Worker::log($e);
-            exit(250);
+            Worker::stopAll(250, $e);
         } catch (\Error $e) {
-            Worker::log($e);
-            exit(250);
+            Worker::stopAll(250, $e);
         }
     }
 

+ 2 - 4
Events/Event.php

@@ -166,11 +166,9 @@ class Event implements EventInterface
         try {
             \call_user_func_array($param[0], $param[1]);
         } catch (\Exception $e) {
-            Worker::log($e);
-            exit(250);
+            Worker::stopAll(250, $e);
         } catch (\Error $e) {
-            Worker::log($e);
-            exit(250);
+            Worker::stopAll(250, $e);
         }
     }
     

+ 2 - 2
Events/EventInterface.php

@@ -63,10 +63,10 @@ interface EventInterface
      * @param mixed    $fd
      * @param int      $flag
      * @param callable $func
-     * @param mixed    $args
+     * @param array    $args
      * @return bool
      */
-    public function add($fd, $flag, $func, $args = null);
+    public function add($fd, $flag, $func, $args = array());
 
     /**
      * Remove event listener from event loop.

+ 2 - 4
Events/Libevent.php

@@ -172,11 +172,9 @@ class Libevent implements EventInterface
         try {
             \call_user_func_array($this->_eventTimer[$timer_id][0], $this->_eventTimer[$timer_id][1]);
         } catch (\Exception $e) {
-            Worker::log($e);
-            exit(250);
+            Worker::stopAll(250, $e);
         } catch (\Error $e) {
-            Worker::log($e);
-            exit(250);
+            Worker::stopAll(250, $e);
         }
         if (isset($this->_eventTimer[$timer_id]) && $this->_eventTimer[$timer_id][3] === self::EV_TIMER_ONCE) {
             $this->del($timer_id, self::EV_TIMER_ONCE);

+ 9 - 5
Events/Swoole.php

@@ -13,6 +13,7 @@
  */
 namespace Workerman\Events;
 
+use Workerman\Worker;
 use Swoole\Event;
 use Swoole\Timer;
 
@@ -38,11 +39,8 @@ class Swoole implements EventInterface
      *
      * @see \Workerman\Events\EventInterface::add()
      */
-    public function add($fd, $flag, $func, $args = null)
+    public function add($fd, $flag, $func, $args = array())
     {
-        if (! isset($args)) {
-            $args = array();
-        }
         switch ($flag) {
             case self::EV_SIGNAL:
                 $res = \pcntl_signal($fd, $func, false);
@@ -67,7 +65,13 @@ class Swoole implements EventInterface
                 }
                 $timer_id = Timer::$method($t,
                     function ($timer_id = null) use ($func, $args, $mapId) {
-                        \call_user_func_array($func, $args);
+                        try {
+                            \call_user_func_array($func, (array)$args);
+                        } catch (\Exception $e) {
+                            Worker::stopAll(250, $e);
+                        } catch (\Error $e) {
+                            Worker::stopAll(250, $e);
+                        }
                         // EV_TIMER_ONCE
                         if (! isset($timer_id)) {
                             // may be deleted in $func

+ 12 - 24
Protocols/Websocket.php

@@ -94,11 +94,9 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
                         try {
                             \call_user_func(isset($connection->onWebSocketClose)?$connection->onWebSocketClose:$connection->worker->onWebSocketClose, $connection);
                         } catch (\Exception $e) {
-                            Worker::log($e);
-                            exit(250);
+                            Worker::stopAll(250, $e);
                         } catch (\Error $e) {
-                            Worker::log($e);
-                            exit(250);
+                            Worker::stopAll(250, $e);
                         }
                     } // Close connection.
                     else {
@@ -157,11 +155,9 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
                             try {
                                 \call_user_func(isset($connection->onWebSocketPing)?$connection->onWebSocketPing:$connection->worker->onWebSocketPing, $connection, $ping_data);
                             } catch (\Exception $e) {
-                                Worker::log($e);
-                                exit(250);
+                                Worker::stopAll(250, $e);
                             } catch (\Error $e) {
-                                Worker::log($e);
-                                exit(250);
+                                Worker::stopAll(250, $e);
                             }
                         } else {
                             $connection->send($ping_data);
@@ -183,11 +179,9 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
                             try {
                                 \call_user_func(isset($connection->onWebSocketPong)?$connection->onWebSocketPong:$connection->worker->onWebSocketPong, $connection, $pong_data);
                             } catch (\Exception $e) {
-                                Worker::log($e);
-                                exit(250);
+                                Worker::stopAll(250, $e);
                             } catch (\Error $e) {
-                                Worker::log($e);
-                                exit(250);
+                                Worker::stopAll(250, $e);
                             }
                         }
                         $connection->websocketType = $tmp_connection_type;
@@ -263,11 +257,9 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
                     try {
                         \call_user_func($connection->onError, $connection, \WORKERMAN_SEND_FAIL, 'send buffer full and drop package');
                     } catch (\Exception $e) {
-                        Worker::log($e);
-                        exit(250);
+                        Worker::stopAll(250, $e);
                     } catch (\Error $e) {
-                        Worker::log($e);
-                        exit(250);
+                        Worker::stopAll(250, $e);
                     }
                 }
                 return '';
@@ -279,11 +271,9 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
                     try {
                         \call_user_func($connection->onBufferFull, $connection);
                     } catch (\Exception $e) {
-                        Worker::log($e);
-                        exit(250);
+                        Worker::stopAll(250, $e);
                     } catch (\Error $e) {
-                        Worker::log($e);
-                        exit(250);
+                        Worker::stopAll(250, $e);
                     }
                 }
             }
@@ -414,11 +404,9 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
                 try {
                     \call_user_func($on_websocket_connect, $connection, $buffer);
                 } catch (\Exception $e) {
-                    Worker::log($e);
-                    exit(250);
+                    Worker::stopAll(250, $e);
                 } catch (\Error $e) {
-                    Worker::log($e);
-                    exit(250);
+                    Worker::stopAll(250, $e);
                 }
                 if (!empty($_SESSION) && \class_exists('\GatewayWorker\Lib\Context')) {
                     $connection->session = \GatewayWorker\Lib\Context::sessionEncode($_SESSION);

+ 12 - 24
Protocols/Ws.php

@@ -97,11 +97,9 @@ class Ws
                         try {
                             \call_user_func($connection->onWebSocketClose, $connection);
                         } catch (\Exception $e) {
-                            Worker::log($e);
-                            exit(250);
+                            Worker::stopAll(250, $e);
                         } catch (\Error $e) {
-                            Worker::log($e);
-                            exit(250);
+                            Worker::stopAll(250, $e);
                         }
                     } // Close connection.
                     else {
@@ -155,11 +153,9 @@ class Ws
                             try {
                                 \call_user_func($connection->onWebSocketPing, $connection, $ping_data);
                             } catch (\Exception $e) {
-                                Worker::log($e);
-                                exit(250);
+                                Worker::stopAll(250, $e);
                             } catch (\Error $e) {
-                                Worker::log($e);
-                                exit(250);
+                                Worker::stopAll(250, $e);
                             }
                         } else {
                             $connection->send($ping_data);
@@ -182,11 +178,9 @@ class Ws
                             try {
                                 \call_user_func($connection->onWebSocketPong, $connection, $pong_data);
                             } catch (\Exception $e) {
-                                Worker::log($e);
-                                exit(250);
+                                Worker::stopAll(250, $e);
                             } catch (\Error $e) {
-                                Worker::log($e);
-                                exit(250);
+                                Worker::stopAll(250, $e);
                             }
                         }
                         $connection->websocketType = $tmp_connection_type;
@@ -264,11 +258,9 @@ class Ws
                     try {
                         \call_user_func($connection->onError, $connection, \WORKERMAN_SEND_FAIL, 'send buffer full and drop package');
                     } catch (\Exception $e) {
-                        Worker::log($e);
-                        exit(250);
+                        Worker::stopAll(250, $e);
                     } catch (\Error $e) {
-                        Worker::log($e);
-                        exit(250);
+                        Worker::stopAll(250, $e);
                     }
                 }
                 return '';
@@ -280,11 +272,9 @@ class Ws
                     try {
                         \call_user_func($connection->onBufferFull, $connection);
                     } catch (\Exception $e) {
-                        Worker::log($e);
-                        exit(250);
+                        Worker::stopAll(250, $e);
                     } catch (\Error $e) {
-                        Worker::log($e);
-                        exit(250);
+                        Worker::stopAll(250, $e);
                     }
                 }
             }
@@ -432,11 +422,9 @@ class Ws
                 try {
                     \call_user_func($connection->onWebSocketConnect, $connection, \substr($buffer, 0, $handshake_response_length));
                 } catch (\Exception $e) {
-                    Worker::log($e);
-                    exit(250);
+                    Worker::stopAll(250, $e);
                 } catch (\Error $e) {
-                    Worker::log($e);
-                    exit(250);
+                    Worker::stopAll(250, $e);
                 }
             }
             // Headbeat.

+ 21 - 28
Worker.php

@@ -1746,11 +1746,9 @@ class Worker
                     try {
                         \call_user_func(static::$onMasterReload);
                     } catch (\Exception $e) {
-                        static::log($e);
-                        exit(250);
+                        static::stopAll(250, $e);
                     } catch (\Error $e) {
-                        static::log($e);
-                        exit(250);
+                        static::stopAll(250, $e);
                     }
                     static::initId();
                 }
@@ -1805,11 +1803,9 @@ class Worker
                 try {
                     \call_user_func($worker->onWorkerReload, $worker);
                 } catch (\Exception $e) {
-                    static::log($e);
-                    exit(250);
+                    static::stopAll(250, $e);
                 } catch (\Error $e) {
-                    static::log($e);
-                    exit(250);
+                    static::stopAll(250, $e);
                 }
             }
 
@@ -1820,12 +1816,17 @@ class Worker
     }
 
     /**
-     * Stop.
+     * Stop all.
      *
-     * @return void
+     * @param int $code
+     * @param string $log
      */
-    public static function stopAll()
+    public static function stopAll($code = 0, $log = '')
     {
+        if ($log) {
+            static::log($log);
+        }
+
         static::$_status = static::STATUS_SHUTDOWN;
         // For master process.
         if (static::$_masterPid === \posix_getpid()) {
@@ -1864,7 +1865,7 @@ class Worker
                 }
 
                 try {
-                    exit(0);
+                    exit($code);
                 } catch (Exception $e) {
 
                 }
@@ -2404,15 +2405,13 @@ class Worker
             try {
                 \call_user_func($this->onWorkerStart, $this);
             } catch (\Exception $e) {
-                static::log($e);
                 // Avoid rapid infinite loop exit.
                 sleep(1);
-                exit(250);
+                static::stopAll(250, $e);
             } catch (\Error $e) {
-                static::log($e);
                 // Avoid rapid infinite loop exit.
                 sleep(1);
-                exit(250);
+                static::stopAll(250, $e);
             }
         }
 
@@ -2432,11 +2431,9 @@ class Worker
             try {
                 \call_user_func($this->onWorkerStop, $this);
             } catch (\Exception $e) {
-                static::log($e);
-                exit(250);
+                static::stopAll(250, $e);
             } catch (\Error $e) {
-                static::log($e);
-                exit(250);
+                static::stopAll(250, $e);
             }
         }
         // Remove listener for server socket.
@@ -2486,11 +2483,9 @@ class Worker
             try {
                 \call_user_func($this->onConnect, $connection);
             } catch (\Exception $e) {
-                static::log($e);
-                exit(250);
+                static::stopAll(250, $e);
             } catch (\Error $e) {
-                static::log($e);
-                exit(250);
+                static::stopAll(250, $e);
             }
         }
     }
@@ -2541,11 +2536,9 @@ class Worker
                 }
                 ++ConnectionInterface::$statistics['total_request'];
             } catch (\Exception $e) {
-                static::log($e);
-                exit(250);
+                static::stopAll(250, $e);
             } catch (\Error $e) {
-                static::log($e);
-                exit(250);
+                static::stopAll(250, $e);
             }
         }
         return true;