Ports communication through IPC - safe to use or likely to be deprecated?

Just realized that BEAM supports communication with external processes using Named Pipes through Ports.

terminal> mkfifo name.pipe
iex> pid = Port.open('name.pipe', [:eof])
iex> Port.command(pid, "Sending data through a pipe to whoever is listening to...\n")

And I was trying to understand whether that could be a reliable feature for doing IPC, but didn’t found any docs about this.

Wondering if this is about to be deprecated or something…? If that’s not the case I’ll probably invest some more time digging into it, and work on it.

3 Likes

I’m looking forward to the discussion.

From what I could tell from the docs, it is possible to interact with FIFOs (and device files, etc) using the file module, but it is recommended against:

While this function can be used to open any file, we recommend against using it for NFS-mounted files, FIFOs, devices, or similar since they can cause IO threads to hang forever.

If your application needs to interact with these kinds of files we recommend breaking out those parts to a port program instead.

(Erlang -- file)

If the lack of docs around open_port(FIFO, Options) is not an omission but a sign it is not supported, I’d be very curious to hear whether interacting with these using ports is the way to go. Perhaps using something like this?

open_port({spawn, "cat in.fifo"}, [in]).
open_port({spawn, "cat > out.fifo"}, [out]).
1 Like

I’ve done some research and it seems like its definitely not something to be considered.

open_port/2 with Atom/String as first arg was deprecated a long time ago and shouldn’t be used anymore, they are called vanilla drivers, assigning ports to atoms/strings that way might crash the whole VM because the only thing that the BIF does is pretty much open a file. It doesn’t have any special treatment for Named Pipes nor blocking files like /dev/urandom

The right way of doing that would be using ports with tuple arguments. {spawn, "..."}

I guess thats it, thanks anyway…

4 Likes

Thank you for researching this and posting your findings!

2 Likes