none
[.NET][WSL] SocketAsyncEventArgs.Completed does not fire when running on WSL RRS feed

  • Question

  • I'm trying to establish a connection asynchronously using System.Net.Sockets.Socket class.

    I've created a simple .net core 2.0 project to test this.

    • it works fine on Windows 10
    • no problem in real Linux (RHEL7)
    • but it fails on WSL - the "Completed" event never fires

    Is there a problem in my code? (In fact, this is how CassandraCSharpDriver actually works. That's the original problem)

    Or is it a WSL bug?

    If so, is it possible to avoid it?

    static void Test(string host, int port)
    {
        Console.WriteLine($"{host}:{port}");
    
        var endPoint = new IPEndPoint(IPAddress.Parse(host), port);
    
        var socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
        socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
        socket.NoDelay = true;
        socket.SendTimeout = 5000;
    
        var eventArgs = new SocketAsyncEventArgs { RemoteEndPoint = endPoint };
    
        var tcs = new TaskCompletionSource<bool>();
    
        eventArgs.Completed += (sender, e) =>
        {
            // 
            // NEVER FIRES WHEN RUNNING ON WSL
            //
            Console.Write("Request completed ");
            var succeeded = true;
            if (e.SocketError != SocketError.Success)
            {
                succeeded = false;
                Console.WriteLine($"with error {e.SocketError}");
            }
            else
            {
                Console.WriteLine($"successfully");
            }
            tcs.TrySetResult(succeeded);
        };
    
        bool res = socket.ConnectAsync(eventArgs);
    
        Console.WriteLine("Request sent");
    
        tcs.Task.Wait();
        Console.WriteLine("Done");
    }

    Tuesday, September 26, 2017 6:27 AM

All replies