Skip to content

Using File.Exists to check the pipe created will make the NamedPipeClientStream connect fail #69604

@lindexi

Description

@lindexi

Description

We can use File.Exists to check whether the pipe exists, just as the code:

File.Exists(@"\\.\pipe\" + PipeName);

But after we use the File.Exists method, the NamedPipeClientStream may connect fail.

Reproduction Steps

Step 1: Using NamedPipeServerStream to create the Pipe

void StartServer()
{
    var server = new NamedPipeServerStream(PipeName,
        PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous, 1024, 1024);

    server.BeginWaitForConnection(OnWaitForConnection, server);
}

Step 2: Using File.Exists to check the pipe exists.

File.Exists(@"\\.\pipe\" + PipeName);

Step 3: Using NamedPipeClientStream to connect the pipe.

void StartClient()
{
    var localServer = ".";
    var pipeDirection = PipeDirection.InOut;
    var client = new NamedPipeClientStream(localServer,
        PipeName, pipeDirection, PipeOptions.Asynchronous);

    var timeout = 1000 * 5;
    client.Connect(timeout);
}

And you can find the client.Connect(timeout); throw the TimeoutException

All the demo code in: https://github.com/lindexi/lindexi_gd/tree/e26bdd0bced93db8edf8be18f1062e571f3d1861/HallwhallkernarbafejaNakeldibi

Expected behavior

The NamedPipeClientStream can connect the pipe.

Actual behavior

The client.Connect will throw timeout exception.

Regression?

No response

Known Workarounds

No response

Configuration

.NET 6 (all version)

Windows 10.

Other information

Is my method of use wrong?

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.IObugin-prThere is an active PR which will close this issue when it is merged

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions