This is Xamarin's bug tracking system. For product support, please use the support links listed in your Xamarin Account.
Bug 41616 - Mono 4.4.0 crashes when using Socket.ConnectAsync to a unix domain socket if the path doesn't exist
Summary: Mono 4.4.0 crashes when using Socket.ConnectAsync to a unix domain socket if ...
Status: VERIFIED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System (show other bugs)
Version: 4.4.0 (C7)
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: (C7SR1)
Assignee: marcos.henrich
URL:
Depends on:
Blocks:
 
Reported: 2016-06-08 18:42 UTC by kirankulkarni747
Modified: 2016-07-17 12:27 UTC (History)
8 users (show)

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


Attachments

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);
            });

            t.GetAwaiter().GetResult();
        }
    }
}

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
https://github.com/mono/mono/pull/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
https://github.com/mono/mono/commit/80d015b2df275ad48fa77c02224f6ebc7e08bbd4

Fixed in mono-4.5.1-branch 0935e38357fa71312c701ed6aeec5459dec84a95
https://github.com/mono/mono/commit/0935e38357fa71312c701ed6aeec5459dec84a95

Open a request for adding the fix on the next service release:
https://github.com/mono/mono/pull/3237

Trello card: 
https://trello.com/c/nq8lB7dO/61-mono-41616-mono-4-4-0-crashes-when-using-socket-connectasync
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
https://github.com/mono/mono/commit/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

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