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

Offen
#2,194 1 Kommentar 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Anfängerfreundlichkeit
35/100
Issue-Typ
Bug
Klarheit
Größtenteils klar
Aktivitätsstatus
Veraltet
Tech-Stack
csharp
Bereich
backend

Rechercherichtung

Beginne bei MessagePackSerializer.Deserialize(value, cancellationToken: cancellationToken) und reproduziere das Verhalten unter .NET 9 mit Version 3.1.3. Verfolge, wie cancellation in MessagePackSerializationException übersetzt wird, und überprüfe anschließend, dass cancellation zu OperationCanceledException führt, ohne andere Serialisierungsfehler zu verändern.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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!

Vorherrschende Sprache
C#
Sterne
6.8k
Forks
778
Ø Merge
22 Std. 8 Min.
Gemergte PRs (30 T.)
2

Entwicklungsumgebung

In Codespaces öffnen

Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus MessagePack-CSharp/MessagePack-CSharp

Alle Issues in MessagePack-CSharp/MessagePack-CSharp

Ähnliche Issues

Weitere Issues zu C#

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.