From b863f5d19e876cb3527e05436b3ef4377406dcd0 Mon Sep 17 00:00:00 2001 From: Sergey Nechaev <6499856+snechaev@users.noreply.github.com> Date: Wed, 22 Oct 2025 20:41:26 +0200 Subject: [PATCH] FFProbe: Do not throw FFMpegException if cancellation was requested. Throw OperationCancelledException in this case to provide more uniform and expected behavior. Fixes #594 --- FFMpegCore/FFProbe/FFProbe.cs | 11 +++++++---- FFMpegCore/FFProbe/ProcessArgumentsExtensions.cs | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/FFMpegCore/FFProbe/FFProbe.cs b/FFMpegCore/FFProbe/FFProbe.cs index e199376..ba08346 100644 --- a/FFMpegCore/FFProbe/FFProbe.cs +++ b/FFMpegCore/FFProbe/FFProbe.cs @@ -84,7 +84,7 @@ public static class FFProbe var instance = PrepareStreamAnalysisInstance(filePath, ffOptions ?? GlobalFFOptions.Current, customArguments); var result = await instance.StartAndWaitForExitAsync(cancellationToken).ConfigureAwait(false); - ThrowIfExitCodeNotZero(result); + ThrowIfExitCodeNotZero(result, cancellationToken); return ParseOutput(result); } @@ -123,7 +123,7 @@ public static class FFProbe { var instance = PrepareStreamAnalysisInstance(uri.AbsoluteUri, ffOptions ?? GlobalFFOptions.Current, customArguments); var result = await instance.StartAndWaitForExitAsync(cancellationToken).ConfigureAwait(false); - ThrowIfExitCodeNotZero(result); + ThrowIfExitCodeNotZero(result, cancellationToken); return ParseOutput(result); } @@ -150,7 +150,7 @@ public static class FFProbe } var result = await task.ConfigureAwait(false); - ThrowIfExitCodeNotZero(result); + ThrowIfExitCodeNotZero(result, cancellationToken); pipeArgument.Post(); return ParseOutput(result); @@ -212,8 +212,11 @@ public static class FFProbe } } - private static void ThrowIfExitCodeNotZero(IProcessResult result) + private static void ThrowIfExitCodeNotZero(IProcessResult result, CancellationToken cancellationToken = default) { + // if cancellation requested, then we are not interested in the exit code, just throw the cancellation exception + // to get consistent and expected behavior. + cancellationToken.ThrowIfCancellationRequested(); if (result.ExitCode != 0) { var message = $"ffprobe exited with non-zero exit-code ({result.ExitCode} - {string.Join("\n", result.ErrorData)})"; diff --git a/FFMpegCore/FFProbe/ProcessArgumentsExtensions.cs b/FFMpegCore/FFProbe/ProcessArgumentsExtensions.cs index ef7140a..6682e94 100644 --- a/FFMpegCore/FFProbe/ProcessArgumentsExtensions.cs +++ b/FFMpegCore/FFProbe/ProcessArgumentsExtensions.cs @@ -13,6 +13,6 @@ public static class ProcessArgumentsExtensions public static async Task StartAndWaitForExitAsync(this ProcessArguments processArguments, CancellationToken cancellationToken = default) { using var instance = processArguments.Start(); - return await instance.WaitForExitAsync(cancellationToken); + return await instance.WaitForExitAsync(cancellationToken).ConfigureAwait(false); } }