Skip to content

Commit 775979b

Browse files
Copilotmanast
andauthored
fix(elixir): ignore late QueueEvents task replies during close
Co-authored-by: manast <95200+manast@users.noreply.github.com>
1 parent 1135b2d commit 775979b

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

elixir/lib/bullmq/queue_events.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,11 @@ defmodule BullMQ.QueueEvents do
346346
end
347347
end
348348

349+
def handle_info({ref, _result}, %{closing: true} = state) when is_reference(ref) do
350+
# Ignore late replies from in-flight consumer tasks that were cancelled during close/cleanup.
351+
{:noreply, state}
352+
end
353+
349354
def handle_info({:DOWN, ref, :process, _pid, reason}, %{consumer_task: ref} = state) do
350355
# Consumer task crashed
351356
Logger.error("[BullMQ.QueueEvents] Consumer task crashed: #{inspect(reason)}")

elixir/test/bullmq/queue_events_integration_test.exs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,30 @@ defmodule BullMQ.QueueEventsIntegrationTest do
501501
end
502502
end
503503

504+
# ---------------------------------------------------------------------------
505+
# Close Race Conditions
506+
# ---------------------------------------------------------------------------
507+
508+
describe "close race conditions" do
509+
@tag :integration
510+
@tag timeout: 10_000
511+
test "ignores late consumer task replies after close", %{conn: conn, queue_name: queue_name} do
512+
{:ok, events} =
513+
QueueEvents.start_link(
514+
queue: queue_name,
515+
connection: conn,
516+
prefix: @test_prefix
517+
)
518+
519+
QueueEvents.close(events)
520+
521+
monitor_ref = Process.monitor(events)
522+
send(events, {make_ref(), {:ok, nil}})
523+
524+
refute_receive {:DOWN, ^monitor_ref, :process, ^events, _reason}, 500
525+
end
526+
end
527+
504528
# ---------------------------------------------------------------------------
505529
# Handler Module
506530
# ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)