Skip to content

[BUG] hyperf/jet 在 recv 过程中对端服务 down 掉会出现死循环 #2970

@teg1c

Description

@teg1c
public function recv()
    {
        $buf = '';
        $timeout = 1000;

        stream_set_blocking($this->client, false);

        // The maximum number of retries is 12, and 1000 microseconds is the minimum waiting time.
        // The waiting time is doubled each time until the server writes data to the buffer.
        // Usually, the data can be obtained within 1 microsecond.
        return retry(12, function () use (&$buf, &$timeout) {
            $read = [$this->client];
            $write = null;
            $except = null;
            while (stream_select($read, $write, $except, 0, $timeout)) {
                foreach ($read as $r) {
                    $buf .= fread($r, 8192);
                }
            }

            if (! $buf) {
                $timeout *= 2;

                throw new RecvFailedException('No data was received');
            }

            return $buf;
        });
    }

stream_select 是在连接发生变化也会返回 1 ,也就是说客户端已经连接上,但是在接收数据的过程中对端断掉,那么

while (stream_select($read, $write, $except, 0, $timeout)) {
                foreach ($read as $r) {
                    $buf .= fread($r, 8192);
                }
            }

它还是要走到这个while里面,就会造成死循环

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions