Skip to content
Advertisement

Predis is giving ‘Error while reading line from server’

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 'PredisNetworkConnectionException' 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): PredisNetworkConnectionBase->onConnectionError('Error while rea...')
#1 library/vendor/predis/lib/Predis/PubSub/PubSubContext.php(259): PredisNetworkStreamConnection->read()
#2 library/vendor/predis/lib/Predis/PubSub/PubSubContext.php(206): PredisPubSubPubSubContext->getValue()
#3 pdf/file.php(16): PredisPubSubPubSubContext->current()
#4 {main}   thrown in Predis/Network/ConnectionBase.php on line 159

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

Advertisement

Answer

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

$redis = new PredisClient('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.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement