Opening a file on a remote node

Hello there,

reading the section about “IO and the file system”, I’ve noticed the following paragraph:

By modeling IO devices with processes, the Erlang VM allows I/O messages to be routed between different nodes running Distributed Erlang or even exchange files to perform read/write operations across nodes. Neat!

I’ve tried opening a file on a remote node using :rpc.call, but the returned pid seems to get closed immediately afterwards:

steffen@sdBookAir ~> iex --sname "b"
Erlang/OTP 23 [erts-11.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [dtrace]

Interactive Elixir (1.11.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(b@sdBookAir)1> Node.connect(:"a@sdBookAir")
true
iex(b@sdBookAir)1> {:ok, file} = :rpc.call(:a@sdBookAir, File, :open, ["/tmp/testfile"])
{:ok, #PID<11250.126.0>}
iex(b@sdBookAir)2> :file.read(file, 100)
{:error, :terminated}

Opening the file on the other node and then “transferring” the pid works:

iex(a@sdBookAir)1> {:ok, pid} = File.open("/tmp/testfile")
{:ok, #PID<0.128.0>}
# then on the other node
iex(b@sdBookAir)3> file = pid("11250.128.0")
#PID<11250.128.0>
iex(b@sdBookAir)4> :file.read(file, 100)
{:ok, "hello\n"}

Is there a way to prevent the file opened with :rpc.call from being closed? And if yes, why is it being closed in the first place?

Thanks!

If anyone is wondering, for now I’m using a GenServer to open the files, something like this:

defmodule RemoteFileServer do
  use GenServer

  @impl true
  def init(_) do
    {:ok, %{files: []}}
  end

  @impl true
  def handle_call({:open_file, path, modes}, _from, state) do
    {:ok, file} = File.open(path, modes)
    {:reply, file, update_in(state, [:files], fn files -> [file | files] end)}
  end

  def start_link(_) do
    GenServer.start_link(__MODULE__, [], name: {:global, {__MODULE__, Node.self()}})
  end

  def open(server, path, modes \\ [:read, :binary]) do
    GenServer.call(server, {:open_file, path, modes})
  end
end

Using the pid that the open function returns works perfectly transparent across nodes :smiley:

rpc:call/4 documentation states that:

You cannot make any assumptions about the process that will perform the apply(). It may be the calling process itself, an rpc server, another server, or a freshly spawned process.

File.open/2 documentation states that:

io_device is actually the PID of the process which handles the file. This process monitors the process that originally opened the file (the owner process). If the owner process terminates, the file is closed and the process itself terminates too. If any process to which the io_device is linked terminates, the file will be closed and the process itself will be terminated.

These two combined, I think what happened is that your RPC call started a new process to open the file, then the process was finished and terminated, which also closed the file. Now that you are using a GenServer, it stays alive and thus the opened file also stays open.

2 Likes