microsoft / garnet

Garnet is a remote cache-store from Microsoft Research that offers strong performance (throughput and latency), scalability, storage, recovery, cluster sharding, key migration, and replication features. Garnet can work with existing Redis clients.
https://microsoft.github.io/garnet/
MIT License
10.34k stars 521 forks source link

[UnitTest] GarnetServerTcp.HandleNewConnection throws SocketException while running on macOS #598

Open plucked opened 2 months ago

plucked commented 2 months ago

Describe the bug

Some unit tests are failing on macOS because of an exception while e.AcceptSocket.NoDelay = true; is called in GarnetServerTcp.HandleNewConnection. It looks like that on macOS, the prior check if (e.SocketError != SocketError.Success) is false when it should be true.

Replacing the code here to:

            try 
            {
                e.AcceptSocket.NoDelay = true;
            } 
            catch (SocketException exception) 
            {
                logger?.LogError(exception, "Error setting NoDelay on accepted socket");
                return false;
            }

Allows me to have all the unit tests that were failing because of this to pass.

Callstack:

Exit code is 134 (Unhandled exception. System.Net.Sockets.SocketException (22): Invalid argument
   at System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(SocketError error, Boolean disconnectOnFailure, String callerName)
   at System.Net.Sockets.Socket.UpdateStatusAfterSocketOptionErrorAndThrowException(SocketError error, String callerName)
   at Garnet.server.GarnetServerTcp.HandleNewConnection(SocketAsyncEventArgs e) in /Users/plucked/Development/garnet/libs/server/Servers/GarnetServerTcp.cs:line 121
   at Garnet.server.GarnetServerTcp.AcceptEventArg_Completed(Object sender, SocketAsyncEventArgs e) in /Users/plucked/Development/garnet/libs/server/Servers/GarnetServerTcp.cs:line 105
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Net.Sockets.SocketAsyncEventArgs.AcceptCompletionCallback(IntPtr acceptedFileDescriptor, Memory`1 socketAddress, SocketError socketError)
   at System.Net.Sockets.SocketAsyncEngine.System.Threading.IThreadPoolWorkItem.Execute()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()
   at System.Threading.Thread.StartCallback())

Steps to reproduce the bug

  1. Clone and run the unit tests

Expected behavior

The unit tests should pass

Screenshots

No response

Release version

main

IDE

Rider 2024.1.5

OS version

macOS 14.5

Additional context

No response

badrishc commented 2 months ago

Interesting, perhaps e.AcceptSocket.NoDelay = true; can be moved into the try that follows below that section, so that we do not pay for two try/catch sections.

badrishc commented 2 months ago

Interesting, perhaps e.AcceptSocket.NoDelay = true; can be moved into the try that follows below that section, so that we do not pay for two try/catch sections.

If you or anyone can try this and verify that it fixes the problem, feel free to create a PR.