Skip to content

Commit

Permalink
Remove :eof from the port
Browse files Browse the repository at this point in the history
  • Loading branch information
akash-akya committed Sep 20, 2024
1 parent d0b2a94 commit 84f5ce0
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 21 deletions.
52 changes: 32 additions & 20 deletions lib/ex_cmd/process.ex
Original file line number Diff line number Diff line change
Expand Up @@ -725,12 +725,15 @@ defmodule ExCmd.Process do
:ok = GenServer.reply(caller, {:error, :epipe})
end)

send(state.owner, {state.exit_ref, {:error, reason}})
state = State.set_status(state, {:exit, {:error, reason}})
state = set_exit_status(state, {:error, reason})

maybe_shutdown(state)
end

def handle_info({:EXIT, port, :normal}, %State{port: port} = state) do
maybe_shutdown(state)
end

# shutdown unconditionally when process owner exit normally.
# Since ExCmd process is linked to the owner, in case of owner crash,
# ex_cmd process will be killed by the VM.
Expand Down Expand Up @@ -847,25 +850,15 @@ defmodule ExCmd.Process do
end

def handle_command(:exit_status, exit_status, state) do
ret =
cond do
exit_status >= 0 ->
{:exit, exit_status}

exit_status == -1 ->
{:exit, {:error, :killed}}
end

state = State.set_status(state, ret)

if state.stdout_status == :closed do
Operations.pending_callers(state)
|> Enum.each(fn caller ->
:ok = GenServer.reply(caller, {:error, :epipe})
end)
Operations.pending_callers(state)
|> Enum.each(fn caller ->
:ok = GenServer.reply(caller, {:error, :epipe})
end)

send(state.owner, {state.exit_ref, ret})
end
state =
state
|> State.set_stdout_status(:closed)
|> set_exit_status({:ok, exit_status})

maybe_shutdown(state)
end
Expand Down Expand Up @@ -907,4 +900,23 @@ defmodule ExCmd.Process do
{timeout - kill_timeout, kill_timeout}
end
end

@spec set_exit_status(State.t(), {:error, term} | {:ok, integer}) :: State.t()
defp set_exit_status(state, status) do
status =
case status do
{:error, reason} ->
{:error, reason}

{:ok, -1} ->
{:error, :killed}

{:ok, exit_status} when is_integer(exit_status) and exit_status >= 0 ->
{:ok, exit_status}
end

send(state.owner, {state.exit_ref, status})

State.set_status(state, {:exit, status})
end
end
2 changes: 1 addition & 1 deletion lib/ex_cmd/process/proto.ex
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ defmodule ExCmd.Process.Proto do

defp start_odu_port(odu_path, cmd_with_args, opts) do
args = build_odu_params(opts) ++ ["--" | cmd_with_args]
options = [:use_stdio, :exit_status, :binary, :hide, :eof, {:packet, 4}, args: args]
options = [:use_stdio, :exit_status, :binary, :hide, {:packet, 4}, args: args]
Port.open({:spawn_executable, odu_path}, options)
end

Expand Down
Binary file modified priv/odu_darwin_amd64
Binary file not shown.
Binary file modified priv/odu_darwin_arm64
Binary file not shown.
Binary file modified priv/odu_linux_amd64
Binary file not shown.
Binary file modified priv/odu_linux_arm64
Binary file not shown.
Binary file modified priv/odu_windows_amd64.exe
Binary file not shown.
Binary file modified priv/odu_windows_arm64.exe
Binary file not shown.

0 comments on commit 84f5ce0

Please sign in to comment.