Skip to content

Newtonsoft deserialization blocking managed threads #2058

Closed

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

Activity

  1. cool-mist commented on Mar 23, 2026

    @cool-mist
    Author

    This fix could potentially cause a regression for applications downloading and deserializing large payloads (>2GB because the backing char[] array is int indexed) using Refit today.

    Users of Refit are generally calling another API, where one wouldn't expect such large payloads in response, so this could be OK. The safest fix might just be to use JsonTextReader over a char[] buffer (StreamReader) so that downloading from network stream is async and deserialization of bytes is sync.

  2. github-actions commented on Apr 7, 2026

    @github-actions

    This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  3. locked as resolved and limited conversation to collaborators on Apr 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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