jakubkulhan / bunny

Performant pure-PHP AMQP (RabbitMQ) sync/async (ReactPHP) library
MIT License
698 stars 101 forks source link

Could not write data to socket after running Client::run for a while #108

Closed frankvanhest closed 3 years ago

frankvanhest commented 3 years ago

Good afternoon,

When I run Client::run for a while and it has been idle as in no messages being consumed I get a Could not write data to socket. error. What can I do to prevent this? If you need more informatie, let me know.

Thanks you in advance.

Cheers,

Frank

WyriHaximus commented 3 years ago

Hey @frankvanhest, did you configure a heartbeat? https://github.com/jakubkulhan/bunny/blob/9d6be4aba9172c69925dc9d137e12dc88f6861ee/test/Bunny/ClientTest.php#L311-L332

Seems I might want to add some documentation about heartbeats :zipper_mouth_face:

frankvanhest commented 3 years ago

Hi @WyriHaximus, thank you for your reply! I thought the default setting in AbstractClient was enough, but apparently this is 60 seconds en de default in rabbitmq is also 60 seconds. I'm guessing this can be somewhat of a close call race between them. I configured the heartbeat with 10 seconds and after half an hour of idle I was still able to publish a message and consuming it. But after using ctrl+c I got the following:

^C
In AbstractClient.php line 330:

  Could not write data to socket.

consumer:run

PHP Fatal error:  Uncaught Error: Call to a member function done() on null in /home/vagrant/code/Packages/dns-management/vendor/bunny/bunny/src/Bunny/Client.php:57
Stack trace:
#0 [internal function]: Bunny\Client->__destruct()
#1 {main}
  thrown in /home/vagrant/code/Packages/dns-management/vendor/bunny/bunny/src/Bunny/Client.php on line 57

Fatal error: Uncaught Error: Call to a member function done() on null in /home/vagrant/code/Packages/dns-management/vendor/bunny/bunny/src/Bunny/Client.php:57
Stack trace:
#0 [internal function]: Bunny\Client->__destruct()
#1 {main}
  thrown in /home/vagrant/code/Packages/dns-management/vendor/bunny/bunny/src/Bunny/Client.php on line 57

I could that this is by other code in the project. Is this something you have seen before? It could be because I have my own signal handler present to ensure the client is disconnected.

pcntl_async_signals(true);
foreach ([SIGINT, SIGTERM] as $signal) {
    pcntl_signal(
        $signal,
        function () use ($signal) {
            $this->client->disconnect();
            exit($signal);
        }
    );
}
frankvanhest commented 3 years ago

The last issue was because of my own signal handler. I came from php-amqp which needed this. I see that bunny has a signal handler implemented. Again thanks for you help @WyriHaximus! Keep up the good work.