Bug 41616

Summary: Mono 4.4.0 crashes when using Socket.ConnectAsync to a unix domain socket if the path doesn't exist
Product: [Mono] Class Libraries Reporter: kirankulkarni747
Component: SystemAssignee: marcos.henrich
Severity: normal CC: jahmai, jkolb, luis.aguilera, masafa, mohitk, mono-bugs+mono, mono-bugs+runtime, peter.collins
Priority: ---    
Version: 4.4.0 (C7)   
Target Milestone: (C7SR1)   
Hardware: Macintosh   
OS: Mac OS   
Tags: Is this bug a regression?: Yes
Last known good build: 4.2.3

Description kirankulkarni747 2016-06-08 18:42:45 UTC
Mono 4.4.0 crashes when using Socket.ConnectAsync to a unix domain socket if the path doesn't exist. I created following console app as test code:

using System;
using System.Net.Sockets;
using System.Threading.Tasks;

namespace MonoSocketConnectBug
    class Program
        static void Main(string[] args)
            string socketPath = (args.Length == 0) ? "/tmp/foo" : args[0];

            Task t = Task.Run(async () =>
                var endPoint = new Mono.Unix.UnixEndPoint(socketPath);
                var socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Unspecified);
                var tcs = new TaskCompletionSource<SocketError>();
                var socketArgs = new SocketAsyncEventArgs();

                socketArgs.RemoteEndPoint = endPoint;
                socketArgs.UserToken = tcs;
                socketArgs.Completed += (sender, e) => ((TaskCompletionSource<SocketError>)e.UserToken).SetResult(e.SocketError);
                SocketError error = socket.ConnectAsync(socketArgs) ? await tcs.Task.ConfigureAwait(false) : socketArgs.SocketError;

                Console.WriteLine("SocketError={0}, Socket.Connected={1}", error, socket.Connected);


On Mono 4.4.0 the above console app crashes with following call stack:
Unhandled Exception:
System.InvalidOperationException: No operation in progress
  at System.Net.Sockets.Socket.<ConnectAsyncCallback>m__3 (IAsyncResult ares) <0x7e3ea0 + 0x0019f> in <filename unknown>:0 
  at System.Net.Sockets.SocketAsyncResult+<Complete>c__AnonStorey0.<>m__0 (System.Object _) <0x7e3de0 + 0x0001b> in <filename unknown>:0 
  at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem () <0x1970810 + 0x00035> in <filename unknown>:0 
  at System.Threading.ThreadPoolWorkQueue.Dispatch () <0x196ee40 + 0x001d8> in <filename unknown>:0 
  at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback () <0x1970680 + 0x00017> in <filename unknown>:0 

This is a regression from Mono 4.2.3 where this same console app runs successfully without any crashes. Here's the output from 4.2.3 runtime:
SocketError=Success, Socket.Connected=False
Comment 1 marcos.henrich 2016-06-27 09:18:49 UTC
Hi Kiran,

Thanks for the bug report,

This should be fixed by PR 3210

Once this is reviewed and merged I will do a cherry pick and pull request into the next service release.
Comment 2 Jeremy Kolb 2016-06-30 19:42:59 UTC
I'm seeing this on Xamarin Android latest stable.
Comment 3 marcos.henrich 2016-07-05 10:18:53 UTC
Hi jkolb, thanks for letting us know.

Fixed in master 80d015b2df275ad48fa77c02224f6ebc7e08bbd4

Fixed in mono-4.5.1-branch 0935e38357fa71312c701ed6aeec5459dec84a95

Open a request for adding the fix on the next service release:

Trello card: 
Comment 4 Luis Aguilera 2016-07-07 14:22:48 UTC
looks like this should be in resolved/fixed state after comment 3
Comment 6 marcos.henrich 2016-07-08 12:59:19 UTC
Fixed in mono-4.4.0-branch 5b18268ddbadb2033c457e224f9ce12bae623809
Comment 7 Jahmai 2016-07-17 10:17:00 UTC
I'm seeing something similar in that when ConnectAsync encounters an error and returns false, it still fires the callback asynchronously. Does this fix address this scenario or should I raise another bug?
Comment 8 marcos.henrich 2016-07-17 12:01:13 UTC
@Jahmai, please open a new bug report with a test case for your issue and let me know of it.
Comment 9 Jahmai 2016-07-17 12:27:43 UTC
@marchos https://bugzilla.xamarin.com/show_bug.cgi?id=42617