Xamarin.iOS app - System.Net.Sockets.SocketException: The socket is not connected

0

I have an Xamarin.iOS app - it uses the https://github.com/Azure/azure-mobile-apps-net-client SDK to communicate with Azure Mobile Service. The app supports offline usage - everything seems to work fine for some time including offline/online switch data synchronization etc. However, eventually, it receives the following error: System.Net.Sockets.SocketException: The socket is not connected. I don't know how to recover from this error.

The full inner exceptions is

ex.InnerException.InnerException.InnerException {System.Net.Sockets.SocketException (0x80004005): The socket is not connected at System.Net.Sockets.Socket.EndSend (System.IAsyncResult asyncResult) [0x00013] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.8.0.2/src/Xamarin.iOS/mcs/class/referencesource/System/net/System/Net/Sockets/Socket.cs:3876 at System.Net.Sockets.NetworkStream.EndWrite (System.IAsyncResult asyncResult) [0x00057] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.8.0.2/src/Xamarin.iOS/mcs/class/referencesource/System/net/System/Net/Sockets/NetworkStream.cs:1043 } base: {System.ComponentModel.Win32Exception} ErrorCode: 10057 Message: "The socket is not connected" SocketErrorCode: System.Net.Sockets.SocketError.NotConnected Non-public members:

There seems to be nothing I can do to recover from this exception. It keeps throwing the same exception on every http call subsequently. I am using Microsoft.WindowsAzure.MobileServices.MobileServiceClient for making http calls as well as offline table sync. I created a new MobileServiceClient instance after catching the exception but that does not seem to help. I keep getting the same error.

The full stack trace of the exception thrown by the https calls look like this:

System.Net.Http.HttpRequestException: An error occurred while sending the request ---> System.Net.WebException: Error: SecureChannelFailure (Unable to write data to the transport connection: The socket is not connected.) ---> System.IO.IOException: Unable to write data to the transport connection: The socket is not connected. ---> System.Net.Sockets.SocketException: The socket is not connected at System.Net.Sockets.Socket.EndSend (System.IAsyncResult asyncResult) [0x00013] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.8.0.2/src/Xamarin.iOS/mcs/class/referencesource/System/net/System/Net/Sockets/Socket.cs:3876 at System.Net.Sockets.NetworkStream.EndWrite (System.IAsyncResult asyncResult) [0x00057] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.8.0.2/src/Xamarin.iOS/mcs/class/referencesource/System/net/System/Net/Sockets/NetworkStream.cs:1043 --- End of inner exception stack trace --- at System.Net.Sockets.NetworkStream.EndWrite (System.IAsyncResult asyncResult) [0x0007c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.8.0.2/src/Xamarin.iOS/mcs/class/referencesource/System/net/System/Net/Sockets/NetworkStream.cs:1054 at System.IO.Stream+<>c.b__53_1 (System.IO.Stream stream, System.IAsyncResult asyncResult) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.8.0.2/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/io/stream.cs:750 at System.Threading.Tasks.TaskFactory1+FromAsyncTrimPromise1[TResult,TInstance].Complete (TInstance thisRef, System.Func`3[T1,T2,TResult] endMethod, System.IAsyncResult asyncResult, System.Boolean requiresSynchronization) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.8.0.2/src/Xamarin.iOS/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/FutureFactory.cs:1292

--- End of stack trace from previous location where exception was thrown ---

at Mono.Net.Security.MobileAuthenticatedStream.InnerWrite (System.Boolean sync, System.Threading.CancellationToken cancellationToken) [0x0008b] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.8.0.2/src/Xamarin.iOS/mcs/class/System/Mono.Net.Security/MobileAuthenticatedStream.cs:764 at Mono.Net.Security.AsyncProtocolRequest.ProcessOperation (System.Threading.CancellationToken cancellationToken) [0x0014f] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.8.0.2/src/Xamarin.iOS/mcs/class/System/Mono.Net.Security/AsyncProtocolRequest.cs:230 at Mono.Net.Security.AsyncProtocolRequest.StartOperation (System.Threading.CancellationToken cancellationToken) [0x00046] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.8.0.2/src/Xamarin.iOS/mcs/class/System/Mono.Net.Security/AsyncProtocolRequest.cs:187 at Mono.Net.Security.MobileAuthenticatedStream.ProcessAuthentication (System.Boolean runSynchronously, Mono.Net.Security.MonoSslAuthenticationOptions options, System.Threading.CancellationToken cancellationToken) [0x0024c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.8.0.2/src/Xamarin.iOS/mcs/class/System/Mono.Net.Security/MobileAuthenticatedStream.cs:396 at Mono.Net.Security.MonoTlsStream.CreateStream (System.Net.WebConnectionTunnel tunnel, System.Threading.CancellationToken cancellationToken) [0x00126] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.8.0.2/src/Xamarin.iOS/mcs/class/System/Mono.Net.Security/MonoTlsStream.cs:129 at System.Net.WebConnection.CreateStream (System.Net.WebOperation operation, System.Boolean reused, System.Threading.CancellationToken cancellationToken) [0x00170] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.8.0.2/src/Xamarin.iOS/mcs/class/System/System.Net/WebConnection.cs:222 --- End of inner exception stack trace --- 2019-05-17 21:16:07.482 pinnacle_wh.iOS[379:49127] at System.Net.WebConnection.CreateStream (System.Net.WebOperation operation, System.Boolean reused, System.Threading.CancellationToken cancellationToken) [0x00208] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.8.0.2/src/Xamarin.iOS/mcs/class/System/System.Net/WebConnection.cs:234 at System.Net.WebConnection.InitConnection (System.Net.WebOperation operation, System.Threading.CancellationToken cancellationToken) [0x000f7] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.8.0.2/src/Xamarin.iOS/mcs/class/System/System.Net/WebConnection.cs:263 at System.Net.WebOperation.Run () [0x00052] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.8.0.2/src/Xamarin.iOS/mcs/class/System/System.Net/WebOperation.cs:268 at System.Net.WebCompletionSource1[T].WaitForCompletion () [0x00094] in <cfeb875a95684ad09f58680b02e4aa2c>:0 at System.Net.HttpWebRequest.RunWithTimeoutWorker[T] (System.Threading.Tasks.Task1[TResult] workerTask, System.Int32 timeout, System.Action abort, System.Func1[TResult] aborted, System.Threading.CancellationTokenSource cts) [0x000f8] in <cfeb875a95684ad09f58680b02e4aa2c>:0 at System.Net.HttpWebRequest.EndGetResponse (System.IAsyncResult asyncResult) [0x00019] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.8.0.2/src/Xamarin.iOS/mcs/class/System/System.Net/HttpWebRequest.cs:1200 at System.Threading.Tasks.TaskFactory1[TResult].FromAsyncCoreLogic (System.IAsyncResult iar, System.Func2[T,TResult] endFunction, System.Action1[T] endAction, System.Threading.Tasks.Task`1[TResult] promise, System.Boolean requiresSynchronization) [0x0000f] in <91e0283eca55453fa9b161bf2de4edfd>:0 --- End of stack trace from previous location where exception was thrown ---

at System.Net.Http.HttpClientHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x003d3] in /Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/external/mono/mcs/class/System.Net.Http/System.Net.Http/HttpClientHandler.cs:402 --- End of inner exception stack trace --- at System.Net.Http.HttpClientHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x0046c] in /Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/external/mono/mcs/class/System.Net.Http/System.Net.Http/HttpClientHandler.cs:406 at PinnacleWareHouser.Helpers.AuthenticationDelegatingHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x0019f] in /Users/sameer/projects/PinnacleWareHouser/PinnacleWareHouser/Helpers/AuthenticationDelegatingHandler.cs:59 at System.Net.Http.HttpClient.SendAsyncWorker (System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpCompletionOption completionOption, System.Threading.CancellationToken cancellationToken) [0x00080] in /Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/external/mono/mcs/class/System.Net.Http/System.Net.Http/HttpClient.cs:276 at Microsoft.WindowsAzure.MobileServices.MobileServiceHttpClient.SendRequestAsync (System.Net.Http.HttpClient client, System.Net.Http.HttpRequestMessage request, System.Boolean ensureResponseContent, System.Threading.CancellationToken cancellationToken) [0x0007a] in <8cb0059c34954d1285025a31cdcda32d>:0 at Microsoft.WindowsAzure.MobileServices.MobileServiceHttpClient.RequestAsync (System.Boolean UseHandlers, System.Net.Http.HttpMethod method, System.String uriPathAndQuery, Microsoft.WindowsAzure.MobileServices.MobileServiceUser user, System.String content, System.Boolean ensureResponseContent, System.Collections.Generic.IDictionary2[TKey,TValue] requestHeaders, System.Threading.CancellationToken cancellationToken) [0x000f0] in <8cb0059c34954d1285025a31cdcda32d>:0 at Microsoft.WindowsAzure.MobileServices.MobileServiceTable.ReadAsync (System.String uriString, Microsoft.WindowsAzure.MobileServices.MobileServiceFeatures features) [0x0009c] in <8cb0059c34954d1285025a31cdcda32d>:0 2019-05-17 21:16:07.483 pinnacle_wh.iOS[379:49127] at Microsoft.WindowsAzure.MobileServices.MobileServiceTable.ReadAsync (System.String query, System.Collections.Generic.IDictionary2[TKey,TValue] parameters, Microsoft.WindowsAzure.MobileServices.MobileServiceFeatures features) [0x00136] in <8cb0059c34954d1285025a31cdcda32d>:0 at Microsoft.WindowsAzure.MobileServices.Sync.PullAction.ProcessTableAsync () [0x00134] in <8cb0059c34954d1285025a31cdcda32d>:0 at Microsoft.WindowsAzure.MobileServices.Sync.TableAction.ExecuteAsync () [0x00251] in <8cb0059c34954d1285025a31cdcda32d>:0 at Microsoft.WindowsAzure.MobileServices.Sync.MobileServiceSyncContext.ExecuteSyncAction (Microsoft.WindowsAzure.MobileServices.Sync.SyncAction action) [0x00090] in <8cb0059c34954d1285025a31cdcda32d>:0 at Microsoft.WindowsAzure.MobileServices.Sync.MobileServiceSyncContext.PullAsync (System.String tableName, Microsoft.WindowsAzure.MobileServices.Sync.MobileServiceTableKind tableKind, System.String queryId, System.String query, Microsoft.WindowsAzure.MobileServices.MobileServiceRemoteTableOptions options, System.Collections.Generic.IDictionary2[TKey,TValue] parameters, System.Collections.Generic.IEnumerable1[T] relatedTables, Microsoft.WindowsAzure.MobileServices.MobileServiceObjectReader reader, System.Threading.CancellationToken cancellationToken, Microsoft.WindowsAzure.MobileServices.Sync.PullOptions pullOptions) [0x00361] in <8cb0059c34954d1285025a31cdcda32d>:0 at PinnacleWareHouser.Clients.AzureCloudTableClient`1[T].PullAsync () [0x0007c] in :0 at PinnacleWareHouser.Services.AzureCloudService.PullTableAsync[T] () [0x0016a] in :0

sockets
xamarin.ios
azure-mobile-services
asked on Stack Overflow May 18, 2019 by user3000624 • edited May 20, 2019 by Jack Hua - MSFT

1 Answer

0

I think the problem was caused because of an error I had in my code. The Microsoft.WindowsAzure.MobileServices.MobileServiceClient object instance was getting garbage collected in Mono but the underlying socket was not getting clean-up in iOS.

answered on Stack Overflow May 22, 2019 by user3000624

User contributions licensed under CC BY-SA 3.0