Skip to content

Commit

Permalink
fix(elixir-client): link request & monitor so client never stalls (#2096
Browse files Browse the repository at this point in the history
)
  • Loading branch information
icehaunter authored Dec 4, 2024
1 parent e840b5f commit 6d9b73b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/eleven-weeks-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@core/elixir-client": patch
---

fix: make sure the client is not stuck when the request dies for some reason
16 changes: 15 additions & 1 deletion packages/elixir-client/lib/electric/client/fetch/monitor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ defmodule Electric.Client.Fetch.Monitor do

@impl true
def init(request_id) do
Process.flag(:trap_exit, true)

state = %{
request_id: request_id,
subscribers: []
Expand Down Expand Up @@ -107,7 +109,7 @@ defmodule Electric.Client.Fetch.Monitor do
Logger.debug(fn ->
[
message:
"#{inspect(pid)} exited with reason #{inspect(reason)}. Removing from subscribers",
"Listener #{inspect(pid)} exited with reason #{inspect(reason)}. Removing from subscribers",
request_id: state.request_id
]
end)
Expand All @@ -119,4 +121,16 @@ defmodule Electric.Client.Fetch.Monitor do

{:noreply, state}
end

def handle_info({:EXIT, pid, reason}, state) do
Logger.debug(fn ->
"Request process #{inspect(pid)} exited with reason #{inspect(reason)} before issuing a reply. Using reason as an error and exiting."
end)

for {pid, ref} <- state.subscribers do
send(pid, {:response, ref, {:error, reason}})
end

{:stop, :normal, state}
end
end
2 changes: 2 additions & 0 deletions packages/elixir-client/lib/electric/client/fetch/request.ex
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ defmodule Electric.Client.Fetch.Request do
"Starting request for #{inspect(request_id)}"
end)

Process.link(monitor_pid)

state = %{
request_id: request_id,
request: request,
Expand Down

0 comments on commit 6d9b73b

Please sign in to comment.