I have redis installed on Ubuntu WSL (Ubuntu 16.04.5 LTS) and when I do redis-cli it gives me the following.

daveomcd@mcdonald-PC9020:~$ redis-cli
Could not connect to Redis at 127.0.0.1:6379: Connection refused
127.0.0.1:6379> ping
PONG
127.0.0.1:6379>

/var/log/redis/redis-server.log

I can execute commands fine but there's a message first saying the connection was refused. How can i figure out what is causing this and further how to get it to stop? I think it may be related to why my app is crashing in development. Here is the log in case it's helpful. I'll gladly add any more information if requested.

daveomcd@mcdonald-PC9020:~$ cat /var/log/redis/redis-server.log
221:C 01 Jan 2019 16:55:30.166 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
221:C 01 Jan 2019 16:55:30.166 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=221, just started
221:C 01 Jan 2019 16:55:30.166 # Configuration loaded
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 5.0.3 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 222
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

222:M 01 Jan 2019 16:55:30.204 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
222:M 01 Jan 2019 16:55:30.204 # Server initialized
222:M 01 Jan 2019 16:55:30.205 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
222:M 01 Jan 2019 16:55:30.205 * Ready to accept connections
daveomcd@mcdonald-PC9020:~$

Comment From: trevor211

Can you reproduct it using the latest version?

Comment From: npel-21

I am having this issue on a Windows 10 Surface Pro using WSL 2 with Ubuntu 16.04. Sometimes my Python application (which is not running in WSL) has no issue connecting, but sometimes I get connection refused and I can't figure out why.

I can confirm Redis is running in Ubuntu: USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND redis 34 0.2 0.8 137160 107336 ? Ssl 16:32 0:01 /usr/bin/redis-server 127.0.0.1:6379

Here's the Python program's error: Traceback (most recent call last): File "C:\Program Files (x86)\Python\lib\site-packages\redis\connection.py", line 559, in connect sock = self._connect() File "C:\Program Files (x86)\Python\lib\site-packages\redis\connection.py", line 615, in _connect raise err File "C:\Program Files (x86)\Python\lib\site-packages\redis\connection.py", line 603, in _connect sock.connect(socket_address) ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

Also I can use redis-cli with no issues in WSL Ubuntu. Could this be a Windows firewall problem?

Comment From: delyanr

I ran into the same problem as @npel-21 on WSL 2 with Ubuntu 20.04 LTS. It occurs totally randomly (or is it?) where the majority of the time my python app connects to redis, but occasionally fails. If it does fail, the only solution seems to be restarting Windows.

Comment From: trevor211

From the trackback that @npel-21 provided, I think the client did not send a command for a relatively long time, which caused redis server closing that connection actively. Note that redis server can actively close a connection after a client is idle for N seconds. FYI https://github.com/redis/redis/blob/2f41a3856845265ffc6cc3a35524883a8690cff7/redis.conf#L119.