{"action":"created","issue":{"url":"https://api.github.com/repos/swoft-cloud/swoft/issues/269","repository_url":"https://api.github.com/repos/swoft-cloud/swoft","labels_url":"https://api.github.com/repos/swoft-cloud/swoft/issues/269/labels{/name}","comments_url":"https://api.github.com/repos/swoft-cloud/swoft/issues/269/comments","events_url":"https://api.github.com/repos/swoft-cloud/swoft/issues/269/events","html_url":"https://github.com/swoft-cloud/swoft/issues/269","id":329776492,"node_id":"MDU6SXNzdWUzMjk3NzY0OTI=","number":269,"title":"协程客户端抛出的异常无法被捕获","user":{"login":"EdenChan","id":7194202,"node_id":"MDQ6VXNlcjcxOTQyMDI=","avatar_url":"https://avatars2.githubusercontent.com/u/7194202?v=4","gravatar_id":"","url":"https://api.github.com/users/EdenChan","html_url":"https://github.com/EdenChan","followers_url":"https://api.github.com/users/EdenChan/followers","following_url":"https://api.github.com/users/EdenChan/following{/other_user}","gists_url":"https://api.github.com/users/EdenChan/gists{/gist_id}","starred_url":"https://api.github.com/users/EdenChan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/EdenChan/subscriptions","organizations_url":"https://api.github.com/users/EdenChan/orgs","repos_url":"https://api.github.com/users/EdenChan/repos","events_url":"https://api.github.com/users/EdenChan/events{/privacy}","received_events_url":"https://api.github.com/users/EdenChan/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-06-06T08:58:32Z","updated_at":"2018-06-06T09:48:17Z","closed_at":null,"author_association":"NONE","body":"以 MySql 为例，向数据库执行 **任意一条语法有问题的 sql** 都会导致 \\Swoft\\Db\\Driver\\Mysql\\MysqlConnection::receive 方法抛出异常\r\n\r\n```php\r\n    public function receive()\r\n    {\r\n        $result = $this->connection->recv();\r\n        if ($result === false) {\r\n            throw new MysqlException('Mysql recv error，connectError=' . $this->connection->connect_error . ' error=' . $this->connection->error);\r\n        }\r\n        $this->connection->setDefer(false);\r\n\r\n        $this->recv   = true;\r\n        $this->result = $result;\r\n\r\n        return $result;\r\n    }\r\n```\r\n\r\n此时抛出的异常会走到事件监听器 \\Swoft\\Event\\Listeners\\ResourceReleaseListener 中，这里面的 handle 方法是没有进行 try catch 处理的，导致当前 worker 进程直接退出\r\n\r\n```\r\n[2018-06-06 16:41:05 *200.0]    ERROR   zm_deactivate_swoole (ERROR 503): Fatal error: Uncaught Exception: connection recv error in /var/www/swoft/vendor/swoft/framework/src/Event/Listeners/ResourceReleaseListener.php:48\r\nStack trace:\r\n#0 /var/www/swoft/vendor/swoft/framework/src/Aop/Aop.php(57): Swoft\\Event\\Listeners\\ResourceReleaseListener->handle(Object(Swoft\\Event\\Event))\r\n#1 /var/www/swoft/vendor/swoft/framework/src/Proxy/Handler/AopHandler.php(43): Swoft\\Aop\\Aop->execute(Object(Swoft\\Event\\Listeners\\ResourceReleaseListener), 'handle', Array)\r\n\r\n[2018-06-06 16:41:05 $195.0]    WARNING swManager_check_exit_status: worker#0 abnormal exit, status=255, signal=0\r\n```\r\n\r\n这样应该会产生比较严重的问题，现在疑问是在这些协程客户端中抛出异常是正确的做法吗？\r\n问题参考：https://github.com/swoole/swoole-src/issues/1598\r\n\r\n稍微看了下 SwooleDistributed 框架，它在协程客户端中貌似是通过 return Exception 的方式去处理异常，而不是直接 throw 的。\r\n\r\n有尝试过在 \\Swoft\\Db\\Driver\\Mysql\\MysqlConnection::receive 方法中返回异常，这样在 \\Swoft\\Core\\AbstractResult::recv 方法中能拿到 return 的 Exception，进行另外的处理。"},"comment":{"url":"https://api.github.com/repos/swoft-cloud/swoft/issues/comments/395011781","html_url":"https://github.com/swoft-cloud/swoft/issues/269#issuecomment-395011781","issue_url":"https://api.github.com/repos/swoft-cloud/swoft/issues/269","id":395011781,"node_id":"MDEyOklzc3VlQ29tbWVudDM5NTAxMTc4MQ==","user":{"login":"huangzhhui","id":12077148,"node_id":"MDQ6VXNlcjEyMDc3MTQ4","avatar_url":"https://avatars1.githubusercontent.com/u/12077148?v=4","gravatar_id":"","url":"https://api.github.com/users/huangzhhui","html_url":"https://github.com/huangzhhui","followers_url":"https://api.github.com/users/huangzhhui/followers","following_url":"https://api.github.com/users/huangzhhui/following{/other_user}","gists_url":"https://api.github.com/users/huangzhhui/gists{/gist_id}","starred_url":"https://api.github.com/users/huangzhhui/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/huangzhhui/subscriptions","organizations_url":"https://api.github.com/users/huangzhhui/orgs","repos_url":"https://api.github.com/users/huangzhhui/repos","events_url":"https://api.github.com/users/huangzhhui/events{/privacy}","received_events_url":"https://api.github.com/users/huangzhhui/received_events","type":"User","site_admin":false},"created_at":"2018-06-06T09:48:17Z","updated_at":"2018-06-06T09:48:17Z","author_association":"MEMBER","body":"首先异常只有抛出一种做法，没有return的做法，然后这个问题应该是 ResourceReleaseListener  在进行连接自动回收时没处理妥善导致的，但是收包动作应当存在于业务代码中的，你的业务代码是怎样的呢？"}}
{"actor":{"display_login":"huangzhhui"}}