Skip to content

Commit

Permalink
Merge pull request #197 from AArnott/closeStreamWithMxChannel
Browse files Browse the repository at this point in the history
Dispose of streams used as mxstream channels when channels close
  • Loading branch information
AArnott authored Jul 9, 2020
2 parents 14832da + 9636686 commit cf51836
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/Nerdbank.Streams.Tests/MultiplexingStreamTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,33 @@ public async Task Dispose_DisposesChannels()
#pragma warning restore CS0618 // Type or member is obsolete
}

[Fact]
public async Task ChannelDispose_ClosesExistingStream()
{
var ms = new MonitoringStream(new MemoryStream());
var disposal = new AsyncManualResetEvent();
ms.Disposed += (s, e) => disposal.Set();

var channel = this.mx1.CreateChannel(new MultiplexingStream.ChannelOptions { ExistingPipe = ms.UsePipe() });
channel.Dispose();
await disposal.WaitAsync(this.TimeoutToken);
}

[Fact]
public async Task RemoteChannelClose_ClosesExistingStream()
{
var ms = new MonitoringStream(new MemoryStream());
var disposal = new AsyncManualResetEvent();
ms.Disposed += (s, e) => disposal.Set();

var ch1 = this.mx1.CreateChannel(new MultiplexingStream.ChannelOptions { ExistingPipe = ms.UsePipe() });
await this.WaitForEphemeralChannelOfferToPropagateAsync();
var ch2 = this.mx2.AcceptChannel(ch1.Id);

ch2.Dispose();
await disposal.WaitAsync(this.TimeoutToken);
}

[Fact]
public async Task CreateChannelAsync_ThrowsAfterDisposal()
{
Expand Down
3 changes: 3 additions & 0 deletions src/Nerdbank.Streams/PipeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -414,10 +414,13 @@ internal static Task LinkToAsync(this PipeReader reader, PipeWriter writer, bool
break;
}
}

await reader.CompleteAsync().ConfigureAwait(false);
}
catch (Exception ex)
{
await writer.CompleteAsync(ex).ConfigureAwait(false);
await reader.CompleteAsync(ex).ConfigureAwait(false);
}
});
}
Expand Down

0 comments on commit cf51836

Please sign in to comment.