Forum Xamarin.iOS

Update seems to have broken my networking code?

BarrettLewisBarrettLewis USMember ✭✭

I have an application that opens a tcp socket to a server and reads/writes data with BeginRead and EndRead in callbacks on a NetworkStream and then an SslStream after establishing TLS. This code is mostly in a shared library that works fine in windows, and worked fine in the first release of our application on Xamarin.Mac. Working on our new mac milestone I started getting this bad behavior where it disconnects within a few seconds of connection.

Specifically, when the client is disconnected after 3-4 seconds it is through an exception in my BeginRead callback, at the point where it calls EndRead, and it gives the message "Operation on non-blocking socket would block" which as I understand should never happen on an EndRead call especially outside of the main thread as is the case in a callback. This error might be happening on the first callback after TLS has been established although I will have to investigate that further tomorrow.

Even rebuilding the SHA from our original mac release, the problem occurs. I have the binaries originally built from this SHA (which are also in the field) and they work fine and do not exhibit the issue. I always try to follow the mantra that 99.99% of the time the bug is in my code, not the tools, but this really suggests to me something is wrong in the build dependencies, either in mono or Xamarin.Mac bindings, or otherwise my environment.

Eventually I discovered that my second Mac environment (used for CI) can build the code in a way that does not exhibit the issue.

If I take the two bundles, built from the same code on the different environments, and look in the MonoBundle folder, the one which works has a Xamarin.Mac.dll version and the one that is broken has Xamarin.Mac.dll version I know it might not actually be this dll, but the mono dlls all just say and I thought this might atleast date the versions.

Fortunately since my CI build environment has not updated and still works, for the time being I can make my dev builds there. But I'm worried because I don't fully know how updates are distributed. Are updates automatic/will they happen through Apple store updates? Or will I be safe as long as I don't run "Check For Updates" within VS (on the mac)?

Before putting in the labor to make a test project, first I wanted to just throw this out and see if anyone has any thoughts about it. Has anything like this been previously reported?


  • BarrettLewisBarrettLewis USMember ✭✭
    edited July 2017

    Sorry I forgot to post the raw exception

    2017-07-13 05:52:38,690 [Threadpool worker] ERROR - Disconnection!
    System.Net.Sockets.SocketException: Operation on non-blocking socket would block
      at System.Net.Sockets.Socket.Receive (System.Byte[] buffer, System.Int32 offset, System.Int32 size, System.Net.Sockets.SocketFlags socketFlags) [0x00017] in /Library/Frameworks/Xamarin.Mac.framework/Versions/ 
      at System.Net.Sockets.NetworkStream.Read (System.Byte[] buffer, System.Int32 offset, System.Int32 size) [0x0009b] in /Library/Frameworks/Xamarin.Mac.framework/Versions/ 
    ------- Next Outer Exception ------
    System.IO.IOException: Unable to read data from the transport connection: Operation on non-blocking socket would block.
      at Mono.Net.Security.MobileAuthenticatedStream.EndReadOrWrite (System.IAsyncResult asyncResult, Mono.Net.Security.AsyncProtocolRequest& nestedRequest) [0x00056] in /Library/Frameworks/Xamarin.Mac.framework/Versions/ 
      at Mono.Net.Security.MobileAuthenticatedStream.EndRead (System.IAsyncResult asyncResult) [0x00000] in /Library/Frameworks/Xamarin.Mac.framework/Versions/ 
      at System.Net.Security.SslStream.EndRead (System.IAsyncResult asyncResult) [0x00000] in /Library/Frameworks/Xamarin.Mac.framework/Versions/ 
      at Intercommunication.Session.SessionBase.OnDataReceived (System.IAsyncResult asyn) [0x0000e] in /Users/gchj43576/Code/Shared/Intercommunication/Session/SessionBase.cs:179 

    Target Framework: Xamarin.Mac .NET 4.5 Framework

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai
    edited July 2017

    What was the version of Xamarin.Mac you were using before this started occurring / current version? I see 2.10 and 3.4, were those an example or the difference?

  • BarrettLewisBarrettLewis USMember ✭✭

    2.10 is what I built on for our release back in may which was/is working well. 2.10 is also what my CI server is currently still building on and does not show the issue. At some point (I don't remember when to be honest) my dev machine was updated to 3.4 and I am seeing the issue.
    I have not ruled out the Xamarin.Mac version is a red herring and it's something to do with my environment. Is it possible to roll back to building on 2.10 in order to test that?

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    So there have been a large number of changes between 2.10 and 3.4, 3 major releases at least.

    One thing I know for certain that has changed networking related is the defaulting of AppleTLS and remove of mono's legacy TLS.

    You could try rebuilding against 2.10 and add --tls-provider=appletls to your additional mmp arguments. If that breaks things, we have a potential smoking gun.

    If that does not work, you could upgrade in slower bumps and see where the problem pops up:

  • BarrettLewisBarrettLewis USMember ✭✭

    When I tried adding --tls-provider=appletls to the project and building it on the CI service which still has Xamarin.Mac 2.10, I got the error "Selecting a TLS provider is only supported in the Unified Mobile Profile (MM2011)"

    Also, is there a trick to installing these older Xamarin.Mac pkgs? I basically just downloaded them, and then tried to run each and then run the project. When I did on the 3.2 and 3.0 package it said

    The assembly mscorlib.dll was not found or could not be loaded.
    It should have been installed in the `/Users/builder/data/lanes/4466/a04678c2/source/xamarin-macios/builds/install/mac64/lib/mono/4.5/mscorlib.dll' directory.

    I also downloaded and tried
    this gave me a different error when I tried to run the project

    In all cases I was able to do VisualStudio -> Check For Updates and update back to 3.4 to get back to the original issue state

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    Looks like setting the tls provider that way on non-Modern (Mobile) profiles was a d15-2 (3.4) feature, my bad.

    You could try launching your 2.10 app this way:

    MONO_TLS_PROVIDER=apple ./Foo.App/Contents/MacOS/Foo

    and see if that works.

    Yes, downloading and installing the pkgs should be sufficient. Sometimes doing a clean build after upgrade is required. If that does not work, please post the build log, as that snippet is insufficient.

    I am assuming you don't have a small sample showing the issue at hand that we can look at?

Sign In or Register to comment.