Search code examples
c#async-awaitudpclient

C# await udpClient.ReceiveAsync() fails and terminates program


I'm running a C# console application with .NET 4.5.1. When I run the following function the udpClient.ReceiveAsync() call below silently terminates the program with no exception. How do I debug this?

   public async void Run()
    {
        try
        {
            var armIpAddress = IPAddress.Parse("239.1.11.1");
            using (var udpClient = new UdpClient())
            {
                udpClient.ExclusiveAddressUse = false;
                var ipEndPoint = new IPEndPoint(IPAddress.Any, 12020);
                udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                udpClient.ExclusiveAddressUse = false;
                udpClient.Client.Bind(ipEndPoint);
                udpClient.JoinMulticastGroup(armIpAddress);

                while (true)
                {
                    var result = await udpClient.ReceiveAsync();
                    //...
                }
        }
        catch (Exception x)
        {
            Console.WriteLine(x.Message);
        }
    }

Solution

  • The call to await udpClient.ReceiveAsync() is not terminating the program nor it is silently failing.

    Given that this is happening, I assume you have something like this:

    public static void Main(string[] args)
    {
        ...
        Run();
    }
    

    In an async void method, control is returned to the parent as soon as an await call is reached, so the flow of the program would be something like:

    public static void Main(string[] args)
    {
        ...
        var armIpAddress = IPAddress.Parse("239.1.11.1");
        using (var udpClient = new UdpClient())
        {
            udpClient.ExclusiveAddressUse = false;
            .......
    
            while (true)
            {
                return; 
            }
        }
    }
    

    So the program ends due to no further blocking code.

    For this to work as you would expect, change the code to this:

    public static void Main(string[] args)
    {
        ...
        Run().Wait();
    }
    
    public async Task Run() { ... }