PipeServer crashing with many connections, IOException: 'Pipe is broken'
See original GitHub issueDescribe the bug
I have a program with a client that sends a message and waits for a response, and a server that waits for clients, and when one sends a message, the server sends a response.
Sometimes the server crashes when it tries to send a response with args.Connection.WriteAsync
at System.IO.Pipes.PipeStream.CheckWriteOperations()
at System.IO.Pipes.PipeStream.Flush()
at System.IO.Pipes.PipeStream.FlushAsync(CancellationToken cancellationToken)
--- End of stack trace from previous location ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at H.Pipes.IO.PipeStreamWriter.<WriteAsync>d__8.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at H.Pipes.IO.PipeStreamWrapper.<WriteAsync>d__17.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at H.Pipes.PipeConnection`1.<WriteAsync>d__38.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Program.<>c.<<<Main>$>b__0_2>d.MoveNext() in C:\Users\Erez\source\repos\HPipeMultiClient\Server\Server.cs:line 21
Happens when running the server in debug from Visual Studio. Doesn’t happen when running the server from console.
Steps to reproduce the bug
- In Visual Studio 2022, Open this minimal solution that reproduces the problem: https://github.com/erezwanderman/HPipeMultiClient
- Build the solution and start debugging (the Server project)
- Start Client.exe outside of Visual Studio
- Watch the server crash on line 21
server.MessageReceived += async (sender, args) => { Console.WriteLine($“Client {args.Connection.PipeName} says: {args.Message}”); var response = new Message { Response = args.Message!.Request + args.Message!.Request }; Console.WriteLine($“Sending to {args.Connection.PipeName}: {response}”); await args.Connection.WriteAsync(response); };
Expected behavior
No crash, send message successfully
Screenshots
NuGet package version
2.0.37
Platform
Console
IDE
Visual Studio 2022
Additional context
No response
Issue Analytics
- State:
- Created a year ago
- Comments:13 (5 by maintainers)
I have released a new version with a suggested fix, please check it out. The ideal situation would be if you could write a test that fails in the version of the library before this fix, so that the situation does not happen again and I do not miss anything.
Replace
Task.WaitAll(tasks);
toawait Task.WhenAll(tasks);
.