Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hanging of NamedPipeClientStream when connecting with infinite timeout #66877

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private void ConnectInternal(int timeout, CancellationToken cancellationToken, i
cancellationToken.ThrowIfCancellationRequested();

// Determine how long we should wait in this connection attempt
int waitTime = timeout - elapsed;
int waitTime = timeout == Timeout.Infinite ? CancellationCheckInterval : timeout - elapsed;
adamsitnik marked this conversation as resolved.
Show resolved Hide resolved
if (cancellationToken.CanBeCanceled && waitTime > CancellationCheckInterval)
{
waitTime = CancellationCheckInterval;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,31 @@ public async Task ClientConnectAsync_With_Cancellation_Throws_Timeout_When_Pipe_
}
}

[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // Unix implementation doesn't rely on a timeout and cancellation token when connecting
public async Task ClientConnectAsync_Cancel_With_InfiniteTimeout()
cranberry-clockworks marked this conversation as resolved.
Show resolved Hide resolved
{
string pipeName = PipeStreamConformanceTests.GetUniquePipeName();

using (var cts = new CancellationTokenSource())
using (var server = new NamedPipeServerStream(pipeName, PipeDirection.InOut, 1))
using (var firstClient = new NamedPipeClientStream(pipeName))
using (var secondClient = new NamedPipeClientStream(pipeName))
{
var firstConnectionTasks = new Task[]
{
firstClient.ConnectAsync(),
server.WaitForConnectionAsync()
};

Assert.True(Task.WaitAll(firstConnectionTasks, 1000));

cts.CancelAfter(100);

await Assert.ThrowsAsync<OperationCanceledException>(() => secondClient.ConnectAsync(cts.Token)).WaitAsync(1000);
}
}

public static IEnumerable<object[]> GetCancellationTokens =>
new []
{
Expand Down