runtime icon indicating copy to clipboard operation
runtime copied to clipboard

SslStream object disposed exception

Open halter73 opened this issue 7 years ago • 27 comments

From @timmydo on Saturday, 08 December 2018 21:55:49

I'm not sure if this is the right place to report this, but I'm seeing this exception occasionally:

kubernetes docker pod uname -a: Linux platform-prod-577cf48b4b-wrg7x 4.15.0-1030-azure dotnet/corefx#31~16.04.1-Ubuntu SMP Tue Oct 30

context: running kestrel behind nginx on linux in docker image based on dotnet:2.1-aspnetcore-runtime

19:40:01 UTC 2018 x86_64 GNU/Linux
Unhandled Exception: System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'SslStream'.
   at System.Net.Security.SslState.ThrowIfExceptional()
   at System.Net.Security.SslState.CheckThrow(Boolean authSuccessCheck, Boolean shutdownCheck)
   at System.Net.Security.SslState.CheckOldKeyDecryptedData(Memory`1 buffer)
   at System.Net.Security.SslState.HandleQueuedCallback(Object& queuedStateRequest)
   at System.Net.Security.SslState.FinishHandshakeRead(Int32 newState)
   at System.Net.Security.SslState.FinishHandshake(Exception e, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.RehandshakeCompleteCallback(IAsyncResult result)
   at System.Net.LazyAsyncResult.Complete(IntPtr userToken)
   at System.Net.LazyAsyncResult.ProtectedInvokeCallback(Object result, IntPtr userToken)
   at System.Net.AsyncProtocolRequest.CompleteUserWithError(Exception e)
   at System.Net.FixedSizeReader.ReadPacketAsync(Stream transport, AsyncProtocolRequest request)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
   at System.Threading.ThreadPoolWorkQueue.Dispatch()

pod error code:

Last State:     Terminated
      Reason:       Error
      Exit Code:    139
      Started:      Fri, 07 Dec 2018 17:06:46 -0800
      Finished:     Fri, 07 Dec 2018 23:48:04 -0800

let me know if there is more info I could provide. thanks.

Copied from original issue: aspnet/KestrelHttpServer#3111

halter73 avatar Dec 12 '18 20:12 halter73

From @halter73 on Wednesday, 12 December 2018 20:16:44

System.Net.Security.SslState.RehandshakeCompleteCallback(IAsyncResult result)

The ODE seems to have happened during TLS renegotiation. Kestrel ensures that it's done with its SslStreams before disposing them meaning that any calls to AuthenticateAsServerAsync(), ReadAsync(), WriteAsync(), etc... have completed.

@timmydo Do you have any evidence that the SslStream belongs to Kestrel? Kestrel doesn't initiate a renegotiations itself, and I don't know why a client would do so either. I suspect that HttpClient might own the SslStream.

Either way, we should probably move this issue to dotnet/corefx.

halter73 avatar Dec 12 '18 20:12 halter73

@karelz @Tratcher

halter73 avatar Dec 12 '18 20:12 halter73

How often does it happen? Does it happen on more than 1 machine or for more than 1 user? It does not seem that the above info is sufficient to root cause it, or to even identify the components owning the SslStream.

karelz avatar Dec 12 '18 20:12 karelz

this seems to happen on different machines. looks like it's happening from an http client request because the program isn't listening for connections (it's just processing things in a queue--so not kestrel related like the original issue). maybe a handful of times a day. if there is a way you could recommend I get more info that would be great. if it's not easy, I can just close this. I agree the stack trace doesn't look that helpful

timmydo avatar Dec 12 '18 23:12 timmydo

I don't know about a way forward without additional info or simple repro. Closing. We can reopen if more people are affected or when there is actionable info available

karelz avatar Jan 09 '19 02:01 karelz

@karelz This issue occurs regularly (every minute) if there is a lot of traffic. I tried ramping up some to this new environment today and had to take it down due to crashes. The stack trace usually appears at the end of the logs, so I would suspect that it's fatal whatever it is. I will try to narrow down which dependency is at fault, but like I said above, I suspect it's related to the http client.

Do you have any ideas on how I could get more info?

timmydo avatar Jan 09 '19 02:01 timmydo

Could this be related to dotnet/runtime#21330 ?

timmydo avatar Jan 09 '19 18:01 timmydo

Or dotnet/runtime#28073 and maybe fixed with dotnet/corefx#34089

timmydo avatar Jan 10 '19 01:01 timmydo

I facing the same problem and due that the application is closing.

Unhandled Exception: 
Unhandled Exception: System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'SslStream'.
   at System.Net.Security.SslState.ThrowIfExceptional()
   at System.Net.Security.SslState.CheckThrow(Boolean authSuccessCheck, Boolean shutdownCheck)
   at System.Net.Security.SslState.CheckOldKeyDecryptedData(Memory`1 buffer)
   at System.Net.Security.SslState.HandleQueuedCallback(Object& queuedStateRequest)
   at System.Net.Security.SslState.FinishHandshakeRead(Int32 newState)
   at System.Net.Security.SslState.FinishHandshake(Exception e, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.RehandshakeCompleteCallback(IAsyncResult result)
   at System.Net.LazyAsyncResult.Complete(IntPtr userToken)
   at System.Net.LazyAsyncResult.ProtectedInvokeCallback(Object result, IntPtr userToken)
   at System.Net.AsyncProtocolRequest.CompleteUserWithError(Exception e)
   at System.Net.FixedSizeReader.ReadPacketAsync(Stream transport, AsyncProtocolRequest request)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
   at System.Threading.ThreadPoolWorkQueue.Dispatch()System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'SslStream'.
   at System.Net.Security.SslState.ThrowIfExceptional()
   at System.Net.Security.SslState.CheckThrow(Boolean authSuccessCheck, Boolean shutdownCheck)
   at System.Net.Security.SslState.CheckOldKeyDecryptedData(Memory`1 buffer)
   at System.Net.Security.SslState.HandleQueuedCallback(Object& queuedStateRequest)
   at System.Net.Security.SslState.FinishHandshakeRead(Int32 newState)
   at System.Net.Security.SslState.FinishHandshake(Exception e, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.RehandshakeCompleteCallback(IAsyncResult result)
   at System.Net.LazyAsyncResult.Complete(IntPtr userToken)
   at System.Net.LazyAsyncResult.ProtectedInvokeCallback(Object result, IntPtr userToken)
   at System.Net.AsyncProtocolRequest.CompleteUserWithError(Exception e)
   at System.Net.FixedSizeReader.ReadPacketAsync(Stream transport, AsyncProtocolRequest request)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
   at System.Threading.ThreadPoolWorkQueue.Dispatch()

Aborted (core dumped)

LucasFarley avatar Jan 20 '19 17:01 LucasFarley

3.0 daily builds fixed it for me

timmydo avatar Jan 21 '19 01:01 timmydo

@timmydo, where can i download this update?

LucasFarley avatar Jan 21 '19 12:01 LucasFarley

@timmydo, where can i download this update?

Daily builds of .NET Core are here: https://github.com/dotnet/core/blob/master/daily-builds.md

davidsh avatar Jan 21 '19 16:01 davidsh

Re-opening for release/2.1 LTS port

davidsh avatar Jul 13 '19 23:07 davidsh

Fix for release/2.1 checked in with PR dotnet/corefx#39456

davidsh avatar Jul 16 '19 20:07 davidsh

When the fix will be released in .Net Core 2.2.(7?) officially? I see that it is already merged with the release branch several weeks ago. Also it would be nice if somebody had any idea of a possible temporary workaround. (I tried and failed..)

benjzh avatar Aug 07 '19 14:08 benjzh

The fix will ship with the next 2.2.x release which is 2.2.7. And that will be sometime next month.

It will also ship with the next 2.1.x release which is 2.1.13. That should be shipped the same time as 2.2.x.

davidsh avatar Aug 08 '19 17:08 davidsh

This doesn't seem to be fixed in .NET Core 3.0.0-preview9. Should I expect it to have been fixed in that release, or would it only appear in rc1 or the full release? (though mine is from an HttpClient)

System.Threading.Tasks.TaskCanceledException: The operation was canceled.
 ---> System.IO.IOException: The read operation failed, see inner exception.
 ---> System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'SslStream'.
   at System.Net.Security.SslStream.ThrowIfExceptional()
   at System.Net.Security.SslStream.CheckThrow(Boolean authSuccessCheck, Boolean shutdownCheck)
   at System.Net.Security.SslStream.CheckOldKeyDecryptedData(Memory`1 buffer)
   at System.Net.Security.SslStream.HandleQueuedCallback(Object& queuedStateRequest)
--- End of stack trace from previous location where exception was thrown ---
   at System.Net.Security.SslStream.ReadAsyncInternal[TReadAdapter](TReadAdapter adapter, Memory`1 buffer)
   --- End of inner exception stack trace ---
   at System.Net.Security.SslStream.ReadAsyncInternal[TReadAdapter](TReadAdapter adapter, Memory`1 buffer)
   at System.Net.Http.HttpConnection.FillAsync()
   at System.Net.Http.HttpConnection.ReadNextResponseHeaderLineAsync(Boolean foldedHeadersAllowed)
   at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync(HttpConnection connection, HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)

kyschouv avatar Sep 23 '19 20:09 kyschouv

.NET Core 3.0 released today. I suggest you try out the official release. If you still have a repro using .NET Core 3.0 and see these ObjectDisposed exceptions, please open a new issue and reference this one. Thanks!

davidsh avatar Sep 23 '19 21:09 davidsh

Will do. Time to failure was about 9 days, so it may not happen again soon, but wanted to make sure to report it, since I jumped to 3.0 just for this fix.

kyschouv avatar Sep 23 '19 21:09 kyschouv

We're getting the same issue on 3.0.0.

kyschouv avatar Sep 27 '19 19:09 kyschouv