Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SslStream object disposed exception #28171

Closed
halter73 opened this issue Dec 12, 2018 · 27 comments
Closed

SslStream object disposed exception #28171

halter73 opened this issue Dec 12, 2018 · 27 comments
Assignees
Labels
area-System.Net.Security bug tenet-reliability Reliability/stability related issue (stress, load problems, etc.)
Milestone

Comments

@halter73
Copy link
Member

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
Copy link
Member Author

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 halter73 self-assigned this Dec 12, 2018
@halter73
Copy link
Member Author

@karelz @Tratcher

@karelz
Copy link
Member

karelz commented Dec 12, 2018

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.

@timmydo
Copy link

timmydo commented Dec 12, 2018

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

@karelz
Copy link
Member

karelz commented Jan 9, 2019

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 karelz closed this as completed Jan 9, 2019
@timmydo
Copy link

timmydo commented Jan 9, 2019

@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
Copy link

timmydo commented Jan 9, 2019

Could this be related to #21330 ?

@timmydo
Copy link

timmydo commented Jan 10, 2019

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

@LucasFarley
Copy link

LucasFarley commented Jan 20, 2019

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)

@timmydo
Copy link

timmydo commented Jan 21, 2019

3.0 daily builds fixed it for me

@LucasFarley
Copy link

@timmydo, where can i download this update?

@davidsh
Copy link
Contributor

davidsh commented Jan 21, 2019

@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 referenced this issue in davidsh/corefx Jul 13, 2019
Port fix from PR dotnet#34089 (.NET Core 3.0) to release/2.1 LTS branch.

Fixes #34033
@davidsh
Copy link
Contributor

davidsh commented Jul 13, 2019

Re-opening for release/2.1 LTS port

@davidsh davidsh reopened this Jul 13, 2019
@davidsh davidsh self-assigned this Jul 13, 2019
davidsh referenced this issue in davidsh/corefx Jul 13, 2019
Port fix from PR dotnet#34089 (.NET Core 3.0) to release/2.1 LTS branch.

Fixes #34033
davidsh referenced this issue in dotnet/corefx Jul 16, 2019
…ler (#39456)

Port fix from PR #34089 (.NET Core 3.0) to release/2.1 LTS branch.

Fixes #34033
@davidsh
Copy link
Contributor

davidsh commented Jul 16, 2019

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

@davidsh davidsh closed this as completed Jul 16, 2019
@benjzh
Copy link

benjzh commented Aug 7, 2019

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..)

@davidsh
Copy link
Contributor

davidsh commented Aug 8, 2019

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.

@kyschouv
Copy link

kyschouv commented Sep 23, 2019

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)

@davidsh
Copy link
Contributor

davidsh commented Sep 23, 2019

.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!

@kyschouv
Copy link

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
Copy link

We're getting the same issue on 3.0.0.

@karelz
Copy link
Member

karelz commented Sep 30, 2019

@kyschouv looks like the problem has multiple root causes (or similar symptoms)) and you've hit another one. Please file a new issue with description, call stacks, repro attempts info, etc., so that we can chase it down.

@menozz
Copy link

menozz commented Oct 10, 2019

We have the same issue in high-load stress environment (release v3.0.0):

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.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)

@ThatBlokeCalledJay
Copy link

We have also been receiving this in our production environment, intermittently over the past few weeks. After reading through this issue, plus a couple of linked issues am able to repro consistently. In short, if I start uploading a large-ish file, one that allows me enough time to close the browser before the file upload completes, I get this error.

System.Threading.Tasks.TaskCanceledException: The operation was canceled. ---> System.Net.Http.HttpRequestException: Error while copying content to a stream. ---> System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'SslStream'.
at System.Net.Security.SslState.ThrowIfExceptional()
at System.Net.Security.SslState.CheckThrow(bool authSuccessCheck, bool shutdownCheck)
at System.Net.Security.SslState.get_SecureStream()
at System.Net.Security.SslStream.WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken)
at System.Net.Http.HttpConnection.WriteToStreamAsync(ReadOnlyMemory source)
at System.Net.Http.HttpConnection.FlushAsync()

@srdmnc
Copy link

srdmnc commented Oct 25, 2019

This issue is closed already. Where's the final fix?

@davidsh
Copy link
Contributor

davidsh commented Nov 1, 2019

n short, if I start uploading a large-ish file, one that allows me enough time to close the browser before the file upload completes, I get this error.

What browser are you using? How is .NET Core involved with your browser exactly?

@karelz
Copy link
Member

karelz commented Nov 1, 2019

@davidsh
Copy link
Contributor

davidsh commented Nov 1, 2019

We definitely fixed the original issue where an ObjectDisposedException on SslStream was being thrown on a threadpool thread and thus crashing the process with an un-catchable exception. That was fixed in .NET Core 3.0 as I mentioned.

However, it appears based on these reports above that there is still an ObjectDisposedException surfacing from SslStream during an HTTP request cancellation/timeout. This is showing up in the callstacks above as an inner exception to the TaskCanceledException. So, if you have a repro for that, please open a new issue so that can be investigated.

Keep in mind that the top-level exception, TaskCanceledException is probably correct. Something caused the HttpClient to cancel the request, most likely due to the HttpClient.Timeout occurring. However, there is potentially a bug still in .NET Core where the cancellation logic and the closing of the SslStream and TCP connections is throwing these inner exceptions that are surfacing in the call stack. Those exceptions can probably be trapped in a better way since the ObjectDisposedException is most likely "expected" as the connection is being closed due to the cancellation.

So, to help isolate this particular issue, please open a new issue and post repro instructions so we can diagnose. Thanks!

@msftgits msftgits transferred this issue from dotnet/corefx Feb 1, 2020
@msftgits msftgits added this to the 2.1.x milestone Feb 1, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Net.Security bug tenet-reliability Reliability/stability related issue (stress, load problems, etc.)
Projects
None yet
Development

No branches or pull requests

12 participants