Skip to content

Newtonsoft deserialization blocking managed threads #2058

@cool-mist

Description

@cool-mist

Describe the bug 🐞

This code in NewtonsoftJsonContentSerializer seems to be doing a blocking read using StreamReader to do the deserialization.

This can block the threads (sync-over-async) when downloading large payloads. This problem doesn't exist in System.Text.Json as it uses the correct HttpContent.ReadFromJsonAsync in the SystemTextJsonContentSerializer.

One fix could be to download the data to an internally managed (preferably pooled) byte buffer before doing the deserialization via StreamReader + JsonTextReader. While this can increase the memory footprint, I think the other problem of blocking threads could be a bigger one comparatively.

Step to reproduce

The code that can block threads is here where line number 67 blocks threads on a network stream because the request is already sent with HttpCompletionOptions.ResponseHeaderRead as part of RequestBuilderImplementation classes.

Image

Reproduction repository

https://github.com/reactiveui/refit

Expected behavior

Threads should not get blocked while waiting for network bytes.

Screenshots 🖼️

No response

IDE

No response

Operating system

No response

Version

No response

Device

No response

Refit Version

No response

Additional information ℹ️

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions