php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62549 Broken persistent connections
Submitted: 2012-07-13 12:34 UTC Modified: 2012-07-18 09:13 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: dyeldandi at gramant dot ru Assigned: pdezwart (profile)
Status: Closed Package: amqp (PECL)
PHP Version: 5.4.4 OS: Fedora 16
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: dyeldandi at gramant dot ru
New email:
PHP Version: OS:

 

 [2012-07-13 12:34 UTC] dyeldandi at gramant dot ru
Description:
------------
When a connection is created by pconnect, and subsequently broken (server restart) it is not possible to recover that connection. The script would throw

AMQPChannelException' with message 'Server connection error: 320, message: CONNECTION_FORCED - broker forced connection closure with reason 'shutdown'

and

'AMQPChannelException' with message 'Library error: Broken pipe'

from new AMQPChannel($cn)

While it is possible to catch it, it's not possible to recover the connection. $cn->isConnected() would still return true, and $cn->disconnect() would do nothing.

I wrote a quick patch adding pdisconnect() that deletes the connection from persistent_list zend_hash, marks it not persistent and calls php_amqp_disconnect().

Test script:
---------------
Script with disconnect() (which doesn't work)


<?php
header("Content-type: text/plain");
$cn = new AMQPConnection();
$cn->setHost('localhost');
if(!$cn->pconnect()) {
        echo "AMQP: Could not create connection.\n";
        exit();
}
if ($cn->isConnected()) echo "It thinks it's connected\n";
else echo "It thinks it's disconnected\n";
try {
        $ch = new AMQPChannel($cn);
        $ex = new AMQPExchange($ch);
} catch(AMQPChannelException $e) {
        echo "Re-creating connection because of $e\n";
        $cn->disconnect();
        if(!$cn->pconnect()) {
                echo "AMQP: Could not re-create connection, give up\n";
                exit();
        }
        if ($cn->isConnected()) echo "It thinks it's connected\n";
        else echo "It thinks it's disconnected\n";
        $ch = new AMQPChannel($cn);
        $ex = new AMQPExchange($ch);
}
if(!$ex->publish('Hallo!', 'key')) {
        echo "Error: Message '$text' was not sent.\n";
        exit();
}

echo "Sent\n";


Script with pdisconnect() (which seems to be working)

<?php
header("Content-type: text/plain");
$cn = new AMQPConnection();
$cn->setHost('localhost');
if(!$cn->pconnect()) {
        echo "AMQP: Could not create connection.\n";
        exit();
}
if ($cn->isConnected()) echo "It thinks it's connected\n";
else echo "It thinks it's disconnected\n";
try {
        $ch = new AMQPChannel($cn);
        $ex = new AMQPExchange($ch);
} catch(AMQPChannelException $e) {
        echo "Re-creating connection because of $e\n";
        $cn->pdisconnect();
        if(!$cn->pconnect()) {
                echo "AMQP: Could not re-create connection, give up\n";
                exit();
        }
        if ($cn->isConnected()) echo "It thinks it's connected\n";
        else echo "It thinks it's disconnected\n";
        $ch = new AMQPChannel($cn);
        $ex = new AMQPExchange($ch);
}
if(!$ex->publish('Hallo!', 'key')) {
        echo "Error: Message '$text' was not sent.\n";
        exit();
}

echo "Sent\n";


Expected result:
----------------
It thinks it's connected
Sent

... restart rabbitmq server ...

It thinks it's connected
Re-creating connection because of exception 'AMQPChannelException' with message 'Server connection error: 320, message: CONNECTION_FORCED - broker forced connection closure with reason 'shutdown'' in /var/www/html/test_good.php:12
Stack trace:
#0 /var/www/html/test_good.php(12): AMQPChannel->__construct(Object(AMQPConnection))
#1 {main}
It thinks it's connected
Sent

Actual result:
--------------
It thinks it's connected
Sent

... restart rabbitmq server ...

It thinks it's connected
Re-creating connection because of exception 'AMQPChannelException' with message 'Server connection error: 320, message: CONNECTION_FORCED - broker forced connection closure with reason 'shutdown'' in /var/www/html/test_bad.php:12
Stack trace:
#0 /var/www/html/test_bad.php(12): AMQPChannel->__construct(Object(AMQPConnection))
#1 {main}
It thinks it's connected

... in error log: ...

[Fri Jul 13 16:25:48 2012] [error] [client 127.0.0.1] PHP Fatal error:  Uncaught exception 'AMQPChannelException' with message 'Library error: Broken pipe' in /var/www/html/test_bad.php:23\nStack trace:\n#0 /var/www/html/test_bad.php(23): AMQPChannel->__construct(Object(AMQPConnection))\n#1 {main}\n  thrown in /var/www/html/test_bad.php on line 23



Patches

amqp_pdisconnect.patch (last revision 2012-07-13 12:34 UTC by dyeldandi at gramant dot ru)

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-07-18 09:13 UTC] pdezwart@php.net
Thank you for your bug report. This issue has already been fixed
in the latest released version of PHP, which you can download at 
http://www.php.net/downloads.php

Fixed in 1.0.4. Thanks for the help!
 [2012-07-18 09:13 UTC] pdezwart@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: pdezwart
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 17:01:33 2025 UTC