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

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

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report for Bug 40006 on GitHub or Developer Community if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: GitHub Markdown or Developer Community HTML
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

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

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(""), 8007);
            var result = socket.ConnectAsync(sa);
            Console.WriteLine($"[ConnectAsync] {result}");
            Console.WriteLine($"[Socket Connected] {socket.Connected}");
            Console.WriteLine($"[Socket Error] {sa.SocketError}");

        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. 

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.