Works in the editor, but after building it doesn't

I’m using unix named pipes (fifo files) to communicate with an external process from the program. To do that I’m using a FileStream interface. When debugging in the unity editor it works perfectly, but then I build, it leads to file locking, requiring to open the file again (To do this I just reload the scene, because I have a button for that). Building in the development mode doesn’t help.

Here is an example of reading from a fifo file:

using (FileStream fs = new FileStream("/tmp/"+pipeName, FileMode.Open, FileAccess.Read)) {
    while (quitThread.IsEmpty) {
        if (await fs.ReadAsync(contents, 0, 1, token) == 1) {
            status = (int)contents[0] == 1;
            enableQueue.Enqueue(status);
        }
        Thread.Sleep(100);
    }
}

Opening a file using File.OpenRead doesn’t affect the behavior.

Because you can read from a file in parallel but only one process is allowed to write to a file/stream that‘s open for writing. So while you are writing from Unity the external process can‘t read the file, and vice versa.

One way to work around this is to catch the permission exceptions and simply try again in a few ms - but this only works if the writing app only write‘s a few things at a time and closes the file handle afterwards rather than keeping the stream open.

If that won‘t work look into interprocess communication. It requires both apps to work together but it bypasses the need for writing to a file making it a lot faster.

Well it’s a fifo file, so it’s required for one process to be trying to write into it, to even open a file (the code waits at line 2, until there is another file trying to write into the pipe) and it works while testing in the editor as there is only one process trying to write into it. Names pipes (fifo files) are a type of interprocess communication, the only problem is that C# doesn’t implement them, so you have to do it manually (it has a NamedPipe interface, but it uses unix sockets under the hood to communicate instead of actual named pipes).

Also there are no errors. The other process is trying to send data to the pipe constantly until it succeeds.

Try using the FileStream constructor with the fourth FileAccess parameter also set to Read. I vaguely remember you have to be very specific about „just gonna read from it“ with parallel file access.

Otherwise I‘d put in a lot of logs to pinpoint where exactly it stops working.

Btw what is quitThread, a concurrentQueue?
And what about the ReadAsync token? You‘re not checking its status.