Skip to content

Image.LoadAsync(stream) uses synchronous IO if the stream is seekable  #1997

Closed
@kroymann

Description

@kroymann

ImageSharp version

1.0.4 (verified issue is still present in 2.0.0)

Other ImageSharp packages and versions

ImageSharp.Web 1.0.5 (+ fixes from PR 222)

Environment (Operating system, version and so on)

Azure App Service (Windows-based)

.NET Framework version

6.0

Description

Image.LoadAsync(stream) ends up synchronously reading from the source stream in both Image.InternalDetectFormatAsync() and then again in ImageDecoderUtilities.DecodeAsync(). When running in a webservice under high-load scenarios, this can quickly lead to thread pool exhaustion as all threads are blocked waiting on network IO.

private static Task<IImageFormat> InternalDetectFormatAsync(Stream stream, Configuration config)
{
    return Task.FromResult(InternalDetectFormat(stream, config));
}
public static Task<Image<TPixel>> DecodeAsync<TPixel>(...)
{
    using var bufferedReadStream = new BufferedReadStream(configuration, stream);
    Image<TPixel> image = decoder.Decode<TPixel>(bufferedReadStream, cancellationToken);
    return Task.FromResult(image);
}

Steps to Reproduce

Try to load an image asynchronously and observe that threads end up blocked on synchronous reads

Images

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions