Skip to content

Commit 63d64de

Browse files
authored
fix: flush a DOWN message if one was present (#832)
1 parent d83b939 commit 63d64de

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

lib/wallaby/session_store.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ defmodule Wallaby.SessionStore do
6868

6969
case result do
7070
[{ref, pid}] ->
71-
true = Process.demonitor(ref)
71+
true = Process.demonitor(ref, [:flush])
7272
:ets.delete(state.ets_table, {ref, session.id, pid})
7373

7474
[] ->

test/wallaby/session_store_test.exs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,44 @@ defmodule Wallaby.SessionStoreTest do
7171
refute Enum.member?(store, second)
7272
assert Enum.member?(store, third)
7373
end
74+
75+
test "flushes DOWN message when demonitoring", %{session_store: session_store, table: table} do
76+
session = %Session{id: "session_to_flush"}
77+
78+
test_pid = self()
79+
80+
owner_pid =
81+
spawn(fn ->
82+
:ok = SessionStore.monitor(session_store, session)
83+
send(test_pid, :monitored)
84+
receive do: (_ -> :ok)
85+
end)
86+
87+
receive do
88+
:monitored -> :ok
89+
after
90+
1000 -> flunk("Failed to monitor session")
91+
end
92+
93+
# Suspend SessionStore so we can queue messages
94+
:sys.suspend(session_store)
95+
96+
# We spawn a task because GenServer.call will block until SessionStore resumes
97+
Task.async(fn ->
98+
SessionStore.demonitor(session_store, session)
99+
end)
100+
101+
Process.sleep(50)
102+
Process.exit(owner_pid, :kill)
103+
Process.sleep(50)
104+
:sys.resume(session_store)
105+
106+
# Ensure all messages are processed (including potentially fatal DOWN)
107+
:sys.get_state(session_store)
108+
109+
# Check if SessionStore is still working
110+
assert [] == SessionStore.list_sessions_for(name: table)
111+
end
74112
end
75113

76114
test "removes sessions when the monitored process dies", %{

0 commit comments

Comments
 (0)