I run redis-stack in a Docker container on my WSL2 instance:
mark@L-R910LPKW:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b6530cb6eecf docker.io/redis/redis-stack:latest /entrypoint.sh 17 hours ago Up 17 hours ago 0.0.0.0:6379->6379/tcp, 0.0.0.0:8001->8001/tcp redis-stack
mark@L-R910LPKW:~$
On the Windows Host itself I am able to navigate to http://localhost:8001
and access the Redis Insights GUI running in the same container. It shows the data just fine.
In WSL2 I can reach the Redis server no problem. I do not have the redis-cli client (except the one available in the container), so I will use the nc
command to run the Redis PING command:
mark@L-R910LPKW:~$ nc localhost 6379
ping
+PONG
^C
mark@L-R910LPKW:~$
On the Windows host I do not have a redis client and neither the nc tool. But I have git-bash and this works:
/c/dayforce/aida$ exec 3<>/dev/tcp/localhost/6379
/c/dayforce/aida$ echo -e "PING\r\n" >&3
/c/dayforce/aida$ cat <&3
+PONG
(Thank you https://www.baeldung.com/linux/redis-connection-without-redis-cli#bashs-built-in-devtcp-file)
And I also tried the INFO command from the git bash (running on the Windows host) and it works fine.
Now I am going to try to run a command using the StackExchange.Redis NuGet package. For that I have this small xUnit test (most of the code configures Redis logging to xUnit):
using StackExchange.Redis;
using System.Diagnostics.CodeAnalysis;
using System.Text;
namespace AidaApi.Tests.Connectors;
public class XUnitTextWriter(ITestOutputHelper testOutputHelper) : TextWriter
{
public override void WriteLine(string value)
{
testOutputHelper.WriteLine(value);
}
public override void WriteLine([StringSyntax("CompositeFormat")] string format, params object[] arg)
{
testOutputHelper.WriteLine(format, arg);
}
public override Encoding Encoding => Encoding.UTF8;
}
public class LocalRedis
{
private readonly IConnectionMultiplexer sut;
public LocalRedis(ITestOutputHelper testOutputHelper)
{
if (Environment.GetEnvironmentVariable("TF_BUILD") == null)
{
sut = ConnectionMultiplexer.Connect("localhost,abortConnect=false", new XUnitTextWriter(testOutputHelper));
}
}
[SkippableFact]
public void Ping()
{
Skip.If(sut == null, $"{GetType().Name} tests can only run locally.");
var db = sut.GetDatabase();
db.Ping();
}
}
And this thing fails:
C:\xyz\aida\aida-api\tests\AidaApi.Tests\bin\Debug\net8.0> vstest.console.exe AidaApi.Tests.dll /Tests:Ping
VSTest version 17.11.1 (x64)
Starting test discovery, please wait...
[xUnit.net 00:00:00.00] xUnit.net VSTest Adapter v2.8.1+ce9211e970 (64-bit .NET 8.0.8)
[xUnit.net 00:00:00.09] Discovering: AidaApi.Tests
[xUnit.net 00:00:00.16] Discovered: AidaApi.Tests
A total of 1 test files matched the specified pattern.
[xUnit.net 00:00:00.00] xUnit.net VSTest Adapter v2.8.1+ce9211e970 (64-bit .NET 8.0.8)
[xUnit.net 00:00:00.08] Starting: AidaApi.Tests
[xUnit.net 00:00:20.20] AidaApi.Tests.Connectors.LocalRedis.Ping [FAIL]
[xUnit.net 00:00:20.21] StackExchange.Redis.RedisConnectionException : The message timed out in the backlog attempting to send because no connection became available (5000ms) - Last Connection Exception: It was not possible to connect to the redis server(s). ConnectTimeout, command=PING, timeout: 5000, inst: 0, qu: 0, qs: 0, aw: False, bw: SpinningDown, rs: NotStarted, ws: Idle, in: 0, last-in: 0, cur-in: 0, sync-ops: 1, async-ops: 0, serverEndpoint: localhost:6379, conn-sec: n/a, aoc: 0, mc: 1/1/0, mgr: 10 of 10 available, clientName: L-R910LPKW(SE.Redis-v2.8.0.27420), IOCP: (Busy=0,Free=1000,Min=1,Max=1000), WORKER: (Busy=2,Free=32765,Min=12,Max=32767), POOL: (Threads=10,QueuedItems=0,CompletedItems=80,Timers=6), v: 2.8.0.27420 (Please take a look at this article for some common client-side issues that can cause timeouts: https://stackexchange.github.io/StackExchange.Redis/Timeouts)
[xUnit.net 00:00:20.21] ---- StackExchange.Redis.RedisConnectionException : It was not possible to connect to the redis server(s). ConnectTimeout
[xUnit.net 00:00:20.21] Stack Trace:
[xUnit.net 00:00:20.21] /_/src/StackExchange.Redis/ConnectionMultiplexer.cs(2105,0): at StackExchange.Redis.ConnectionMultiplexer.ExecuteSyncImpl[T](Message message, ResultProcessor`1 processor, ServerEndPoint server, T defaultValue)
[xUnit.net 00:00:20.21] /_/src/StackExchange.Redis/RedisBase.cs(62,0): at StackExchange.Redis.RedisBase.ExecuteSync[T](Message message, ResultProcessor`1 processor, ServerEndPoint server, T defaultValue)
[xUnit.net 00:00:20.21] /_/src/StackExchange.Redis/RedisBase.cs(24,0): at StackExchange.Redis.RedisBase.Ping(CommandFlags flags)
[xUnit.net 00:00:20.21] C:\xyz\aida\aida-api\tests\AidaApi.Tests\Connectors\LocalRedis.cs(38,0): at AidaApi.Tests.Connectors.LocalRedis.Ping()
[xUnit.net 00:00:20.21] at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
[xUnit.net 00:00:20.21] at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
[xUnit.net 00:00:20.21] ----- Inner Stack Trace -----
[xUnit.net 00:00:20.21]
[xUnit.net 00:00:20.21] Output:
[xUnit.net 00:00:20.21] Connecting (sync) on .NET 8.0.8 (StackExchange.Redis: v2.8.0.27420)
[xUnit.net 00:00:20.21] localhost,abortConnect=False
[xUnit.net 00:00:20.21] localhost:6379/Interactive: Connecting...
[xUnit.net 00:00:20.21] localhost:6379: BeginConnectAsync
[xUnit.net 00:00:20.21] 1 unique nodes specified (with tiebreaker)
[xUnit.net 00:00:20.21] localhost:6379: OnConnectedAsync init (State=Connecting)
[xUnit.net 00:00:20.21] Allowing 1 endpoint(s) 00:00:05 to respond...
[xUnit.net 00:00:20.21] Awaiting 1 available task completion(s) for 5000ms, IOCP: (Busy=0,Free=1000,Min=1,Max=1000), WORKER: (Busy=2,Free=32765,Min=12,Max=32767), POOL: (Threads=3,QueuedItems=0,CompletedItems=4,Timers=3)
[xUnit.net 00:00:20.21] Not all available tasks completed cleanly (from ReconfigureAsync#1499, timeout 5000ms), IOCP: (Busy=0,Free=1000,Min=1,Max=1000), WORKER: (Busy=5,Free=32762,Min=12,Max=32767), POOL: (Threads=7,QueuedItems=0,CompletedItems=13,Timers=1)
[xUnit.net 00:00:20.21] localhost:6379: OnConnectedAsync completed (Disconnected)
[xUnit.net 00:00:20.21] Server[0] (localhost:6379) Status: WaitingForActivation (inst: 0, qs: 0, in: 0, qu: 0, aw: False, in-pipe: -1, out-pipe: -1, bw: Inactive, rs: NotStarted. ws: Initializing)
[xUnit.net 00:00:20.21] Endpoint summary:
[xUnit.net 00:00:20.21] localhost:6379: Endpoint is (Interactive: Connecting, Subscription: Connecting)
[xUnit.net 00:00:20.21] Task summary:
[xUnit.net 00:00:20.21] localhost:6379: Returned, but incorrectly
[xUnit.net 00:00:20.21] Election summary:
[xUnit.net 00:00:20.21] Election: localhost:6379 had no tiebreaker set
[xUnit.net 00:00:20.21] Election: No primaries detected
[xUnit.net 00:00:20.21] Endpoint Summary:
[xUnit.net 00:00:20.21] localhost:6379: Standalone v3.0.0, primary; keep-alive: 00:01:00; int: Connecting; sub: Connecting; not in use: DidNotRespond
[xUnit.net 00:00:20.21] localhost:6379: int ops=0, qu=0, qs=0, qc=0, wr=0, socks=2; sub ops=0, qu=0, qs=0, qc=0, wr=0, socks=2
[xUnit.net 00:00:20.21] Connection failed: localhost:6379 (Interactive, UnableToConnect): UnableToConnect on localhost:6379/Interactive, Initializing/NotStarted, last: NONE, origin: BeginConnectAsync, outstanding: 0, last-read: 5s ago, last-write: 5s ago, keep-alive: 60s, state: Connecting, mgr: 10 of 10 available, last-heartbeat: never, global: 5s ago, v: 2.8.0.27420
[xUnit.net 00:00:20.21] Connection failed: localhost:6379 (Subscription, UnableToConnect): UnableToConnect on localhost:6379/Subscription, Initializing/NotStarted, last: NONE, origin: BeginConnectAsync, outstanding: 0, last-read: 5s ago, last-write: 5s ago, keep-alive: 60s, state: Connecting, mgr: 10 of 10 available, last-heartbeat: never, global: 5s ago, v: 2.8.0.27420
[xUnit.net 00:00:20.21] localhost:6379: Circular op-count snapshot; int: 0 (0.00 ops/s; spans 10s); sub: 0 (0.00 ops/s; spans 10s)
[xUnit.net 00:00:20.21] Sync timeouts: 0; async timeouts: 0; fire and forget: 0; last heartbeat: -1s ago
[xUnit.net 00:00:20.21] Resetting failing connections to retry...
[xUnit.net 00:00:20.21] Retrying - attempts left: 2...
[xUnit.net 00:00:20.21] 1 unique nodes specified (with tiebreaker)
[xUnit.net 00:00:20.21] localhost:6379: OnConnectedAsync init (State=Connecting)
[xUnit.net 00:00:20.21] Allowing 1 endpoint(s) 00:00:05 to respond...
[xUnit.net 00:00:20.21] Awaiting 1 available task completion(s) for 5000ms, IOCP: (Busy=0,Free=1000,Min=1,Max=1000), WORKER: (Busy=5,Free=32762,Min=12,Max=32767), POOL: (Threads=10,QueuedItems=0,CompletedItems=30,Timers=3)
[xUnit.net 00:00:20.21] Not all available tasks completed cleanly (from ReconfigureAsync#1499, timeout 5000ms), IOCP: (Busy=0,Free=1000,Min=1,Max=1000), WORKER: (Busy=3,Free=32764,Min=12,Max=32767), POOL: (Threads=10,QueuedItems=0,CompletedItems=40,Timers=1)
[xUnit.net 00:00:20.21] Server[0] (localhost:6379) Status: WaitingForActivation (inst: 0, qs: 0, in: -1, qu: 0, aw: False, in-pipe: -1, out-pipe: -1, bw: Inactive, rs: NA. ws: NA)
[xUnit.net 00:00:20.21] Endpoint summary:
[xUnit.net 00:00:20.21] localhost:6379: Endpoint is (Interactive: Disconnected, Subscription: Disconnected)
[xUnit.net 00:00:20.21] Task summary:
[xUnit.net 00:00:20.21] localhost:6379: Did not respond (Task.Status: WaitingForActivation)
[xUnit.net 00:00:20.21] Election summary:
[xUnit.net 00:00:20.21] Election: localhost:6379 had no tiebreaker set
[xUnit.net 00:00:20.21] Election: No primaries detected
[xUnit.net 00:00:20.21] Endpoint Summary:
[xUnit.net 00:00:20.21] localhost:6379: Standalone v3.0.0, primary; keep-alive: 00:01:00; int: Disconnected; sub: Disconnected; not in use: DidNotRespond
[xUnit.net 00:00:20.21] localhost:6379: int ops=0, qu=0, qs=0, qc=0, wr=0, socks=2; sub ops=0, qu=0, qs=0, qc=0, wr=0, socks=2
[xUnit.net 00:00:20.21] localhost:6379: Circular op-count snapshot; int: 0 (0.00 ops/s; spans 10s); sub: 0 (0.00 ops/s; spans 10s)
[xUnit.net 00:00:20.21] Sync timeouts: 0; async timeouts: 0; fire and forget: 0; last heartbeat: -1s ago
[xUnit.net 00:00:20.21] Resetting failing connections to retry...
[xUnit.net 00:00:20.21] Retrying - attempts left: 1...
[xUnit.net 00:00:20.21] 1 unique nodes specified (with tiebreaker)
[xUnit.net 00:00:20.21] localhost:6379: OnConnectedAsync init (State=Connecting)
[xUnit.net 00:00:20.21] Allowing 1 endpoint(s) 00:00:05 to respond...
[xUnit.net 00:00:20.21] Awaiting 1 available task completion(s) for 5000ms, IOCP: (Busy=0,Free=1000,Min=1,Max=1000), WORKER: (Busy=4,Free=32763,Min=12,Max=32767), POOL: (Threads=10,QueuedItems=0,CompletedItems=40,Timers=3)
[xUnit.net 00:00:20.21] Not all available tasks completed cleanly (from ReconfigureAsync#1499, timeout 5000ms), IOCP: (Busy=0,Free=1000,Min=1,Max=1000), WORKER: (Busy=7,Free=32760,Min=12,Max=32767), POOL: (Threads=10,QueuedItems=0,CompletedItems=49,Timers=1)
[xUnit.net 00:00:20.21] localhost:6379: OnConnectedAsync completed (Disconnected)
[xUnit.net 00:00:20.21] localhost:6379: OnConnectedAsync completed (Disconnected)
[xUnit.net 00:00:20.21] Server[0] (localhost:6379) Status: WaitingForActivation (inst: 0, qs: 0, in: -1, qu: 0, aw: False, in-pipe: -1, out-pipe: -1, bw: Inactive, rs: NA. ws: NA)
[xUnit.net 00:00:20.21] Endpoint summary:
[xUnit.net 00:00:20.21] localhost:6379: Endpoint is (Interactive: Disconnected, Subscription: Disconnected)
[xUnit.net 00:00:20.21] Task summary:
[xUnit.net 00:00:20.21] localhost:6379: Returned, but incorrectly
[xUnit.net 00:00:20.21] Election summary:
[xUnit.net 00:00:20.21] Election: localhost:6379 had no tiebreaker set
[xUnit.net 00:00:20.21] Election: No primaries detected
[xUnit.net 00:00:20.21] Endpoint Summary:
[xUnit.net 00:00:20.21] localhost:6379: Standalone v3.0.0, primary; keep-alive: 00:01:00; int: Disconnected; sub: Disconnected; not in use: DidNotRespond
[xUnit.net 00:00:20.21] localhost:6379: int ops=0, qu=0, qs=0, qc=0, wr=0, socks=3; sub ops=0, qu=0, qs=0, qc=0, wr=0, socks=3
[xUnit.net 00:00:20.21] localhost:6379: Circular op-count snapshot; int: 0 (0.00 ops/s; spans 10s); sub: 0 (0.00 ops/s; spans 10s)
[xUnit.net 00:00:20.21] Sync timeouts: 0; async timeouts: 0; fire and forget: 0; last heartbeat: -1s ago
[xUnit.net 00:00:20.21] Starting heartbeat...
[xUnit.net 00:00:20.21] Total connect time: 15,051 ms
[xUnit.net 00:00:20.22] Finished: AidaApi.Tests
Failed AidaApi.Tests.Connectors.LocalRedis.Ping [20 s]
Error Message:
...
Test Run Failed.
Total tests: 1
Failed: 1
Total time: 20.6085 Seconds
C:\xyz\aida\aida-api\tests\AidaApi.Tests\bin\Debug\net8.0>
(For some reason the stack trace and the captured output are printed twice - once with the [xUnit.net 00:00:...]
prefix and then without, so I replaced the latter with ellipsis for brevity)
I am new to Redis and it is totally unclear to me what is going on. Also, I am puzzled by the following output when running the unit test:
localhost:6379: Standalone v3.0.0, ...
I wonder what is the meaning of it, given that the INFO command reports the server version as 7.4.0
.
I replaced localhost
with 127.0.0.1
and it worked. So the question is now - what is the problem with localhost
in .NET, because it worked fine in Git Bash.
Posting my answer in case anyone else faces the same issue.
The root cause is localhost
resolution on my laptop. For some reason, it does not resolve to 127.0.0.1
when using Redis .NET client.
So, using 127.0.0.1 as the redis server works.