Skip to content

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

Open
@chunhei2008

Description

@chunhei2008
  • 问题代码&修复方案

// 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;
                    }
                }
            }
        });
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions