Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 709930a

Browse files
author
Ian Hays
committed
Merge pull request #2873 from ianhays/pipes
Unix NamedServerPipeStream disposal no longer deletes the FIFO
2 parents 4f8a814 + 4f88d4b commit 709930a

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

src/System.IO.Pipes/src/System/IO/Pipes/NamedPipeServerStream.Unix.cs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ namespace System.IO.Pipes
1515
/// </summary>
1616
public sealed partial class NamedPipeServerStream : PipeStream
1717
{
18-
private bool _createdFifo;
1918
private string _path;
2019
private PipeDirection _direction;
2120
private PipeOptions _options;
@@ -50,7 +49,9 @@ private void Create(string pipeName, PipeDirection direction, int maxNumberOfSer
5049
int result = Interop.libc.mkfifo(_path, (int)Interop.libc.Permissions.S_IRWXU);
5150
if (result == 0)
5251
{
53-
_createdFifo = true;
52+
// The FIFO was successfully created - note that although we create the FIFO here, we don't
53+
// ever delete it. If we remove the FIFO we could invalidate other servers that also use it.
54+
// See #2764 for further discussion.
5455
break;
5556
}
5657

@@ -121,17 +122,6 @@ public void Disconnect()
121122
InitializeHandle(null, false, false);
122123
}
123124

124-
protected override void Dispose(bool disposing)
125-
{
126-
// If we created the FIFO object, be a good citizen and clean it up.
127-
// If this doesn't happen, worst case is we leave a temp file around.
128-
if (_createdFifo && _path != null)
129-
{
130-
Interop.libc.unlink(_path); // ignore any errors
131-
}
132-
base.Dispose(disposing);
133-
}
134-
135125
// Gets the username of the connected client. Not that we will not have access to the client's
136126
// username until it has written at least once to the pipe (and has set its impersonationLevel
137127
// argument appropriately).

src/System.IO.Pipes/tests/NamedPipeTests/NamedPipeTest.Specific.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,5 +397,40 @@ public void InvalidReadMode_Throws_ArgumentOutOfRangeException(PipeDirection ser
397397
Assert.Throws<ArgumentOutOfRangeException>(() => client.ReadMode = (PipeTransmissionMode)999);
398398
}
399399
}
400+
401+
[Fact]
402+
[PlatformSpecific(PlatformID.AnyUnix)]
403+
public void Unix_MultipleServerDisposal_DoesntDeletePipe()
404+
{
405+
// Test for when multiple servers are created and linked to the same FIFO. The original ServerStream
406+
// that created the FIFO is disposed. The other servers should still be valid and useable.
407+
string serverName = GetUniquePipeName();
408+
409+
var server1 = new NamedPipeServerStream(serverName, PipeDirection.In); // Creates the FIFO
410+
var server2 = new NamedPipeServerStream(serverName, PipeDirection.In);
411+
var client1 = new NamedPipeClientStream(".", serverName, PipeDirection.Out);
412+
var client2 = new NamedPipeClientStream(".", serverName, PipeDirection.Out);
413+
414+
Task server1Task = server1.WaitForConnectionAsync(); // Opens a handle to the FIFO
415+
Task server2Task = server2.WaitForConnectionAsync(); // Opens a handle to the same FIFO as server1
416+
417+
Task client1Task = client1.ConnectAsync();
418+
Task.WaitAll(server1Task, server2Task, client1Task); // client1 connects to server1 AND server2
419+
420+
Assert.True(server1.IsConnected);
421+
Assert.True(server2.IsConnected);
422+
423+
// Get rid of client1/server1 and make sure server2 isn't connected (so that it can connect to client2)
424+
server1.Dispose();
425+
client1.Dispose();
426+
server2.Disconnect();
427+
Assert.False(server2.IsConnected);
428+
429+
server2Task = server2.WaitForConnectionAsync(); // Opens a handle to the same FIFO
430+
Task client2Task = client2.ConnectAsync();
431+
Task.WaitAll(server2Task, client2Task); // Should not block!
432+
Assert.True(server2.IsConnected);
433+
}
434+
400435
}
401436
}

0 commit comments

Comments
 (0)