Bug 13416 - Socket.Connect blocks on non-blocking socket
Summary: Socket.Connect blocks on non-blocking socket
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: System (show other bugs)
Version: 4.4.0 (C7)
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2013-07-23 12:08 UTC by Christian
Modified: 2018-03-13 12:02 UTC (History)
4 users (show)

Tags:
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 13416 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:
Status:
NEW

Description Christian 2013-07-23 12:08:40 UTC
MSDN says:

Connect method will block, unless you specifically set the Blocking property to false prior to calling Connect. If you are using a connection-oriented protocol like TCP and you do disable blocking, Connect will throw a SocketException because it needs time to make the connection. Connectionless protocols will not throw an exception because they simply establish a default remote host. You can use SocketException.ErrorCode to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error. If the error returned WSAEWOULDBLOCK, the remote host connection has been initiated by a connection-oriented Socket, but has not yet completed successfully. Use the Poll method to determine when the Socket is finished connecting.

But it is not the case with Mono..

Socket = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
Socket.Blocking = false;
Socket.Connect( "10.10.10.10", 123456 );

Socket.Connect should immediately throw a SocketConnection but nothing happens. After three minutes a SocketException is throwing...

System.Net.Sockets.SocketException: Connection timed out
  at System.Net.Sockets.Socket.Connect (System.Net.IPAddress[] addresses, Int32 port) [0x00128] in /usr/src/packages/BUILD/mono-2.8.2/mcs/class/System/System.Net.Sockets/Socket.cs:1873
  at System.Net.Sockets.Socket.Connect (System.String host, Int32 port) [0x00007] in /usr/src/packages/BUILD/mono-2.8.2/mcs/class/System/System.Net.Sockets/Socket.cs:1878

Same thing on all Mono Versions. 2.6.7, 2.8.2, 2.10.9, 3.1.2

Thank you.
Comment 1 Christian 2013-07-23 12:13:30 UTC
Port 123456 will not working ever, sorry
Comment 2 Martin Baulig 2014-07-29 07:11:18 UTC
old
Comment 3 Thad House 2016-08-06 20:50:27 UTC
I've noticed this recently in a project I have as well, and have figured out the root cause. This is still an issue in Mono 4.4. The root cause is that when a socket is set to non blocking, the Connect() method calls Poll() with a timeout of -1. This causes an infinite wait until the socket times out, which is not the proper behavior.  The proper behavior works in .NET Core, as in .NET Core, if the socket is non blocking it returns the socket error immediately. The code in mono is labeled FIXME as well, so I have submitted a pull request to solve this issue. The pull request is located at https://github.com/mono/mono/pull/3368.