-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Cannot access a disposed object. Object name: 'SslStream'. #31254
Comments
Is it possible your requests are timing out? Does the same issue happen on .NET Core 3.0? |
Are you accidentally returning from an async method without awaiting within a using block? |
@scalablecory @Plasma |
@scalablecory
|
Okay, can you reduce your code into a small repro? This will help diagnose the problem. |
@samneefs
then this would be perceived as "normal" behavior. By "normal", I mean that the HTTP request was cancelled probably due to a timeout-related cancellation token expiring. The cancellation token could either be one you pass into the HttpClient APIs. Or the internal one used when HttpClient.Timeout is used. By default HttpClient.Timeout has a non-infinite timeout. I think the "bug" here is that while cancelling the HTTP request, there are operations that get cancelled internally (such as on SslStream). And cancelling those I/O operation results in a bug where we are trying to reference the, now disposed, SslStream. That is probably why those other internal exceptions are occurring. It would probably make sense for the HTTP stack to potentially ignore some of those exceptions since they are "expected" to occur while shutting down the connection. However, I think the SslStream ObjectDisposedException is something where we can fix that so that we don't try to access the SslStream after it has been disposed. |
I got same exception in the local build
I was running tests under strace and everything is very slow. I did get several OperationCanceled exceptions before (and I fiddle with timeouts) but this is the first time when I get ObjectDisposedException. |
I can also get running current 5.0 master.
|
I would say based on this stack trace, the only part we need to fix is the 'ObjectDisposedException'. We should be able to detect when the SslStream is being closed by upstack code (in this case due to a cancellation) and prevent SslStream from accessing its own objects while closing down. |
Triage: We should see if we can detect it and avoid throwing. If not, then it is likely confusing to customers, in which case we may want to mask it / not include as inner exception of TaskCanceled. Note: We have seen more customers running into this and being confused in the past. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Any update on this issue @karelz ? |
@kshyju not really. We just didn't get to it yet. |
Do you have way how to reproduce it @kshyj? Is is still happening with 5.0? |
Yep. "Just my code" needs to be disabled and the exception needs to be enabled to trigger a break in VS. |
Do you have any simple repro @trympet |
@wfurt, sure. I'll email you a repro and memory dump. |
@wfurt remember to disable "Just my code" and enable the |
can it be reproduced without VS/Debugger @trympet? |
@wfurt I was unable to reproduce the issue without attaching a debugger. |
HTTP/2 is whole different animal @trympet. Because of the multiplex, there is read task to pull incoming frames with race condition to disposal. However, as far as I can tell, this does not surface as that is expected and get eaten by the HttpClient. When you running under debugger, it allows you to see and intercept all the exception regarless if they are handled or not. So you would see it there but that should not impact the actual code execution. |
Is there any update on this Issue , I am using net6.0 and I am also facing same error
|
Do you have repro @bhanupalsingh? Perhaps you can also try 8.0 as current LTS. |
Since I didn't see it mentioned while skimming existing comments, this is almost certainly the result of our weird way of "cancelling" pending I/O in our HTTP/1.1 implementation. runtime/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnection.cs Lines 922 to 927 in ff0c538
We're not actually passing the cancellation token down to |
While Or should we change the cancellation logic? I'm wondering if the existing code comes from time when cancellation on Is this something we should try to investigate in 10? While the impact may be small it seems like this keeps coming over and over again ;( |
We have a mechanism to wrap these exceptions in runtime/src/libraries/System.Net.Http/src/System/Net/Http/CancellationHelper.cs Lines 19 to 20 in c9af66c
Then we wrap the exception to a runtime/src/libraries/System.Net.Http/src/System/Net/Http/CancellationHelper.cs Lines 26 to 27 in c9af66c
We use this mechanism excessively in code that deals with the mentioned |
This is a connection pooling bug where a disposed (canceled) connection is returned to the pool and picked up by the next request in the queue. (And that request fails with an In the case I managed to reproduce I see this happening in
I see two simple fixes to this bug:
|
Nice catch. Lines 402 to 414 in cc74292
But Another case where this check may fail is if the cancellation token passed to
or a similar case where the second read happens as part of Solution-wise, we should ideally fix the underlying cause for all of this, which is using Otherwise your proposed solution nr. 2 seems better. |
I agree, but that's a relatively wide change that should be executed carefully. My preference would be to ship a minimal fix for this bug and open a separate one to track the HTTP/1.1 cancellation refactor. |
I have an issue with SendAsync in .NET Core 2.2.
When doing a lot of requests, I encounter the exception on some of them.
I have gotten the error on different requests, for example on a small GET request. However I do have to mention that asynchronously I'm also doing other requests, for example POST (with content smaller than 4MB).
This issue is similar to https://github.com/dotnet/corefx/issues/34033 , but not the same. Mine doesn't mention the handshake in the stacktrace and the other one should be fixed since Core 2.1 (and I use 2.2).
The text was updated successfully, but these errors were encountered: