MagickImage.ReadAsync swallows non cancellation related exceptions from the stream it reads from
还没有人认领这个 Issue。
评估
调研方向
首先定位 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
- 主要语言
- C#
- 星标
- 4k
- 派生
- 454
- 平均合并
- 11 小时 22 分钟
- 30 天内合并 PR
- 5
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
dlemstra/Magick.NET 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 74/100
dlemstra/Magick.NET#2087 · 2 条评论 ·
-
难度 4/5 3-5 天 新手友好度 45/100
dlemstra/Magick.NET#2098 ·
-
难度 3/5 1-2 天 新手友好度 48/100
dlemstra/Magick.NET#2042 · 4 条评论 ·
-
GPU support未关闭
难度 5/5 一周以上 新手友好度 35/100
dlemstra/Magick.NET#1949 · 5 条评论 ·
-
难度 4/5 3-5 天 新手友好度 25/100
dlemstra/Magick.NET#1936 · 1 条评论 ·
查看 dlemstra/Magick.NET 的全部 Issue
相似的 Issue
-
devtools
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 1 天内回复
-
clawsweeper:fix-shape-clear clawsweeper:needs-maintainer-review clawsweeper:no-new-fix-pr clawsweeper:not-repro-on-main impact:ux-release-blocker issue-rating: 🦪 silver shellfish P0
难度 2/5 1-3 小时 新手友好度 72/100
openclaw/openclaw-windows-node#1843 · 1 条评论 · 1 个 reaction ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 66/100
-
kind:ux size:S type:bug
难度 2/5 1-3 小时 新手友好度 78/100
elsa-workflows/elsa-foundation#3256 ·
维护者通常 1 天内回复
-
[Bug]: Revit selector reads process ids 2000-2099 as years (flaky AgentClientTests.AYearOrAProcessIdSelectsTheHost)可能已有人在做 @Wurschdhaud 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 83/100
pyrevitlabs/pyRevit#3817 ·
维护者通常 1 天内回复