Merge pull request #595 from snechaev/PR-594_Fix_Unexpected_FFMpegException_On_Cancellation

Do not throw unexpected FFMpegException on FFProbe cancallation. Fixes  #594
This commit is contained in:
Malte Rosenbjerg 2025-10-27 19:15:11 +01:00 committed by GitHub
commit 1346049991
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 72 additions and 2 deletions

View file

@ -1,4 +1,5 @@
using FFMpegCore.Test.Resources; using FFMpegCore.Exceptions;
using FFMpegCore.Test.Resources;
namespace FFMpegCore.Test; namespace FFMpegCore.Test;
@ -285,4 +286,70 @@ public class FFProbeTests
var info = FFProbe.Analyse(TestResources.Mp4Video, customArguments: "-headers \"Hello: World\""); var info = FFProbe.Analyse(TestResources.Mp4Video, customArguments: "-headers \"Hello: World\"");
Assert.AreEqual(3, info.Duration.Seconds); Assert.AreEqual(3, info.Duration.Seconds);
} }
[TestMethod]
[Timeout(10000, CooperativeCancellation = true)]
public async Task Parallel_FFProbe_Cancellation_Should_Throw_Only_OperationCanceledException()
{
// Warm up FFMpegCore environment
Helpers.FFProbeHelper.VerifyFFProbeExists(GlobalFFOptions.Current);
var mp4 = TestResources.Mp4Video;
if (!File.Exists(mp4))
{
Assert.Inconclusive($"Test video not found: {mp4}");
return;
}
var cts = CancellationTokenSource.CreateLinkedTokenSource(TestContext.CancellationToken);
var token = cts.Token;
using var semaphore = new SemaphoreSlim(Environment.ProcessorCount, Environment.ProcessorCount);
var tasks = Enumerable.Range(0, 50).Select(x => Task.Run(async () =>
{
await semaphore.WaitAsync(token);
try
{
var analysis = await FFProbe.AnalyseAsync(mp4, cancellationToken: token);
return analysis;
}
finally
{
semaphore.Release();
}
}, token)).ToList();
// Wait for 2 tasks to finish, then cancel all
await Task.WhenAny(tasks);
await Task.WhenAny(tasks);
await cts.CancelAsync();
cts.Dispose();
var exceptions = new List<Exception>();
foreach (var task in tasks)
{
try
{
await task;
}
catch (Exception e)
{
exceptions.Add(e);
}
}
Assert.IsNotEmpty(exceptions, "No exceptions were thrown on cancellation. Test was useless. " +
".Try adjust cancellation timings to make cancellation at the moment, when ffprobe is still running.");
// Check that all exceptions are OperationCanceledException
CollectionAssert.AllItemsAreInstancesOfType(exceptions, typeof(OperationCanceledException));
}
[TestMethod]
[Timeout(10000, CooperativeCancellation = true)]
public async Task FFProbe_Should_Throw_FFMpegException_When_Exits_With_Non_Zero_Code()
{
var input = TestResources.SrtSubtitle; //non media file
await Assert.ThrowsAsync<FFMpegException>(async () => await FFProbe.AnalyseAsync(input,
cancellationToken: TestContext.CancellationToken, customArguments: "--some-invalid-argument"));
}
} }

View file

@ -84,6 +84,7 @@ public static class FFProbe
var instance = PrepareStreamAnalysisInstance(filePath, ffOptions ?? GlobalFFOptions.Current, customArguments); var instance = PrepareStreamAnalysisInstance(filePath, ffOptions ?? GlobalFFOptions.Current, customArguments);
var result = await instance.StartAndWaitForExitAsync(cancellationToken).ConfigureAwait(false); var result = await instance.StartAndWaitForExitAsync(cancellationToken).ConfigureAwait(false);
cancellationToken.ThrowIfCancellationRequested();
ThrowIfExitCodeNotZero(result); ThrowIfExitCodeNotZero(result);
return ParseOutput(result); return ParseOutput(result);
@ -123,6 +124,7 @@ public static class FFProbe
{ {
var instance = PrepareStreamAnalysisInstance(uri.AbsoluteUri, ffOptions ?? GlobalFFOptions.Current, customArguments); var instance = PrepareStreamAnalysisInstance(uri.AbsoluteUri, ffOptions ?? GlobalFFOptions.Current, customArguments);
var result = await instance.StartAndWaitForExitAsync(cancellationToken).ConfigureAwait(false); var result = await instance.StartAndWaitForExitAsync(cancellationToken).ConfigureAwait(false);
cancellationToken.ThrowIfCancellationRequested();
ThrowIfExitCodeNotZero(result); ThrowIfExitCodeNotZero(result);
return ParseOutput(result); return ParseOutput(result);
@ -150,6 +152,7 @@ public static class FFProbe
} }
var result = await task.ConfigureAwait(false); var result = await task.ConfigureAwait(false);
cancellationToken.ThrowIfCancellationRequested();
ThrowIfExitCodeNotZero(result); ThrowIfExitCodeNotZero(result);
pipeArgument.Post(); pipeArgument.Post();

View file

@ -13,6 +13,6 @@ public static class ProcessArgumentsExtensions
public static async Task<IProcessResult> StartAndWaitForExitAsync(this ProcessArguments processArguments, CancellationToken cancellationToken = default) public static async Task<IProcessResult> StartAndWaitForExitAsync(this ProcessArguments processArguments, CancellationToken cancellationToken = default)
{ {
using var instance = processArguments.Start(); using var instance = processArguments.Start();
return await instance.WaitForExitAsync(cancellationToken); return await instance.WaitForExitAsync(cancellationToken).ConfigureAwait(false);
} }
} }