MessagePackSerializer.Deserialize throws MessagePackSerializationException instead of OperationCanceledException when cancellation is requested
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Anfängerfreundlichkeit
- 35/100
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
Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.
- Kein Dockerfile und keine Docker-Compose-Datei
- Keine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus MessagePack-CSharp/MessagePack-CSharp
-
SerializableMemberTypeAnalyzer throws AD0001 when any referenced assembly has an assembly-level attribute with a single array argumentEvtl. vergeben Ein verknüpfter Pull Request ist offen oder bereits gemergt. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 76/100
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
MessagePack-CSharp/MessagePack-CSharp#2276 · 2 Kommentare ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 66/100
MessagePack-CSharp/MessagePack-CSharp#2252 · 3 Kommentare ·
-
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 52/100
Alle Issues in MessagePack-CSharp/MessagePack-CSharp
Ähnliche Issues
-
kind/bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
unoplatform/uno#26555 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
Listenarrs/Listenarr#1173 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 77/100
CactuseSecurity/firewall-orchestrator#5739 ·
Maintainer antworten meist innerhalb von 1 Tag
-
chore project-system small
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
vikramlearning/blazorbootstrap#1388 ·
Maintainer antworten meist innerhalb von 1 Tag