Error: ConnectFailure (Network is unreachable) CBL .NET Xamarin Android

Currently using Couchbase Lite 1.4 build 14 in Xamarin.Android, Android 5.0, with Samsung Galaxy Tab T2. When losing Wifi connection, we are getting the following exception:

01-06 16:34:31.440 E/mono    (23265): Unhandled Exception:
01-06 16:34:31.440 E/mono    (23265): System.Net.WebException: Error: ConnectFailure (Network is unreachable) ---> System.Net.Sockets.SocketException: Network is unreachable
01-06 16:34:31.440 E/mono    (23265):   at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x000cb] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/System/System.Net.Sockets/Socket.cs:1313 
01-06 16:34:31.440 E/mono    (23265):   at System.Net.WebConnection.Connect (System.Net.HttpWebRequest request) [0x0019b] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/System/System.Net/WebConnection.cs:195 
01-06 16:34:31.440 E/mono    (23265):    --- End of inner exception stack trace ---
01-06 16:34:31.440 E/mono    (23265):   at System.Net.HttpWebRequest.EndGetResponse (System.IAsyncResult asyncResult) [0x0005e] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/System/System.Net/HttpWebRequest.cs:1023 
01-06 16:34:31.440 E/mono    (23265):   at System.Threading.Tasks.TaskFactory`1[TResult].FromAsyncCoreLogic (System.IAsyncResult iar, System.Func`2[T,TResult] endFunction, System.Action`1[T] endAction, System.Threading.Tasks.Task`1[TResult] promise, System.Boolean requiresSynchronization) [0x00014] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/referencesource/mscorlib/system/threading/Tasks/FutureFactory.cs:550 
01-06 16:34:31.440 E/mono    (23265): --- End of stack trace from previous location where exception was thrown ---
01-06 16:34:31.440 E/mono    (23265):   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 
01-06 16:34:31.440 E/mono    (23265):   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 
01-06 16:34:31.440 E/mono    (23265):   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 
01-06 16:34:31.440 E/mono    (23265):   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 
01-06 16:34:31.440 E/mono    (23265):   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:535 
01-06 16:34:31.440 E/mono    (23265):   at System.Net.Http.HttpClientHandler+<SendAsync>c__async0.MoveNext () [0x003ce] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/System.Net.Http/System.Net.Http/HttpClientHandler.cs:379 
01-06 16:34:31.440 E/mono    (23265): --- End of stack trace from previous location where exception was thrown ---
01-06 16:34:31.440 E/mono    (23265):   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 
01-06 16:34:31.440 E/mono    (23265):   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 
01-06 16:34:31.440 E/mono    (23265):   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 
01-06 16:34:31.440 E/mono    (23265):   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 
01-06 16:34:31.440 E/mono    (23265):   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.

I think this is related to issue https://github.com/couchbase/couchbase-lite-net/issues/664, which is currenly closed.

Also, the following is printed before the above stack trace:

01-10 11:22:16.429 I/mono-stdout( 3505): 10/01/2017 11:22:16 AM|Fatal|<startReceiving>c__AnonStorey4.<>m__2|System.IO.IOException: Unable to read data from the transport connection: Connection timed out. ---> System.Net.Sockets.SocketException: Connection timed out
10/01/2017 11:22:16 AM|Fatal|<startReceiving>c__AnonStorey4.<>m__2|System.IO.IOException: Unable to read data from the transport connection: Connection timed out. ---> System.Net.Sockets.SocketException: Connection timed out
                               at System.Net.Sockets.Socket.EndReceive (System.IAsyncResult result) [0x0002d] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/System/System.Net.Sockets/Socket.cs:2031 
01-10 11:22:16.429 I/mono-stdout( 3505):                                at System.Net.Sockets.Socket.EndReceive (System.IAsyncResult result) [0x0002d] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/System/System.Net.Sockets/Socket.cs:2031 
01-10 11:22:16.429 I/mono-stdout( 3505):                                at System.Net.Sockets.NetworkStream.EndRead (System.IAsyncResult asyncResult) [0x0005f] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/referencesource/System/net/System/Net/Sockets/NetworkStream.cs:858 
01-10 11:22:16.429 I/mono-stdout( 3505):                                 --- End of inner exception stack trace ---
01-10 11:22:16.429 I/mono-stdout( 3505):                                at System.Net.Sockets.NetworkStream.EndRead (System.IAsyncResult asyncResult) [0x00092] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/referencesource/System/net/System/Net/Sockets/NetworkStream.cs:870 
01-10 11:22:16.429 I/mono-stdout( 3505):                                at WebSocketSharp.Ext+<ReadBytesAsync>c__AnonStorey3.<>m__0 (System.IAsyncResult ar) [0x00000] in /Users/jenkins/jenkins/workspace/couchbase-lite-net-build@2/1.4/Android/couchbase-lite-net/vendor/websocket-sharp/websocket-sharp/Ext.cs:648 
                               at System.Net.Sockets.NetworkStream.EndRead (System.IAsyncResult asyncResult) [0x0005f] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/referencesource/System/net/System/Net/Sockets/NetworkStream.cs:858 
                                --- End of inner exception stack trace ---
                               at System.Net.Sockets.NetworkStream.EndRead (System.IAsyncResult asyncResult) [0x00092] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/referencesource/System/net/System/Net/Sockets/NetworkStream.cs:870 
                               at WebSocketSharp.Ext+<ReadBytesAsync>c__AnonStorey3.<>m__0 (System.IAsyncResult ar) [0x00000] in /Users/jenkins/jenkins/workspace/couchbase-lite-net-build@2/1.4/Android/couchbase-lite-net/vendor/websocket-sharp/websocket-sharp/Ext.cs:648 
[0:] INFO) CHANGE TRACKER (WebSocketChangeTracker): [8] 2017-1-10 11:22:16.450+08:00 WebSocketChangeTracker[sync_gateway] remote forcibly closed connection (1006 An exception has occurred while receiving.)
[0:] INFO) SYNC (Replication): [6] 2017-1-10 11:22:16.580+08:00 Pusher e0f3be80-bf53-4409-8d50-3056603ad893 going offline
[0:] INFO) SYNC (Replication): [6] 2017-1-10 11:22:16.588+08:00 Puller e30c9f24-feb9-4d95-9475-003301ce65b4 going offline
[0:] INFO) CHANGE TRACKER (WebSocketChangeTracker): [10] 2017-1-10 11:22:16.598+08:00 WebSocketChangeTracker[sync_gateway] requested to stop
10/01/2017 11:22:16 AM|Error|WebSocket.CloseAsync|This operation isn't available in: closed
01-10 11:22:16.599 I/mono-stdout( 3505): 10/01/2017 11:22:16 AM|Error|WebSocket.CloseAsync|This operation isn't available in: closed
[0:] INFO) CHANGE TRACKER (WebSocketChangeTracker): [5] 2017-1-10 11:22:16.626+08:00 WebSocketChangeTracker[sync_gateway] remote error An error has occurred in closing the connection.
[0:] INFO) CHANGE TRACKER (WebSocketChangeTracker): [5] 2017-1-10 11:22:16.638+08:00 WebSocketChangeTracker[sync_gateway] remote error An error has occurred in closing the connection.:

An unhandled exception occured.

referenceTable GDEF length=814 1
referenceTable GSUB length=11364 1
referenceTable GPOS length=47302 1
referenceTable GDEF length=808 1
referenceTable GSUB length=11364 1
referenceTable GPOS length=49206 1
referenceTable head length=54 1

As the ticket indicates, I have not ever been able to reproduce this problem. That exception is a normal thing that happens when connectivity is lost but I have yet to see it bring down the entire application. I will need a reproduction case in order to figure out what is going on.

A little more info: I have been able resolve this issue, I don’t fully understand why, but i’ll explain what I changed.
Originally, I was running the creation of the replications (push and pull) in a Task.Run, so as to not hang the UI when the app starts. I have taken it out of that now and it seems to be ok.