From d608026d177943411b36291a591ea1fddaa24ce9 Mon Sep 17 00:00:00 2001 From: Ashish <156739271+alahane-techtel@users.noreply.github.com> Date: Fri, 4 Jul 2025 23:12:05 +1000 Subject: [PATCH] Fixed race condition on Named pipe dispose/disconnect --- FFMpegCore/FFMpeg/Arguments/PipeArgument.cs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/FFMpegCore/FFMpeg/Arguments/PipeArgument.cs b/FFMpegCore/FFMpeg/Arguments/PipeArgument.cs index 0751c9e..0fa0f7c 100644 --- a/FFMpegCore/FFMpeg/Arguments/PipeArgument.cs +++ b/FFMpegCore/FFMpeg/Arguments/PipeArgument.cs @@ -31,8 +31,12 @@ namespace FFMpegCore.Arguments public void Post() { Debug.WriteLine($"Disposing NamedPipeServerStream on {GetType().Name}"); - Pipe?.Dispose(); - Pipe = null!; + lock(Pipe) + { + + Pipe?.Dispose(); + Pipe = null!; + } } public async Task During(CancellationToken cancellationToken = default) @@ -48,9 +52,15 @@ namespace FFMpegCore.Arguments finally { Debug.WriteLine($"Disconnecting NamedPipeServerStream on {GetType().Name}"); - if (Pipe is { IsConnected: true }) + lock (Pipe ?? new object()) + //if Pipe is null, then the lock doesnt matter, + //Because the next code will not execute anyways. + //so we can use a new object { - Pipe.Disconnect(); + if (Pipe is { IsConnected: true }) + { + Pipe.Disconnect(); + } } } }