Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions src/MsBuildPipeLogger.Server/PipeBuffer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Concurrent;
using System.IO;
using System.IO.Pipes;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -119,15 +120,27 @@ public Buffer(byte[] buffer, int offset, int count)

public int FillFromStream(Stream stream, CancellationToken cancellationToken)
{
Count = cancellationToken.Try(
() =>
{
_offset = 0;
Task<int> readTask = stream.ReadAsync(_buffer, _offset, BufferSize, cancellationToken);
readTask.Wait(cancellationToken);
return readTask.Status == TaskStatus.Canceled ? 0 : readTask.Result;
},
() => 0);
if (stream is AnonymousPipeServerStream || stream is AnonymousPipeClientStream)
{
// We can't use ReadAsync with Anonymous PipeStream
// https://github.com/dotnet/runtime/issues/23638
// https://docs.microsoft.com/en-us/windows/win32/ipc/anonymous-pipe-operations
// Asynchronous (overlapped) read and write operations are not supported by anonymous pipes
_offset = 0;
Count = cancellationToken.IsCancellationRequested ? 0 : stream.Read(_buffer, _offset, BufferSize);
}
else
{
Count = cancellationToken.Try(
() =>
{
_offset = 0;
Task<int> readTask = stream.ReadAsync(_buffer, _offset, BufferSize, cancellationToken);
readTask.Wait(cancellationToken);
return readTask.Status == TaskStatus.Canceled ? 0 : readTask.Result;
},
() => 0);
}
return Count;
}

Expand Down
19 changes: 0 additions & 19 deletions tests/MsBuildPipeLogger.Tests/IntegrationFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,6 @@ public void SerializesData([ValueSource(nameof(MessageCounts))] int messageCount
}
}

[Test]
public void AnonymousPipeSupportsCancellation()
{
// Given
BuildEventArgs buildEvent = null;
using (CancellationTokenSource tokenSource = new CancellationTokenSource())
{
using (AnonymousPipeLoggerServer server = new AnonymousPipeLoggerServer(tokenSource.Token))
{
// When
tokenSource.CancelAfter(1000); // The call to .Read() below will block so need to set a timeout for cancellation
buildEvent = server.Read();
}
}

// Then
buildEvent.ShouldBeNull();
}

[Test]
public void NamedPipeSupportsCancellation()
{
Expand Down