Skip to content

fix: ensure cleanup after shutdown - #108

Merged
zmstone merged 3 commits into
kafka4beam:mainfrom
zmstone:251122-ensure-cleanup-after-shutdown
Nov 24, 2025
Merged

fix: ensure cleanup after shutdown#108
zmstone merged 3 commits into
kafka4beam:mainfrom
zmstone:251122-ensure-cleanup-after-shutdown

Conversation

@zmstone

@zmstone zmstone commented Nov 22, 2025

Copy link
Copy Markdown
Contributor
  • Ensure wolff_client_sup:ensure_absence and wolff_producers_sup:ensure_absence will perform shutdown and cleanup atomically.
    Previously, if the caller process is killed while waiting for shutdown, a terminated child may leak under the supervisor.
  • Fixed a race condition which may cause {Exit, Supervisor, shutdown} message get consumed, causing wolff_client_sup:ensure_absence to force kill instead of normal shutdown.

@zmstone
zmstone force-pushed the 251122-ensure-cleanup-after-shutdown branch from c117a35 to 59acb5d Compare November 24, 2025 10:51
Comment thread src/wolff_client.erl Outdated
Comment on lines +204 to +206
handle_info({'EXIT', Pid, Reason}, #{owner := Pid} = St) ->
%% If owner is not supervisor
{stop, Reason, St};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be possible in a gen_server, IIRC.

Indeed, a quick check indicates that's the case:

defmodule A do
  use GenServer

  def start_link() do
    GenServer.start_link(__MODULE__, self())
  end

  def init(owner) do
    Process.flag(:trap_exit, true)
    {:ok, %{owner: owner}}
  end

  def handle_info({:EXIT, pid, reason}, %{owner: owner} = st) do
    IO.inspect({owner, pid, reason})
    {:noreply, st}
  end
end

Process.flag(:trap_exit, true)
{:ok, pid0} = A.start_link()

# not the owner
spawn(fn -> Process.exit(pid0, :die_not_owner) end)

# owner tells it to die
Process.exit(pid0, :die_from_owner)

Output (does not print inspect when exit signal comes from owner):

iex(5)> # not the owner
nil
iex(6)> spawn(fn -> Process.exit(pid0, :die_not_owner) end)
{#PID<0.105.0>, #PID<0.112.0>, :die_not_owner}
#PID<0.112.0>
iex(7)> # owner tells it to die
nil
iex(8)> Process.exit(pid0, :die_from_owner)
true

12:00:03.369 [error] GenServer #PID<0.111.0> terminating
** (stop) :die_from_owner
Last message: {:EXIT, #PID<0.105.0>, :die_from_owner}
State: %{owner: #PID<0.105.0>}

@zmstone
zmstone merged commit 523bc97 into kafka4beam:main Nov 24, 2025
4 checks passed
@zmstone
zmstone deleted the 251122-ensure-cleanup-after-shutdown branch November 24, 2025 16:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants