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

SentryHttpFailedRequestHandler disposes Content on .net framework #2684

Closed
Liklainy opened this issue Sep 28, 2023 · 3 comments · Fixed by #3306
Closed

SentryHttpFailedRequestHandler disposes Content on .net framework #2684

Liklainy opened this issue Sep 28, 2023 · 3 comments · Fixed by #3306
Assignees
Labels
Bug Something isn't working .NET Framework

Comments

@Liklainy
Copy link

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

  1. Generate client from OpenApi with NSwag
  2. Create HttpClient with SentryHttpMessageHandler and pass it to generated client
  3. Send request which will fail and will be caught with SentryHttpFailedRequestHandler

Expected Result

No exception thrown

Actual Result

  1. Handler uses EnsureSuccessStatusCode which disposes Content on older versions (before net core)
  2. NSwag generated code checks status code and if it's not 200 it tries to read content body which is already disposed and generates ObjectDisposed exception
@Liklainy Liklainy added Bug Something isn't working Platform: .NET labels Sep 28, 2023
@Liklainy Liklainy changed the title SentryHttpFailedRequestHandler disposes Content on older versions SentryHttpFailedRequestHandler disposes Content on .net framework Sep 28, 2023
@jamescrosswell
Copy link
Collaborator

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:

@Liklainy
Copy link
Author

Liklainy commented Oct 3, 2023

Hi @jamescrosswell, thank you for quick reply!

I can suggest two ways to fix this issue:

  1. As a simple and crude fix we can store Content stream locally, make it null so it will not be disposed, and restore it in finally
  2. Throwing an exception just to immediately catch it doesn't make any sense. We only need to create an exception with new. But HttpRequestException has a different structure in different versions, for example, StatusCode was 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

@bitsandfoxes
Copy link
Contributor

we could fairly easily implement our own EnsureSuccessStatusCode for target frameworks below .net5.0.

I agree with this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working .NET Framework
Projects
Status: Done
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants