MessagePackSerializer.Deserialize throws MessagePackSerializationException instead of OperationCanceledException when cancellation is requested
還沒有人認領這個 Issue。
評估
研究方向
從 MessagePackSerializer.Deserialize(value, cancellationToken: cancellationToken) 開始,使用 3.1.3 版本在 .NET 9 上重現該行為。追蹤 cancellation 如何被轉換為 MessagePackSerializationException,然後驗證取消會導致 OperationCanceledException,同時不變更無關的序列化錯誤。
由索引模型根據 Issue 內容生成。
描述
Bug description
Instead of raising an OperationCanceledException, MessagePackSerializer.Deserialize throws a MessagePackSerializationException when cancellation is requested.
Repro steps
Trigger a cancellation while MessagePackSerializer.Deserialize<T>(value, cancellationToken: cancellationToken); is running
Expected behavior
OperationCanceledException is thrown
Actual behavior
MessagePackSerializationException is trown with OperationCanceledException as the InnerException
- Version used: 3.1.3
- Runtime: net9
Additional context
👋 Big fan of the project! Thank you so much for your contributions.
I'm running a Service that does 24/7 transcoding of MessagePack into other formats. The service's configuration can be hot reloaded and when that happens, it triggers a CancellationTokenSource, swap the configuration and restarts everything.
A common pattern (checked both how dotnet Hosting internals do it and a few other projects) is to catch OperationCanceledException and handle the cancellation graciously. Here's a snippet
public async Task RunInfiniteTranscodingAsync<T>(PayloadProvider<byte[]> payloadProvider, CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
try
{
var payload = payloadProvider.GetNextAsync(cancellationToken);
var msg = MessagePackSerializer.Deserialize<T>(payload, cancellationToken: cancellationToken);
// Do something with message
}
catch (OperationCanceledException)
{
// Handle the case where the operation was canceled. We don't need to log this as a fatal error.
Log.Information("Transcoding execution cancellation requested. Exiting gracefully...");
}
catch (Exception ex)
{
// Handle all other unexpected exceptions.
Log.Fatal(ex, "Unexpected Exception encountered in RunInfiniteTranscodingAsync. Restarting in 15s...");
await Safe.TaskDelay(TimeSpan.FromSeconds(15), cancellationToken);
}
Log.Information("RunInfiniteTranscodingAsync closed.");
}
}
Because the OperationCanceledException is wrapped in MessagePackSerializationException, code would need to be adopted in all places where this pattern is used to either catch the exception explicitly or catch Exception and check the type of the InnerException explicitly as well.
This pattern is also used (a bit differently but conceptually the same) in https://source.dot.net/#Microsoft.Extensions.Hosting/Internal/Host.cs
In the snippet, the usage of MessagePack is right there but usually there's a few layers between that specific try/catch and the usage, it'd be a shame for like "root service handlers" to be aware that one of the job they are running is a MessagePack transcoding job and to handle the specific case where cancellation is requested on that method.
All of that to say that I do believe convention should be that OperationCanceledException be the Exception that is raised when cancellation is requested.
It's also tricky because this would technically be a breaking change as well.
It'd love to hear your thoughts on this!
- 主要語言
- C#
- 星號
- 6.8k
- 分支
- 778
- 平均合併
- 22 小時 8 分鐘
- 30 天內合併 PR
- 2
環境準備
在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。
- 沒有 Dockerfile 或 Docker Compose 檔案
- 沒有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
MessagePack-CSharp/MessagePack-CSharp 的其他 Issue
-
SerializableMemberTypeAnalyzer throws AD0001 when any referenced assembly has an assembly-level attribute with a single array argument可能已有人在做 關聯的 PR 仍在進行中或已合併。 未關閉
難度 2/5 1-3 小時 新手友好度 82/100
-
難度 1/5 1 小時以內 新手友好度 76/100
-
難度 2/5 1-3 小時 新手友好度 68/100
MessagePack-CSharp/MessagePack-CSharp#2276 · 2 則留言 ·
-
難度 2/5 1-3 小時 新手友好度 66/100
MessagePack-CSharp/MessagePack-CSharp#2252 · 3 則留言 ·
-
難度 4/5 3-5 天 新手友好度 52/100
查看 MessagePack-CSharp/MessagePack-CSharp 的全部 Issue
相似的 Issue
-
kind/bug
難度 2/5 1-3 小時 新手友好度 78/100
unoplatform/uno#26555 ·
維護者通常 1 天內回覆
-
Search button on a book does not retry without the author when the full query finds nothing可能已有人在做 關聯的 PR 仍在進行中或已合併。 未關閉
難度 2/5 1-3 小時 新手友好度 75/100
Listenarrs/Listenarr#1173 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 77/100
CactuseSecurity/firewall-orchestrator#5739 ·
維護者通常 1 天內回覆
-
Add File sorts a picked file by VB6's extensions, not the project format's (#4243 follow-up)可能已有人在做 關聯的 PR 仍在進行中或已合併。 未關閉chore project-system small
難度 2/5 1-3 小時 新手友好度 78/100
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 68/100
vikramlearning/blazorbootstrap#1388 ·
維護者通常 1 天內回覆