Skip to content

CancellationToken should be passed to ReadAsStringAsync/ReadAsStreamAsync #5186

@arshrp

Description

@arshrp

The code generated by NSwag allows the consumer to pass a CancellationToken, which it uses on the call to HttpClient.SendAsync, but it doesn't use on the downstream call to response.Content.ReadAsStringAsync (or ReadAsStreamAsync).

This is a potential issue, because the NSwag-generated call to SendAsync uses HttpCompletionOption.ResponseHeadersRead, meaning the SendAsync call will return as soon as the headers are read (it will not wait for the full response body to be read, that will happen on the subsequent call to response.Content.ReadAsStringAsync).

The Microsoft Documentation suggests the use of the HttpCompletionOption.ResponseHeadersRead option means the following call to ReadAsStringAsync could block, because the CancellationToken (as well as the default timeout) only applies to the SendAsync call.

I am seeing this behavior at my end. Http log statements before and after the SendAsync call are appearing, but nothing afterward. My best guess is the host service becomes unresponsive during the ReadAsStringAsync call, causing an infinite block.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions