Broken Redis session handler with unix socket, can't login
commit 83ea69d54bea72e1df9a2647ee101c73deb86d44 on Apr 26, 2019 (Add Redis session handler) is not valid if using redis with unix socket.
if setting environment
- REDIS_HOST=/var/run/redis/redis.sock
- REDIS_PORT=0
code in entrypoint.sh is setting a tcp configuration:
echo "Configuring Redis as session handler"
{
echo 'session.save_handler = redis'
echo "session.save_path = \"tcp://${REDIS_HOST}:${REDIS_HOST_PORT:=6379}\""
} > /usr/local/etc/php/conf.d/redis-session.ini
this produce "Failed to start session" error when accessing to nextcloud:
{"reqId":"LnNxDDhkJ5TOCSOBw8PL","level":3,"time":"2019-04-29T19:56:23+00:00","remoteAddr":"192.168.2.10","user":"--","app":"base","method":"GET","url":"/","message":{"Exception":"Exception","Message":"Failed to start session","Code":0,"Trace":[{"file":"/var/www/html/lib/base.php","line":424,"function":"__construct","class":"OC\Session\Internal","type":"->","args":["ocmj3i1peglp"]},{"file":"/var/www/html/lib/base.php","line":642,"function":"initSession","class":"OC","type":"::","args":[]},{"file":"/var/www/html/lib/base.php","line":1056,"function":"init","class":"OC","type":"::","args":[]},{"file":"/var/www/html/index.php","line":40,"args":["/var/www/html/lib/base.php"],"function":"require_once"}],"File":"/var/www/html/lib/private/Session/Internal.php","Line":61,"CustomMessage":"--"},"userAgent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0","version":""}
I assume you have a shared volume for the socket. Do you have any use case for this? The Docker concept usually makes heavy use of networks.
Yes, I used a shared volume.
All the nexcloud service is made in a single composite container, and only fpm or proxy requires an assigned public port. Why not have all the communication inside the container (redis, mariadb and fpm) through a Unix socket and eliminate all the TCP stack overload?. Doing this just to try, it's not really a real case.
I discovered that there is also another file to patch, but that in the Nextcloud Server Code, in RedisFactory.php, for the socket connection to work, only the form connect($ host); is valid, so a conditional test is required. I think, something like next code can be enought:
entrypoint.sh
echo "Configuring Redis as session handler"
{
echo 'session.save_handler = redis'
if [ "${REDIS_HOST_PORT:-0}" -eq 0 ]; then
echo "session.save_path = \"unix://${REDIS_HOST}?persistent=1&weight=1&database=0\""
else
echo "session.save_path = \"tcp://${REDIS_HOST}:${REDIS_HOST_PORT:=6379}\""
fi
} > /usr/local/etc/php/conf.d/redis-session.ini
RedisFactory.php
if (empty($port))
$this->instance->connect($host);
else
$this->instance->connect($host, $port, $timeout);
Would you like to open a PR? Please report/fix the upstream issue in https://github.com/nextcloud/docker/.
My suggested code may work, but it can be done better. Actually connect($host, $port, $timeout); works fine if $port is not set, so RedisFactory really does not need to be patched, but even if REDIS_PORT env var is not set, $port is being set, it must be find where this bug is.
Also, default redis port it's not needed to be set for the tcp case, phpredis/redis.c set it, if not a unix socket, and port it is not provided.
/* If it's not a unix socket, set to default */
if(port == -1 && host_len && host[0] != '/') {
port = 6379;
}
docker config/redis.config.php must be fixed, it is setting port.
<?php
if (getenv('REDIS_HOST')) {
$CONFIG = array (
'memcache.distributed' => '\OC\Memcache\Redis',
'memcache.locking' => '\OC\Memcache\Redis',
'redis' => array(
'host' => getenv('REDIS_HOST'),
'port' => getenv('REDIS_HOST_PORT') ?: 6379,
),
);
}
#944