1iveowl / SocketLite

MIT License
33 stars 7 forks source link

SocketLite throws exception on start with UWP Net Native Toolchain compilation #25

Open kelly987654 opened 6 years ago

kelly987654 commented 6 years ago

Issue: I'm attempting to use the SocketLite libraries for a Xamarin Forms UWP application. Everything works fine in the Debug/Release builds of the application with the 'Compile with .NET Native tool chain' setting turned off, but when the setting is turned on, an exception is thrown by the TcpSocketListener on the call to CreateObservableListener.

image

.NET Native tool chain setting (UWP Project -> Properties -> Build) image

Exceptions (unfortunately I don't have a more helpful exception message): Unhandled exception at 0x75D364A1 (combase.dll) in XF.TestApp.UWP.exe: 0xC000027B: An application-internal exception has occurred (parameters: 0x15065888, 0x00000001).

Unhandled exception at 0x75C7A2B9 (combase.dll) in XF.TestApp.UWP.exe: Stack cookie instrumentation code detected a stack-based buffer overrun.

Steps to reproduce:

  1. Create a test Xamarin Forms project containing a UWP project
  2. Install the SocketLite Nuget package in the Portable and UWP Projects
  3. Add code to the portable implementation to start a TcpSocketListener.
  4. Switch to release mode ('Compile with .NET Native tool chain' setting is on by default) and start the app => exception is thrown on CreateObservableListener
  5. Uncheck 'Compile with .NET Native tool chain' setting and redeploy the app => no exception

Versions used:

1iveowl commented 6 years ago

Have you tried with the updated version 5.1.1?

kelly987654 commented 6 years ago

More info I found in the build output from building with 5.0.1: C:\Program Files (x86)\Microsoft SDKs\UWPNuGetPackages\microsoft.net.native.compiler\2.1.8\tools\Microsoft.NetNative.targets(788,5): warning : MCG : warning MCG0007: Unresolved P/Invoke method 'iphlpapi.dll!GetAdaptersInfo' for method 'System.Int32 SocketLite.Helper.AdapterInfo.GetAdaptersInfo(System.IntPtr, System.Int64)'. Calling this method would throw exception at runtime. Please make sure the P/Invoke either points to a Windows API allowed in UWP applications, or a native DLL that is part of the package. If for some reason your P/Invoke does not satisfy those requirements, please use [DllImport(ExactSpelling=true) to indicate that you understand the implications of using non-UWP APIs.

I tried updating SocketLite to 5.1.1 along with the required System.Reactive update to 4.1.0, and the build of the UWP app seems to hang indefinitely. I tried with SocketLite 5.1.1 and System.Reactive 4.0.0 and then I still got the same exception behavior as in 5.0.1.

I think System.Reactive 4.1.0 has some significant issues, would it be possible to also revert back to 4.0.0?

Thanks for your help!

1iveowl commented 6 years ago

If you use v5.0.1 then you will be using System.Reactive 4.0.0. There are not other differences between 5.1.1 and 5.0.1 than the move to System.Reative 4.1.0.

Let me know if this helps?

Also, I'll do some more research on System.Reactive 4.1.0.

kelly987654 commented 6 years ago

Using 5.0.1 avoids the System.Reactive issue, but still has the issue with the exception being thrown at runtime when compiled with NET Native toolchain.

Thanks!

kelly987654 commented 6 years ago

I was able to reproduce the issue in the SocketLite UWP test app run in Release mode with the following modification to MainPage.xaml.cs:

      public MainPage()
      {
        this.InitializeComponent();
        Start();
      }

      public async void Start()
      {
        var test = new TcpSocketListener();
        var temp = await test.CreateObservableListener(8004);
        temp.Subscribe(x => { });
      }

(Using the 5.0.1 release commit due to the System.Reactive issue)