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
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement