Bug 40006 - Socket.ConnectAsync() bug
Summary: Socket.ConnectAsync() bug
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: System (show other bugs)
Version: 4.2.0 (C6)
Hardware: All All
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-03-30 23:46 UTC by Gutemberg
Modified: 2016-09-28 15:39 UTC (History)
4 users (show)

See Also:
Tags:
Is this bug a regression?: ---
Last known good build:


Attachments

Description Gutemberg 2016-03-30 23:46:16 UTC
Hi!

I'm trying to use System.Net.Sockets.Socket.ConnectAsync(SocketAsyncEventArgs) however, looks like the Completed event/callback is never fired.

Here is the code:

class Program
    {
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            var socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
            var sa = new SocketAsyncEventArgs();
            sa.Completed += Sa_Completed;
            sa.RemoteEndPoint = new IPEndPoint(IPAddress.Parse("192.168.150.142"), 8007);
            var result = socket.ConnectAsync(sa);
            Console.WriteLine($"[ConnectAsync] {result}");
            Console.WriteLine($"[Socket Connected] {socket.Connected}");
            Console.WriteLine($"[Socket Error] {sa.SocketError}");
            Console.ReadLine();
        }

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Console.WriteLine($"[ERROR] {e.ExceptionObject.ToString()}");
        }

        private static void Sa_Completed(object sender, SocketAsyncEventArgs e)
        {
            Console.WriteLine($"[Callback] {e.LastOperation}");
        }
    }

The callback isn't never called and the output will be:

[ConnectAsync] False
[Socket Connected] False
[Socket Error] Success

If I use TcpClient methods async and sync both works however, the XXXAsync methods from TcpClient are calling XXXBegin/XXXEnd wrapped on Tasks underneath, so the SocketAsyncEventArgs isn't used in TcpClient. This problem doesn't have on .Net on Windows...

Can someone confirm that it is a bug or are we doing something wrong? It's really critical and a major blocker for us. Please note that we are using a linux but the same code will run on Xamarin iOS/Android and I wonder if we will have the same problem as well. 

Thanks!
Comment 1 Jon Purdy 2016-04-04 21:29:44 UTC
It’s worth noting that ConnectAsync() returns false when the operation has completed synchronously, so the Completed event will not fire [1]. However, I am interested in why we have different behaviour from .NET here.

[1]: https://msdn.microsoft.com/en-us/library/bb538102(v=vs.110).aspx
Comment 2 Gutemberg 2016-04-05 02:13:08 UTC
I agree with you Jon that the events would not fire if it returns false however, socket.Connected should in case of synchronous mode, return true but it doesn't.
Comment 3 Warren 2016-09-28 15:39:22 UTC
This is definitely a bug.  I'm not aware of a real workaround, either which makes this whole API unusable.

The problem is that ConnectAsync() is returning false in all cases.  It should return true if the event will fire and false if it will not.

Because it always returns false (which is wrong) and then also fires the event, code that is written correctly will end up trying to handle the CONNECTs twice.

Note You need to log in before you can comment on or make changes to this bug.