steffend

steffend

Phoenix Core Team

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!

Marked As Solved

Nicd

Nicd

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.

Also Liked

steffend

steffend

Phoenix Core Team

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:

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54092 488
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42576 114
New

We're in Beta

About us Mission Statement