MessagePackSerializer.Deserialize throws MessagePackSerializationException instead of OperationCanceledException when cancellation is requested

未關閉
#2,194 1 則留言 1 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
3/5
預估耗時
1-2 天
新手友好度
35/100
Issue 類型
缺陷
描述清晰度
基本清楚
活躍度
停滯
技術堆疊
csharp
領域
backend

研究方向

從 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

環境準備

在 Codespaces 中開啟

在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。

  • 沒有 Dockerfile 或 Docker Compose 檔案
  • 沒有 Pull Request 範本
  • 閱讀貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

MessagePack-CSharp/MessagePack-CSharp 的其他 Issue

查看 MessagePack-CSharp/MessagePack-CSharp 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。