Merge pull request #571 from techtel-pstevens/main

Fixed race condition on Named pipe dispose/disconnect
This commit is contained in:
Malte Rosenbjerg 2025-10-17 15:43:57 +02:00 committed by GitHub
commit cf775ae7a9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -7,6 +7,7 @@ namespace FFMpegCore.Arguments;
public abstract class PipeArgument public abstract class PipeArgument
{ {
private readonly PipeDirection _direction; private readonly PipeDirection _direction;
private readonly object _pipeLock = new();
protected PipeArgument(PipeDirection direction) protected PipeArgument(PipeDirection direction)
{ {
@ -21,6 +22,8 @@ public abstract class PipeArgument
public abstract string Text { get; } public abstract string Text { get; }
public void Pre() public void Pre()
{
lock (_pipeLock)
{ {
if (Pipe != null) if (Pipe != null)
{ {
@ -29,13 +32,17 @@ public abstract class PipeArgument
Pipe = new NamedPipeServerStream(PipeName, _direction, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous); Pipe = new NamedPipeServerStream(PipeName, _direction, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
} }
}
public void Post() public void Post()
{ {
Debug.WriteLine($"Disposing NamedPipeServerStream on {GetType().Name}"); Debug.WriteLine($"Disposing NamedPipeServerStream on {GetType().Name}");
lock (_pipeLock)
{
Pipe?.Dispose(); Pipe?.Dispose();
Pipe = null!; Pipe = null!;
} }
}
public async Task During(CancellationToken cancellationToken = default) public async Task During(CancellationToken cancellationToken = default)
{ {
@ -50,12 +57,15 @@ public abstract class PipeArgument
finally finally
{ {
Debug.WriteLine($"Disconnecting NamedPipeServerStream on {GetType().Name}"); Debug.WriteLine($"Disconnecting NamedPipeServerStream on {GetType().Name}");
lock (_pipeLock)
{
if (Pipe is { IsConnected: true }) if (Pipe is { IsConnected: true })
{ {
Pipe.Disconnect(); Pipe.Disconnect();
} }
} }
} }
}
protected abstract Task ProcessDataAsync(CancellationToken token); protected abstract Task ProcessDataAsync(CancellationToken token);
} }