Bug 42617 - Socket.ConnectAsync calling SocketAsyncEventArgs.Completed even when completing synchronously with an error
Summary: Socket.ConnectAsync calling SocketAsyncEventArgs.Completed even when completi...
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: 4.4.0 (C7)
Hardware: PC Windows
: Normal normal
Target Milestone: Future Release
Assignee: marcos.henrich
Depends on:
Reported: 2016-07-17 12:27 UTC by Jahmai
Modified: 2017-02-09 07:18 UTC (History)
7 users (show)

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

Sample Project (4.90 KB, application/zip)
2016-08-02 12:44 UTC, John Miller [MSFT]
Sample Project (14.35 KB, application/zip)
2016-08-03 12:22 UTC, John Miller [MSFT]

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 42617 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 Jahmai 2016-07-17 12:27:05 UTC
count should be 1, but actually it's 2. When ConnectAsync returns false, Completed is not meant to be called.

Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
SocketAsyncEventArgs e = new SocketAsyncEventArgs();
int count = 0;
e.RemoteEndPoint = new IPEndPoint(IPAddress.Any, 9999);
e.Completed += (sender, args) => Interlocked.Increment(ref count);

if (!socket.ConnectAsync(e))
    Interlocked.Increment(ref count);

Debug.Assert(count == 1);
Comment 1 Rodrigo Kumpera 2016-07-20 00:57:58 UTC
Hi Jahmai,

Could you provide a test case that demonstrate your issue?

A code snippet is not enough to fully understand what you're trying to do.
Comment 2 Jahmai 2016-07-20 01:00:21 UTC
You want me to copy paste that code into a project for you? No thanks.

The problem is clear, if ConnectAsync returns false, it should not fire SocketAsyncEventArgs.Completed.

We've already worked around the problem.
Comment 3 John Miller [MSFT] 2016-08-02 12:44:00 UTC
Created attachment 16862 [details]
Sample Project

Add a sample console project with the code from the report. 

I ran it and count is 1, so I am not sure if I am using the correct mono version or if the test case is bad. 

Mono 4.4.0 (mono-4.4.0-branch/fcf7a6d) (64-bit)
Comment 5 Jahmai 2016-08-02 12:56:37 UTC
I ran the test on an iPhone, so it is possible that the network stack behaves differently when trying to connect to IPAddress.Any.
Comment 6 John Miller [MSFT] 2016-08-03 12:22:48 UTC
Created attachment 16875 [details]
Sample Project

Attached a new sample project that includes an iOS app project with the same code. 

Both projects (console and iOS) reproduce the issue when using IPAddress.Any. The count is 2, and the expected is 1. 

=== Xamarin Studio Enterprise ===

Version 6.0.2 (build 73)
Installation UUID: e01c3049-a2d2-4e0a-aad8-afe6fb627c4d
	Mono 4.4.2 (mono-4.4.0-branch-c7sr1/f72fe45) (64-bit)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 404020011

=== Xamarin.Profiler ===

Not Installed

=== Apple Developer Tools ===

Xcode 7.3 (10183.3)
Build 7D175

=== Xamarin.Mac ===

Version: (Xamarin Enterprise)

=== Xamarin.Android ===

Version: (Xamarin Enterprise)
Android SDK: /Users/johnmiller/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		2.3   (API level 10)
		4.0.3 (API level 15)
		4.1   (API level 16)
		4.2   (API level 17)
		4.4   (API level 19)
		5.0   (API level 21)
		5.1   (API level 22)
		6.0   (API level 23)

SDK Tools Version: 25.1.7
SDK Platform Tools Version: 24.0.0
SDK Build Tools Version: 23.0.2

Java SDK: /usr
java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)

Android Designer EPL code available here:

=== Xamarin Android Player ===

Version: 0.6.5
Location: /Applications/Xamarin Android Player.app

=== Xamarin.iOS ===

Version: (Xamarin Enterprise)
Hash: f37444a
Branch: cycle7-sr1
Build date: 2016-07-28 12:17:02-0400

=== Xamarin Inspector ===

Hash: 4d868da
Branch: master
Build date: Mon Jun 13 19:14:13 UTC 2016

=== Build Information ===

Release ID: 600020073
Git revision: a6f7a24a9723a2d4f5d33c176615b0d44703ab5b
Build date: 2016-07-26 13:36:15-04
Xamarin addins: f5acb37866a0141bc5ddbe95118f18dae4014568
Build lane: monodevelop-lion-cycle7-sr1

=== Operating System ===

Mac OS X 10.11.2

=== Enabled user installed addins ===

Prism Template Pack 1.3
Xamarin Inspector
XamlStyler 1.0.1