System.IO.Exception: Pipe is broken

37,002

Solution 1

I'll post my code that seems to work - I was curious since I never did anything with pipes. I didn't find the class you name for the server-side in the relevant namespace, so here's the code based on the NamedPipeServerStream. The callback stuff is just because I couldn't be bothered with two projects.

NamedPipeServerStream s = new NamedPipeServerStream("p", PipeDirection.In);
Action<NamedPipeServerStream> a = callBack;
a.BeginInvoke(s, ar => { }, null);
...
private void callBack(NamedPipeServerStream pipe)
{
  while (true)
  {
    pipe.WaitForConnection();
    StreamReader sr = new StreamReader(pipe);
    Console.WriteLine(sr.ReadToEnd());
    pipe.Disconnect();
  }
}

And the client does this:

using (var pipe = new NamedPipeClientStream(".", "p", PipeDirection.Out))
using (var stream = new StreamWriter(pipe))
{
  pipe.Connect();
  stream.Write("Hello");
}

I can repeat above block multiple times with the server running, no prob.

Solution 2

The problem for me has occurred when I would call pipe.WaitForConnection() from the server, after the client disconnected. The solution is to catch the IOException and call pipe.Disconnect(), and then call pipe.WaitForConnection() again:

while (true)
{
    try
    {
        _pipeServer.WaitForConnection();
        break;
    }
    catch (IOException)
    {
        _pipeServer.Disconnect();
        continue;
    }            
 }
Share:
37,002

Related videos on Youtube

Marcus Mangelsdorf
Author by

Marcus Mangelsdorf

I'm an automation engineer currently working as senior software engineer at a sustainability-focused company in south Germany. I've had a strong relationship with programming since Gymnasium but decided to not focus only on software engineering. So as of now I'm kind of not an expert in any specific field but gathered knowledge in both mechanical engineering (medium to advanced), electrical engineering (basic understanding) and software engineering (advanced). I love to bring these skills together by automating engineering tools like CATIA or Siemens NX. Although I focused on the software side recently. Also I really like to mediate between teams of the different domains, knowing both ways of thought usually makíng it hard for mechanical engineers to talk to software engineers and vice versa.

Updated on July 09, 2022

Comments

  • Marcus Mangelsdorf
    Marcus Mangelsdorf almost 2 years

    I have two .NET applications that talk to each other over a named pipe. Everything is great the first time through, but after the first message is sent, and the server is going to listen again, the WaitForConnection() method throws a System.IO.Exception with message Pipe is broken.
    Why am I getting this exception here? This is my first time working with pipes, but a similar pattern has worked for me in the past with sockets.

    Code ahoy!
    Server:

    using System.IO.Pipes;
    
    static void main()
    {
        var pipe = new NamedPipeServerStream("pipename", PipeDirection.In);
        while (true)
        {
            pipe.Listen();
            string str = new StreamReader(pipe).ReadToEnd();
            Console.Write("{0}", str);
        }
    }
    

    Client:

    public void sendDownPipe(string str)
    {
        using (var pipe = new NamedPipeClientStream(".", "pipename", PipeDirection.Out))
        {
            using (var stream = new StreamWriter(pipe))
            {
                stream.Write(str);
            }
        }
    }
    

    The first call to sendDownPipe gets the server to print the message I send just fine, but when it loops back up to listen again, it poops.

    • user3141326
      user3141326 about 8 years
      I think the reason you are having this issue is because of the line "new StreamReader(pipe)". The scope of created stream reader is the first while loop and it is then recreated. However stream readers behavior is to close the stream they are wrapping - thus once it is out of scope it will close the pipe stream. You can try moving its declaration out of the while loop and check (P.S: I did not personally try if the code works if you do so - just wanted to add a comment rather than answer)
  • Admin
    Admin almost 15 years
    That did the trick. I guess theres no implicit disconnect when a client falls off the other end. Thanks.
  • Mike Pateras
    Mike Pateras about 14 years
    That helped me, too. Thanks for the great answer!
  • eliah
    eliah over 12 years
    This gives me an error that the pipe isn't connected on this line: using (var stream = new StreamWriter(pipe))
  • Sean
    Sean almost 7 years
    Doesn't seem to work between processes. The pipe.Disconnect() blows up with a "Cannot access a closed pipe"
  • Sean
    Sean almost 7 years
    Here's what I ended up doing: stackoverflow.com/a/44828800/1641247
  • Michael
    Michael over 4 years
    @Sean, when your StreamWriter is disposed by the using(), the underlying pipe is closed, too. You can avoid that by creating the StreamWriter with new StreamWriter(pipe, Encoding.UTF8, -1, true). And you need to create the reader with a similar overload.
  • Jirka Picek
    Jirka Picek almost 3 years
    If you don't use using consider using Dispose(). Hoping that GC will free the memory is a bad approach.