Skip to content

UdpClient hangs when Receive is waiting and gets a Close() in macos #64551

Description

@jaimefinat

Hi
we are experiencing an issue in macos (Monterey) when closing a UdpClient socket from the server side.
Also related to previously detected issue #21392

  1. We create the UdpClient:
    mUdpClient = new UdpClient(listenPort);
  2. The UdpClient calls Receive method:
    byte[] bytes = mUdpClient.Receive(ref groupEP);
  3. While waiting to receive something from any client, the Close() method is called in another thread:
    mUdpClient.Close();

Attached to this, I'm sending a minimum version of a Server-Client application to reproduce the issue.

Expected behaviour:
In Windows it is rising an exception which allos us to stop the server:

System.Net.Sockets.SocketException (10004): A blocking operation was interrupted by a call to WSACancelBlockingCall.
   at System.Net.Sockets.Socket.ReceiveFrom(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, EndPoint& remoteEP)
   at System.Net.Sockets.UdpClient.Receive(IPEndPoint& remoteEP)
   at Server.StartListener() in C:\experiments\client-server\server\Server.cs:line 21

In macos it hangs in Receive until it gets a message from the client, so it processes the request and then triggers another exception:

Unhandled exception. System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'System.Net.Sockets.Socket'.
   at System.Net.Sockets.Socket.ThrowObjectDisposedException()
   at System.Net.Sockets.Socket.ReceiveFrom(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, EndPoint& remoteEP)
   at System.Net.Sockets.UdpClient.Receive(IPEndPoint& remoteEP)
   at Server.StartListener() in /Users/daniel.hompanera/wkspaces/UdpApp/Server/Server.cs:line 21
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   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.Threading.ThreadHelper.ThreadStart()
[1]    29462 abort      dotnet Server.dll

To reproduce the issue (different behaviour in macos/windows) download and build the solution from this post, start the server and type: efss to EndFromServerSide.

The code has been modified from the basic version here

Thank you very much for your kind support!
Jaime

UdpApp.zip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions