Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

设置了exception callback 可能会死循环 #64

Open
chunhei2008 opened this issue Apr 16, 2022 · 2 comments · May be fixed by #65
Open

设置了exception callback 可能会死循环 #64

chunhei2008 opened this issue Apr 16, 2022 · 2 comments · May be fixed by #65

Comments

@chunhei2008
Copy link

chunhei2008 commented Apr 16, 2022

  • 问题代码&修复方案

// SwooleClient.php

   private function startRecvCo(): void
    {
        $this->coRecvRunning = true;
        $this->recvCoId = true;
        $this->recvCoId = Coroutine::create(function () {
            while ($this->coRecvRunning) {
                try {
                    $data = $this->socket->recv(4, -1);
                    if ($data === '') {
                        break;
                    }
                    $length = Int32::unpack($data);
                    $data = $this->socket->recv($length);
                    $correlationId = Int32::unpack($data);
                    if (isset($this->recvChannels[$correlationId])) {
                        $this->recvChannels[$correlationId]->push($data);
                    }
                } catch (Exception $e) {
                    if ($e instanceof SocketException && ! $this->connected) {
                        return;
                    }
                    $callback = $this->getConfig()->getExceptionCallback();
                    if ($callback) {
                        //  已经设置了 exception callback 没有退出就会一直死循环下去
                        $callback($e);
                        // 修复方案:在此次增加 return 
                        return;
                    } else {
                        throw $e;
                    }
                }
            }
        });
    }
@chunhei2008 chunhei2008 changed the title 设置了exception callback 依然throw exception 设置了exception callback 会死循环 Apr 16, 2022
@chunhei2008 chunhei2008 changed the title 设置了exception callback 会死循环 设置了exception callback 可能会死循环 Apr 16, 2022
chunhei2008 added a commit to chunhei2008/phpkafka that referenced this issue Apr 16, 2022
修复 swoole client starRecvCo 设置了exception callback 时会死循环

✅ Closes: swoole#64
@chunhei2008 chunhei2008 linked a pull request Apr 16, 2022 that will close this issue
@shuifa
Copy link

shuifa commented Oct 21, 2022

  • 问题代码&修复方案

// SwooleClient.php

   private function startRecvCo(): void
    {
        $this->coRecvRunning = true;
        $this->recvCoId = true;
        $this->recvCoId = Coroutine::create(function () {
            while ($this->coRecvRunning) {
                try {
                    $data = $this->socket->recv(4, -1);
                    if ($data === '') {
                        break;
                    }
                    $length = Int32::unpack($data);
                    $data = $this->socket->recv($length);
                    $correlationId = Int32::unpack($data);
                    if (isset($this->recvChannels[$correlationId])) {
                        $this->recvChannels[$correlationId]->push($data);
                    }
                } catch (Exception $e) {
                    if ($e instanceof SocketException && ! $this->connected) {
                        return;
                    }
                    $callback = $this->getConfig()->getExceptionCallback();
                    if ($callback) {
                        //  已经设置了 exception callback 没有退出就会一直死循环下去
                        $callback($e);
                        // 修复方案:在此次增加 return 
                        return;
                    } else {
                        throw $e;
                    }
                }
            }
        });
    }

你这没有解决吧。如果callback死循环了。还是走不到return啊

@shuifa
Copy link

shuifa commented Oct 21, 2022

  • 问题代码&修复方案

// SwooleClient.php

   private function startRecvCo(): void
    {
        $this->coRecvRunning = true;
        $this->recvCoId = true;
        $this->recvCoId = Coroutine::create(function () {
            while ($this->coRecvRunning) {
                try {
                    $data = $this->socket->recv(4, -1);
                    if ($data === '') {
                        break;
                    }
                    $length = Int32::unpack($data);
                    $data = $this->socket->recv($length);
                    $correlationId = Int32::unpack($data);
                    if (isset($this->recvChannels[$correlationId])) {
                        $this->recvChannels[$correlationId]->push($data);
                    }
                } catch (Exception $e) {
                    if ($e instanceof SocketException && ! $this->connected) {
                        return;
                    }
                    $callback = $this->getConfig()->getExceptionCallback();
                    if ($callback) {
                        //  已经设置了 exception callback 没有退出就会一直死循环下去
                        $callback($e);
                        // 修复方案:在此次增加 return 
                        return;
                    } else {
                        throw $e;
                    }
                }
            }
        });
    }

我到是觉得这里的 创建协程用的是 Swoole\Coroutine ,如果没有设置callback 就会抛出异常。。这样会导致php的致命错误。当前进程会被kiil。这里是不合理的。。。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants