Skip to content

[BUG]: Unreadable stream #451

Description

@mig42

AAS extension version used

3.42.0

Operating system and platform

Windows (x64)

Language

.NET

Language Runtime Version

.NET 10

Bug Report

After upgrading the Datadog Extension in my Azure App Service, this code started failing:

using System.Text;
using Newtonsoft.Json;

namespace Test;

public class ApiClient
{
    public async Task<T> SendAsync<T>(
        HttpMethod method,
        Uri server,
        string path,
        object content,
        CancellationToken cancellationToken = default)
    {
        var completeUri = new Uri(server, path);

        using var request = new HttpRequestMessage(method, completeUri);
        request.Content = content == null ? null : new JsonStreamContent(content);

        var response = await httpClient.SendAsync(
            request,
            HttpCompletionOption.ResponseHeadersRead,
            cancellationToken);
        if (!response.IsSuccessStatusCode)
        {
            var responseContent = await response.Content.ReadAsStringAsync(cancellationToken);
            throw new MyServerException((int)response.StatusCode, responseContent);
        }

        await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken);
        using var streamReader = new StreamReader(stream, Encoding.UTF8);
        await using var jsonReader = new JsonTextReader(streamReader);

        var serializer = JsonSerializer.CreateDefault();
        return serializer.Deserialize<T>(jsonReader);
    }
}

This line:

        using var streamReader = new StreamReader(stream, Encoding.UTF8);

randomly fails, causing this exception:

  System.ArgumentException: Stream was not readable.                                                                                                                                                                                                                                                               
     at System.IO.StreamReader..ctor(Stream stream, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize, Boolean leaveOpen)                                                                                                                                                              
     at System.IO.StreamReader..ctor(Stream stream, Encoding encoding)                                                                                                                                                                                                                                             
     at plasticscm.com.Api.Clients.Plastic.Server.ApiClient.SendAsync[T](HttpMethod method, Uri server, String path, Object content, String plasticUserHeader, String plasticToken, CancellationToken cancellationToken) in                                                                                        
  /buildpath/ApiClient.cs:line 31

The problem is intermitent, and it disappears if I remove the extension. This started happening when I upgraded the extension from 3.36.0 to 3.42.0. The randomness might be related to sampling not being 100%.

Reproduction Code

using System.Text;
using Newtonsoft.Json;

namespace Test;

public class ApiClient
{
    public async Task<T> SendAsync<T>(
        HttpMethod method,
        Uri server,
        string path,
        object content,
        CancellationToken cancellationToken = default)
    {
        var completeUri = new Uri(server, path);

        using var request = new HttpRequestMessage(method, completeUri);
        request.Content = content == null ? null : new JsonStreamContent(content);

        var response = await httpClient.SendAsync(
            request,
            HttpCompletionOption.ResponseHeadersRead,
            cancellationToken);
        if (!response.IsSuccessStatusCode)
        {
            var responseContent = await response.Content.ReadAsStringAsync(cancellationToken);
            throw new MyServerException((int)response.StatusCode, responseContent);
        }

        await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken);
        using var streamReader = new StreamReader(stream, Encoding.UTF8);
        await using var jsonReader = new JsonTextReader(streamReader);

        var serializer = JsonSerializer.CreateDefault();
        return serializer.Deserialize<T>(jsonReader);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions