Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

MagickImage.ReadAsync swallows non cancellation related exceptions from the stream it reads from

已关闭 适合新手
#2,097 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
78/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
csharp
领域
backend

调研方向

首先定位 AsyncStreamWrapper.ReadAsync,以及等待它的 MagickImage.ReadAsync 路径。使用提供的 ThrowingStream 测试重现问题,然后验证其 IOException 能够传递到调用方,同时取消操作仍按预期运行。测试通过且不返回空图像即表示完成。

由索引模型根据 Issue 内容生成。

描述

Magick.NET version

14.17.2

Environment (Operating system, version and so on)

Mac / Linix

Description

AsyncStreamWrapper has this code:

    private async Task ReadAsync(CancellationToken cancellationToken)
    {
        while (true)
        {
            try
            {
                await _performRead.WaitAsync(cancellationToken).ConfigureAwait(false);
            }
            catch
            {
                _exceptionThrown = true;
                if (_performRead.Wait(0))
                    _readDone.Release();
                return;
            }

            if (_readCount == -1)
                return;

            try
            {
                _readCount = await _stream.ReadAsync(Data, 0, _readCount, cancellationToken).ConfigureAwait(false);
            }
            catch
            {
                _exceptionThrown = true;
            }
            finally
            {
                _readDone.Release();
            }
        }
    }

if _stream.ReadAsync throws the fact is logged so that subsequently in the caller the cancellation token has an opportunity to throw.

        var readTask = ReadAsync(cancellationToken);

        await Task.WhenAll(actionTask, readTask).ConfigureAwait(false);

        if (_exceptionThrown)
            cancellationToken.ThrowIfCancellationRequested();
    }

However this has an unfortunate side effect, and that is that non-cancellation related exceptions are swallowed. I discovered this because I use a wrapper around my network streams that throw under certain conditions. When this wrapper throws MagickImage.ReadAsync returns an RGB image of 0x0 dimensions rather than allowing the exception to percolate up.

Steps to Reproduce
    class ThrowingStream : MemoryStream
    {
        public ThrowingStream(byte[] buffer, bool writable) : base(buffer, writable)
        {
        }

        public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
        {
            throw new IOException("Space Goat Failure");
        }
    }
    
    [TestMethod]
    public async Task SafeReadAsync_HealthyStream_ReadsTheImageBad()
    {
        using var image = new MagickImage();
        byte[] input = [1, 2, 3, 4, 5];

        await Assert.ThrowsAsync<Exception>(() => image.ReadAsync(new ThrowingStream(input, false)));
    }

The test fails because the exception is swallowed.

The screenshot is just a placeholder so I could submit the issue, it is of no relevance.

Images

Screenshot 2026-10-01 at 16.48.56.png

主要语言
C#
星标
4k
派生
454
平均合并
11 小时 22 分钟
30 天内合并 PR
5

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

dlemstra/Magick.NET 的其他 Issue

查看 dlemstra/Magick.NET 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。