Skip to content

HCR060

Dispose HttpResponseMessage when using ResponseHeadersRead.

Why

ResponseHeadersRead returns control before the response body has been fully consumed. The HttpResponseMessage remains the owner of the connection and content stream, so failing to dispose it can hold sockets and response resources longer than intended.

Bad

var response = await client.SendAsync(
    request,
    HttpCompletionOption.ResponseHeadersRead,
    cancellationToken);

Better

using var response = await client.SendAsync(
    request,
    HttpCompletionOption.ResponseHeadersRead,
    cancellationToken);

Returning a stream or content object from the response does not dispose the response owner:

var response = await client.SendAsync(
    request,
    HttpCompletionOption.ResponseHeadersRead,
    cancellationToken);

return await response.Content.ReadAsStreamAsync(cancellationToken);

Suppression

Suppress only when ownership is transferred to the caller or another object that guarantees disposal.

Current Detection

The implementation reports local response variables initialized or assigned from awaited visible HttpClient calls such as SendAsync(...) or GetAsync(...), or synchronous HttpClient.Send(...), using HttpCompletionOption.ResponseHeadersRead unless the declaration is already a using var, owned by using (...) — including using (var alias = response) declarations — directly or through an order-preserving chain of unreassigned local aliases, transferred to a same-block using declaration such as using var owned = response, directly disposed at the containing block level through the same alias model (Dispose() or DisposeAsync(), awaited or not), disposed in a finally block either directly or through those aliases, directly returned as the response, passed directly into a returned owner wrapper, assigned into a returned owner wrapper initializer, or transferred through a wrapper local that is returned. Parentheses, null-forgiving operators, and explicit response casts are treated as transparent around direct ownership references. Disposal, using-declaration transfer, and direct return evidence must apply before the response local is reassigned; disposing or returning a later assigned value does not count as ownership evidence for the original response. Ownership evidence for a value assigned inside a nested branch is searched from the block that declares the local, so returning that local later still counts as a transfer. Reassignments in mutually exclusive branches, or code after a guard branch that always exits, do not invalidate earlier ownership evidence. Reassigning the local to a new owner that consumes the previous value, such as response = new Wrapper(response), is treated as a transfer rather than a lost reference. Conditional disposal such as if (...) response.Dispose(); is not treated as sufficient ownership evidence. Locals that only store the pending Task<HttpResponseMessage> are skipped so diagnostics target actual response owners. Resolved custom HttpClient types, unrelated custom extensions on HttpClient, custom lookalike HttpCompletionOption.ResponseHeadersRead members, and lookalike calls are skipped unless Roslyn type information shows that the receiver, method owner, completion option, and returned response shape are the System.Net.Http types, or the source is unresolved but visibly uses those framework names. Test code is skipped: types ending in Test/Tests, types carrying common test attributes, types deriving from a test-named base type, types in a Test, Tests, Testing, *Test, or *Tests namespace, and files under a test, tests, testassets, testapps, or acceptance directory.

Known Limitations

Ownership must be visible in the method that acquires the response. The analyzer does not follow calls into helpers, services, or factories, so these shapes can still report even when disposal happens elsewhere:

  • passing the response to a method that takes ownership, such as a content reader or a service that disposes it;
  • storing the response in a field, collection, or custom container that is disposed later;
  • disposing through an alias declared before the response is assigned, especially one typed as IDisposable rather than HttpResponseMessage (the Elastic transport pattern);
  • a response produced by a custom wrapper or factory method instead of a resolved System.Net.Http.HttpClient call.

Test code is skipped by type name, attribute, namespace, and directory heuristics. A test-support library that does not look like test code can still report.

Code Fix

The code fix converts a simple local declaration into a using var declaration. It also merges an immediately adjacent uninitialized declaration and assignment into one using declaration, preserving comments between the two statements. Both shapes also apply to top-level statements. Non-adjacent or nested assignments are left for manual ownership review. Merges are withheld when a preprocessor directive guards the declaration or the assignment, so conditional compilation keeps its meaning. No fix is offered when the response outlives the block — stored into a member or container, or passed as an argument — because disposing at block end would break the new owner. Merely calling members on the response does not count as escaping.

References