1
1
// Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
- using System . Diagnostics . CodeAnalysis ;
5
4
using Microsoft . Win32 . SafeHandles ;
6
5
7
6
namespace System . IO . Pipes
@@ -12,7 +11,8 @@ namespace System.IO.Pipes
12
11
public sealed partial class AnonymousPipeServerStream : PipeStream
13
12
{
14
13
private SafePipeHandle _clientHandle = null ! ;
15
- private bool _clientHandleExposed ;
14
+ private bool _clientHandleExposed , _clientHandleExposedAsString ;
15
+ private readonly HandleInheritability _inheritability ;
16
16
17
17
public AnonymousPipeServerStream ( )
18
18
: this ( PipeDirection . Out , HandleInheritability . None , 0 )
@@ -73,6 +73,7 @@ public AnonymousPipeServerStream(PipeDirection direction, HandleInheritability i
73
73
}
74
74
75
75
Create ( direction , inheritability , bufferSize ) ;
76
+ _inheritability = inheritability ;
76
77
}
77
78
78
79
~ AnonymousPipeServerStream ( )
@@ -84,7 +85,7 @@ public AnonymousPipeServerStream(PipeDirection direction, HandleInheritability i
84
85
// processes. For now, people do it via command line arguments.
85
86
public string GetClientHandleAsString ( )
86
87
{
87
- _clientHandleExposed = true ;
88
+ _clientHandleExposedAsString = _clientHandleExposed = true ;
88
89
GC . SuppressFinalize ( _clientHandle ) ;
89
90
return _clientHandle . DangerousGetHandle ( ) . ToString ( ) ;
90
91
}
@@ -121,10 +122,11 @@ protected override void Dispose(bool disposing)
121
122
{
122
123
try
123
124
{
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 when it was not exposed at all OR
126
+ // it was exposed as a string (handle finalization has been suppressed) and created inheritable (out-of-proc communication).
127
+ if ( ! _clientHandleExposed || ( _clientHandleExposedAsString && _inheritability == HandleInheritability . Inheritable ) )
126
128
{
127
- _clientHandle . Dispose ( ) ;
129
+ DisposeLocalCopyOfClientHandle ( ) ;
128
130
}
129
131
}
130
132
finally
0 commit comments