zyborg / LogentriesDotNet

Port of Logentries logging targets to .NET Core
MIT License
4 stars 2 forks source link

iOS: System.IO.IOException: Unable to write data to the transport connection: The socket has been shut down. #6

Open jmosbech opened 6 years ago

jmosbech commented 6 years ago

Hello,

When using the library in a Xamarin/iOS app we're seeing these exceptions quite often: System.IO.IOException: Unable to write data to the transport connection: The socket has been shut down.

We have not been able to figure exactly how to trigger them, but they seem to appear when the app is terminating or going to the background.

The full stack trace is here:

System.Net.Sockets.Socket.Send(byte[] buffer, int offset, int size, SocketFlags socketFlags)Socket.cs:1540
System.Net.Sockets.NetworkStream.Write(byte[] buffer, int offset, int size)NetworkStream.cs:591
--- End of inner exception stack trace ---
System.Net.Sockets.NetworkStream.Write(byte[] buffer, int offset, int size)NetworkStream.cs:602
Mono.Net.Security.MobileAuthenticatedStream.<InnerWrite>b__67_0()MobileAuthenticatedStream.cs:579
System.Threading.Tasks.Task.InnerInvoke()Task.cs:2886
System.Threading.Tasks.Task.Execute()Task.cs:2509
Mono.Net.Security.MobileAuthenticatedStream.<InnerWrite>d__67.MoveNext()MobileAuthenticatedStream.cs:583
Mono.Net.Security.AsyncProtocolRequest.<ProcessOperation>d__24.MoveNext()AsyncProtocolRequest.cs:230
Mono.Net.Security.AsyncProtocolRequest.<StartOperation>d__23.MoveNext()AsyncProtocolRequest.cs:187
Mono.Net.Security.MobileAuthenticatedStream.<StartOperation>d__58.MoveNext()MobileAuthenticatedStream.cs:380
--- End of inner exception stack trace ---
System.Threading.Tasks.Task.ThrowIfExceptional(bool includeTaskCanceledExceptions)Task.cs:2166
System.Threading.Tasks.Task.Wait(int millisecondsTimeout, CancellationToken cancellationToken)Task.cs:3196
System.Threading.Tasks.Task.Wait()Task.cs:3061
Mono.Net.Security.MobileAuthenticatedStream.Write(byte[] buffer, int offset, int count)MobileAuthenticatedStream.cs:326
System.Net.Security.SslStream.Write(byte[] buffer, int offset, int count)SslStream.cs:379
Zyborg.Logentries.LogentriesClient.Write(byte[] buffer, int offset, int count)<672858277c0944d4bbfd5142450eb616>:0
Zyborg.Logentries.LogentriesAsyncLogger.Run()<672858277c0944d4bbfd5142450eb616>:0
System.Threading.ThreadHelper.ThreadStart_Context(object state)thread.cs:68
System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, object state, bool preserveSyncCtx)executioncontext.cs:957
System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, object state, bool preserveSyncCtx)executioncontext.cs:904
System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, object state)executioncontext.cs:893
System.Threading.ThreadHelper.ThreadStart()thread.cs:105
---> (Inner Exception #0) System.IO.IOException: Unable to write data to the transport connection: The socket has been shut down. ---> System.Net.Sockets.SocketException: The socket has been shut down
System.Net.Sockets.Socket.Send(byte[] buffer, int offset, int size, SocketFlags socketFlags)Socket.cs:1540
System.Net.Sockets.NetworkStream.Write(byte[] buffer, int offset, int size)NetworkStream.cs:591
--- End of inner exception stack trace ---
System.Net.Sockets.NetworkStream.Write(byte[] buffer, int offset, int size)NetworkStream.cs:602
Mono.Net.Security.MobileAuthenticatedStream.<InnerWrite>b__67_0()MobileAuthenticatedStream.cs:579
System.Threading.Tasks.Task.InnerInvoke()Task.cs:2886
System.Threading.Tasks.Task.Execute()Task.cs:2509
Mono.Net.Security.MobileAuthenticatedStream.<InnerWrite>d__67.MoveNext()MobileAuthenticatedStream.cs:583
Mono.Net.Security.AsyncProtocolRequest.<ProcessOperation>d__24.MoveNext()AsyncProtocolRequest.cs:230
Mono.Net.Security.AsyncProtocolRequest.<StartOperation>d__23.MoveNext()AsyncProtocolRequest.cs:187
Mono.Net.Security.MobileAuthenticatedStream.<StartOperation>d__58.MoveNext()MobileAuthenticatedStream.cs:380

We have not seen this behaviour in the le_dotnet library which we are using in another app.

Do you have any ideas for a fix?

ebekker commented 6 years ago

Unfortunately, I'm not actively working on this port anymore. Based on your description and knowing what little I do about iOS platform, my guess is that iOS is suspending or even terminating network connections -- I believe it does this and other similar operations to mange power consumption by background tasks.

Is your other app that uses le_dotnet also an iOS app or Android? If iOS, I would recommend switching to le_dotnet as it's the more official library for LE.

jmosbech commented 6 years ago

Thanks! Appreciate your answer and the time you invested in the package. We will be looking at the le_dotnet package instead.

Edit: le_dotnet is not working on Xamarin. We switched to Serilog and the Logentries sink, and it seems to be working better.