sentry-dotnet
sentry-dotnet copied to clipboard
SentryHttpFailedRequestHandler disposes Content on .net framework
Package
Sentry
.NET Flavor
.NET Framework
.NET Version
4.7.2
OS
Any (not platform specific)
SDK Version
3.33.0
Self-Hosted Sentry Version
No response
Steps to Reproduce
- Generate client from OpenApi with
NSwag - Create
HttpClientwithSentryHttpMessageHandlerand pass it to generated client - Send request which will fail and will be caught with
SentryHttpFailedRequestHandler
Expected Result
No exception thrown
Actual Result
- Handler uses
EnsureSuccessStatusCodewhich disposesContenton older versions (before net core) - NSwag generated code checks status code and if it's not
200it tries to read content body which is already disposed and generatesObjectDisposedexception
Hi @Liklainy, thanks for getting in touch.
@bitsandfoxes we could fairly easily implement our own EnsureSuccessStatusCode for target frameworks below .net5.0. I know Matt was against this on principal, as it would mean we wouldn't get any fixes if the code we copied changed.
However, we've already got a workaround for the broken framework code, but at the moment that workaround only protects the proper functioning of our own code (but doesn't guarantee other stuff won't get broken).
In this particular case, I'd be OK to bend the "don't copy framework code into our solution" rule, as long as our code is only used on .netcore3.1 and below. Under the hood, all EnsureSuccessStatusCode does is throw an exception if the response status code isn't in the 200 range, so trivial to implement ourselves:
- https://github.com/microsoft/referencesource/blob/51cf7850defa8a17d815b4700b67116e3fa283c2/System/net/System/Net/Http/HttpResponseMessage.cs#L117-L120
- https://github.com/microsoft/referencesource/blob/51cf7850defa8a17d815b4700b67116e3fa283c2/System/net/System/Net/Http/HttpResponseMessage.cs#L142-L159
Hi @jamescrosswell, thank you for quick reply!
I can suggest two ways to fix this issue:
- As a simple and crude fix we can store
Contentstream locally, make itnullso it will not be disposed, and restore it infinally - Throwing an exception just to immediately catch it doesn't make any sense. We only need to create an exception with
new. ButHttpRequestExceptionhas a different structure in different versions, for example,StatusCodewas added as a separate property which probably should be initialized for compatibility, which leads to more #if workarounds. Maybe the best way will be to create a separate custom exception class
we could fairly easily implement our own EnsureSuccessStatusCode for target frameworks below .net5.0.
I agree with this.