Skip to content

Commit b3de41e

Browse files
committed
When AnonymousPipeServerStream is created with HandleInheritability.Inheritable, its Dispose method should dispose the client no matter if it was exposed or not.
This is going to allow users who did not call DisposeLocalCopyOfClientHandle to avoid resource leaks.
1 parent c2a2bb2 commit b3de41e

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/libraries/System.IO.Pipes/src/System/IO/Pipes/AnonymousPipeServerStream.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System.Diagnostics.CodeAnalysis;
54
using Microsoft.Win32.SafeHandles;
65

76
namespace System.IO.Pipes
@@ -13,6 +12,7 @@ public sealed partial class AnonymousPipeServerStream : PipeStream
1312
{
1413
private SafePipeHandle _clientHandle = null!;
1514
private bool _clientHandleExposed;
15+
private readonly HandleInheritability _inheritability;
1616

1717
public AnonymousPipeServerStream()
1818
: this(PipeDirection.Out, HandleInheritability.None, 0)
@@ -73,6 +73,7 @@ public AnonymousPipeServerStream(PipeDirection direction, HandleInheritability i
7373
}
7474

7575
Create(direction, inheritability, bufferSize);
76+
_inheritability = inheritability;
7677
}
7778

7879
~AnonymousPipeServerStream()
@@ -121,10 +122,10 @@ protected override void Dispose(bool disposing)
121122
{
122123
try
123124
{
124-
// We should dispose of the client handle if it was not exposed.
125-
if (!_clientHandleExposed && _clientHandle != null && !_clientHandle.IsClosed)
125+
// We should dispose of the client handle if it was created inheritable or it was not exposed at all.
126+
if (_inheritability == HandleInheritability.Inheritable || !_clientHandleExposed)
126127
{
127-
_clientHandle.Dispose();
128+
DisposeLocalCopyOfClientHandle();
128129
}
129130
}
130131
finally

0 commit comments

Comments
 (0)