Why do Pipes Hang When I try to Read Them?

Viewed 74

I am making a program that starts a child process and communicates via anonymous pipes. When I read from a pipe, the program hangs at the first ReadLine() call as seen in the following code method:

// Reads messages sent from module's process via anonymous pipe
        internal string[] ReadPipe() {
            try {
                Log.Verbose("Checking read pipe");

                // Check for sync message and return null if there is no message to receive
                string pipeMessage = _pipeInReader.ReadLine(); // HANGS ON THIS LINE
                if(pipeMessage == null || !pipeMessage.StartsWith("SYNC")) {
                    Log.Verbose("No message found in pipe");
                    return null;
                }

                // Return array of message lines
                Log.Verbose("Received message from module {ModuleName}", _fileInfo.Name);
                List<string> pipeMessageLines = new();
                do {
                    pipeMessage = _pipeInReader.ReadLine();
                    pipeMessageLines.Add(pipeMessage);
                    Log.Debug(pipeMessage);
                } while(pipeMessage != null && !pipeMessage.StartsWith("END"));

                return pipeMessageLines.ToArray();
            } catch(Exception e) {
                Log.Error(e.ToString());
                return null;
            }
        }

The code I am using to write to the pipe is the following:

// Sends a message to module's process via anonymous pipe
        public static void WritePipe(string message) {
            try {
                Log.Verbose("Sending \"{Message}\" to kit pipe", message);
                
                // Send sync message and wait for module process to receive it
                Log.Verbose("Waiting for pipe drain");
                _pipeOutWriter.Write("SYNC");
                _pipeOut.WaitForPipeDrain();

                // Send the specified message
                Log.Verbose("Pipe drained. Sending message");
                _pipeOutWriter.Write(message);
                _pipeOutWriter.Write("END");

            } catch(Exception e) {
                Log.Error(e.ToString());
            }
        }

Why does it hang at that ReadLine() line? Thanks in advance!

1 Answers

Without a proper minimal, reproducible example, it's impossible to say for sure. However, one glaring problem with your code is that when you write to the _pipeOutWriter object, you don't call Flush(). Assuming that's a TextWriter, by default it's going to buffer the data until the internal buffer is full, and not send anything to the underlying Stream until then.

By calling Flush(), you force it to flush its internal buffer and send the data right away.

If that does not address your question, please edit the question to improve it, making sure to provide a minimal, reproducible example, and any additional details about what you've tried so far to fix the problem and what specifically you need help with.

Related