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

Tee
can someone please explain to me how Enum.reduce works with maps
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
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
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42842 311
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
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
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35953 110
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
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