Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Predis is giving 'Error while reading line from server'

Tags:

I am using predis, it's subscribed to a channel and listening. It throws the following error (below) and dies after 60 secs exactly. It's surely not my web servers error or its timeout.

There is a similar issue being discussed here. Could not get much of it.

I tried setting connection_timeout in predis conf file to 0, but doesn't helps much.

Also if i keep using (send data to it and it processes) the worker it doesn't give any error. So its likely a timeout somewhere, and that too in connection.

Here is my code snippet, which is likely producing error, because if data is given to worker it runs this code and go forward, which produces no error after that.

$pubsub = $redis->pubSub(); $pubsub->subscribe($channel1);  foreach ($pubsub as $message) { //doing stuff here and unsubscribing from channel } 

Trace

PHP Fatal error:  Uncaught exception 'Predis\Network\ConnectionException' with message 'Error while reading line from the server' in Predis/Network/ConnectionBase.php:159 Stack trace: #0 library/vendor/predis/lib/Predis/Network/StreamConnection.php(195): Predis\Network\ConnectionBase->onConnectionError('Error while rea...') #1 library/vendor/predis/lib/Predis/PubSub/PubSubContext.php(259): Predis\Network\StreamConnection->read() #2 library/vendor/predis/lib/Predis/PubSub/PubSubContext.php(206): Predis\PubSub\PubSubContext->getValue() #3 pdf/file.php(16): Predis\PubSub\PubSubContext->current() #4 {main}   thrown in Predis/Network/ConnectionBase.php on line 159 

Checked the redis.conf timeout too, its also disabled.

like image 361
amitchhajer Avatar asked Aug 02 '12 10:08

amitchhajer


2 Answers

Just set the read_write_timeout connection parameter to 0 or -1 to fix this. e.g.

$redis = new Predis\Client('tcp://10.0.0.1:6379'."?read_write_timeout=0"); 

Setting connection parameters is documented in the README. The author of Redis noted the relevance of the read_write_timeout parameter to this error in an issue on GitHub, in which he notes that:

If you are using Predis in a daemon-like script you should set read_write_timeout to -1 if you want to completely disable the timeout (this value works with older and newer versions of Predis). Also, remember that you must disable the default timeout of Redis by setting timeout = 0 in redis.conf or Redis will drop the connection of idle clients after 300 seconds of inactivity.

like image 137
amitchhajer Avatar answered Sep 21 '22 10:09

amitchhajer


I had similar problem, better solution to the situation is not setting the timeout to 0 but using a exponential backoff and set the upper and the lower limit. Change in the config parameter connection_timeout to 0 will also solve the issue.

like image 26
Jayesh Pawar Avatar answered Sep 20 '22 10:09

Jayesh Pawar