ReactPHP libevent 和 socket_pair 抛出错误

ReactPHP libevent and socket_pair throws error

我遇到问题,然后我将 ReactPHP 事件循环与 stream_socket_pair 结合使用。

使用 StreamSelect 循环,它工作得很好,但后来我尝试将它与 LibEvent 扩展一起使用,我得到了无限的错误流:

[warn] kevent: Bad file descriptor

重现此错误的最少代码

<?php

require_once __DIR__ . '/vendor/autoload.php';

$loop = React\EventLoop\Factory::create();

$sockets = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);

$pid = pcntl_fork();
if ($pid > 0) {
    fclose($sockets[1]);

    $loop->addReadStream($sockets[0], function ($socket) {
        $data = fread($socket, 100000);
        echo '[' . posix_getpid() . '] Master: ' . $data . PHP_EOL;
    });

    $loop->addTimer(1, function () use ($sockets) {
        fwrite($sockets[0], 'Hello slave!');
    });

    $loop->run();


} else {
    fclose($sockets[0]);

    $loop->addReadStream($sockets[1], function ($socket) {
        $data = fread($socket, 100000);

        echo '[' . posix_getpid() . '] Slave: ' . $data . PHP_EOL;
    });

    $loop->addTimer(1, function () use ($sockets) {
        fwrite($sockets[1], 'Hello master!');
    });

    $loop->run();
    exit();
}

pcntl_wait($status);

它与 socket_pair 无关,但与处理分叉有关。 LibEvent 指出:

Some event mechanisms do not survive across fork. The event_base needs to be reinitialized with this function (event_base_reinit).

在 fork 之后添加 "event_base_reinit" 之后一切正常