php-imap icon indicating copy to clipboard operation
php-imap copied to clipboard

PHP Warning stream_set_timeout() expects parameter 1 to be resource, bool given after connection issue

Open dartrax opened this issue 4 years ago • 2 comments

Describe the bug Most of the time it works as expected. But, from time to time I see this warnings:

PHP Warning:  stream_socket_client(): SSL: Connection reset by peer in /var/www/vhosts/.../imap_idle/Connection/Protocols/Protocol.php on line 191
PHP Warning:  stream_socket_client(): Failed to enable crypto in /var/www/vhosts/.../imap_idle/Connection/Protocols/Protocol.php on line 191
PHP Warning:  stream_socket_client(): unable to connect to ssl://wipper.bitpalast.net:993 (Unknown error) in /var/www/vhosts/.../imap_idle/Connection/Protocols/Protocol.php on line 191
PHP Warning:  stream_set_timeout() expects parameter 1 to be resource, bool given in /var/www/vhosts/.../imap_idle/Connection/Protocols/Protocol.php on line 193

Used config default config.

Expected behavior A way to handle this warning like trying to reopen the connection? At least I think $stream should be checked (like in Line 195) before stream_set_timeout is called in Line 193.

Desktop / Server (please complete the following information): PHP v 7.4.20 Version v 2.7.1

dartrax avatar Oct 16 '21 12:10 dartrax

Hi @dartrax , many thanks for your suggestion. I've just took a look at it. I suspect it also fails within ImapProtocol::connect method in L74 ? https://github.com/Webklex/php-imap/blob/2b5dda2e83a27d1b78098b880f84c0efcdc0e822/src/Connection/Protocols/ImapProtocol.php#L74

How would you reopen the connection there? Run a loop like this?

$retries = 10;
$try = 0;
$timeout = 30;
do {
    try {
        $this->stream = $this->createStream($transport, $host, $port, $this->connection_timeout);
    }catch(\Exception $e){
        sleep($timeout);
    }
} while (!$this->stream && $retries > $try++);

..but this would only work for exceptions and not for warnings etc. So we would have to explore general error handling: https://stackoverflow.com/questions/1241728/can-i-try-catch-a-warning I'm not sure that this is a good idea, wouldn't this potentially break things downstream in other libraries which already handle such errors?

Best regards,

Webklex avatar Nov 03 '21 20:11 Webklex

I suspect it also fails within ImapProtocol::connect method in L74 ?

Yes, I searched the logs and found the corresponding "Exception: connection failed" entry that was generated by the surrounding try-catch.

As for the details, I've just started using PHP in my projects, so I'm not very experienced especially when it comes to error handling. But I still think it would be good to catch those warnings and raise an exception that can be handled by the code of the project that uses your library and may be even trying to reconnect for a limited time like you proposed before ultimately raising that exception. Shouldn't something like this work? Could that break anything in the underlying libraries? https://stackoverflow.com/a/51091503

dartrax avatar Nov 12 '21 22:11 dartrax