docker icon indicating copy to clipboard operation
docker copied to clipboard

Broken Redis session handler with unix socket, can't login

Open epma01 opened this issue 7 years ago • 6 comments

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":""}

epma01 avatar Apr 29 '19 20:04 epma01

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.

J0WI avatar Apr 30 '19 19:04 J0WI

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);

epma01 avatar Apr 30 '19 22:04 epma01

Would you like to open a PR? Please report/fix the upstream issue in https://github.com/nextcloud/docker/.

J0WI avatar Apr 30 '19 22:04 J0WI

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;
    }

epma01 avatar May 01 '19 09:05 epma01

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,
    ),
  );
}

epma01 avatar May 01 '19 09:05 epma01

#944

J0WI avatar Jan 10 '20 23:01 J0WI