ConnorRigby
I have a GenServer that reads/writes to a Linux Pipe or Fifo. I need to do:
{:ok, fifo} = :file.open('/path/to/fifo', [:read, :write, :binary])
{:ok, <<protocol_pattern_match>>} = :file.read(fifo, protocol_size)
Except :file.read/2 blocks the entire calling process until it is complete similar to read() in C. This is fine because i should be able to just do task = Task.async(:file, :read, [protocol_size]) and get the result in
handle_info/2. Maybe i misunderstood the docs, but that doesn’t seem to be working for me. I’m expecting to get handle_info({ref, {:ok, <<protocol_pattern_match>>}, %{task: %{ref: ref}}) but that doesn’t seem to happen.
My other issue is that Task.shutdown/2 or :file.close/1 do not seem to be working as expected.
the docs for Task.shutdown/2 say when a calling process exits, the task should exit, but even using
Task.shutdown(state.task, :brutal_kill) doesn’t allow me to call :file.close(state.fifo) in terminate/2. (It just blocks forever)
anyway here’s the entire GenServer implementation:
defmodule PipeWorker do
@moduledoc """
Proxy for IO operations.
"""
use GenServer
require Logger
def start_link(pipe_name) do
GenServer.start_link(__MODULE__, pipe_name)
end
def close(pipe) do
GenServer.stop(pipe, :normal)
end
def read(pipe, size) do
GenServer.call(pipe, {:read, [size]}, :infinity)
end
def write(pipe, packet) do
GenServer.call(pipe, {:write, [packet]}, :infinity)
end
def init(pipe_name) do
with {_, 0} <- System.cmd("mkfifo", [pipe_name]),
{:ok, pipe} <- :file.open(to_charlist(pipe_name), [:read, :write, :binary]) do
{:ok, %{pipe_name: pipe_name, pipe: pipe, task: nil, caller: nil}}
else
{:error, _} = error -> {:stop, error}
{_, _num} -> {:stop, {:error, "mkfifo"}}
end
end
def terminate(_, state) do
Logger.warn("PipeWorker #{state.pipe_name} exit")
state.task && Task.shutdown(state.task, :brutal_kill)
Logger.warn("Pipe Task shut down")
IO.inspect(state.pipe, label: "pipe")
# :file.close(state.pipe) # blocks indefinitely no matter what. Shell becomes unresponsive.
# Logger.warn("Pipe closed")
File.rm!(state.pipe_name)
Logger.warn("Pipe removed")
end
def handle_call({cmd, args}, {pid, _} = _from, state) do
IO.inspect([state.pipe | args], label: "Pipe task args")
task = Task.async(:file, cmd, [state.pipe | args])
IO.inspect(task, label: "Pipe task")
{:reply, task.ref, %{state | task: task, caller: pid}}
end
# This is never called?
def handle_info({ref, result}, %{task: %{ref: ref}, caller: pid} = state) do
IO.inspect({ref, result}, label: "Task result")
send(pid, {__MODULE__, ref, result})
{:noreply, %{state | task: nil, caller: nil}}
end
end
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #phoenix_html
- #elixirconf-us
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
ConnorRigby
Okay so while investigating this, i discovered something.
This code will hang forever no matter what. It doesn’t matter how much data
is written to the pipe, the data will never be read by Erlang/Elixir.
This code however works fine. I suspect this is because the
:rawoption is a NIF now. (just a guess?)The only problem with opening in
:rawmode is that i can’t useTaskto callread/2because theTaskis not an owning process. I get the following error:I guess this isn’t really an Elixir issue, but an Erlang one but any help would be appreciated.
idi527
Doesn’t that need to be used with
Task.awaitto receive the result? I also think tasks usereceiveblocks that wouldn’t work in a genserver process.ConnorRigby
From the Task docs:
idi527
Oh, sorry then.
ConnorRigby
if i manually change my code to do a
:file.read(fd, 0)the behaviour works. I recieve the result inhandle_info, so i at least am fairly certainTaskis not the problem here.josevalim
The solution is to use ports, which will also give asynchrony. See previous discussion here: Elixir vs Unix named pipe
I think it was recently announced that erlang supports pipes, but I cant recall if in port or file. Or I may be completely misremebering it. Anyway, a port should do nowadays.
ConnorRigby
Sorry, i should have stated this in the original post, but i have tried this as well and it did not work either.
and in another terminal, writing to the FIFO just hangs until i send a
ctrl+cjosevalim
So this worked:
After typing the receive, I ran
echo "foo" > test.pipein another terminal. Could it be something related to permissions as the error message says?ConnorRigby
ah ha! i was trying to use
spawnwhich was not needed. Will try this method out and report back.ConnorRigby
I can confirm that this does in fact allow reading and writing from a named pipe. However it does add the requirement of buffering now, since you are not telling the port how much data you want to read. Either way thanks for the help!