Skip to content

Commit 55dc45c

Browse files
mkuratczykkjnilsson
authored andcommitted
Fix channel crash when publishing to a new stream (#12969)
The following scenario led to a channel crash: 1. Publish to a non-existing stream: `perf-test -y 0 -p -e amq.default -t direct -k stream` 2. Declare the stream: `rabbitmqadmin declare queue name=stream queue_type=stream` There is no pid yet, so we got a function_clause with `none` ``` {function_clause, [{osiris_writer,write, [none,<0.877.0>,<<"<0.877.0>_-65ZKFz18ll5lau0phi7CsQ">>,1, [[0,"Sp",[192,6,5,"B@@ac"]], [0,"Sr", [193,38,4, [[[163,10,<<"x-exchange">>],[161,0,<<>>]], [[163,13,<<"x-routing-key">>],[161,6,<<"stream">>]]]]], [0,"Su",[160,12,[<<0,19,252,1,0,0,98,171,20,16,108,167>>]]]]], [{file,"src/osiris_writer.erl"},{line,158}]}, {rabbit_stream_queue,deliver0,4, [{file,"rabbit_stream_queue.erl"},{line,540}]}, {rabbit_stream_queue,'-deliver/3-fun-0-',4, [{file,"rabbit_stream_queue.erl"},{line,526}]}, {lists,foldl,3,[{file,"lists.erl"},{line,2146}]}, {rabbit_queue_type,'-deliver0/4-fun-5-',5, [{file,"rabbit_queue_type.erl"},{line,707}]}, {maps,fold_1,4,[{file,"maps.erl"},{line,860}]}, {rabbit_queue_type,deliver0,4, [{file,"rabbit_queue_type.erl"},{line,704}]}, {rabbit_queue_type,deliver,4, [{file,"rabbit_queue_type.erl"},{line,662}]}]} ``` Co-authored-by: Karl Nilsson <[email protected]> (cherry picked from commit 68de3fd)
1 parent 9cf304c commit 55dc45c

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

deps/rabbit/src/rabbit_stream_queue.erl

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -923,8 +923,31 @@ readers(QName) ->
923923
{node(), 0}
924924
end.
925925

926+
get_writer_pid(Q) ->
927+
case amqqueue:get_pid(Q) of
928+
none ->
929+
%% the stream is still starting; wait up to 5 seconds
930+
%% and ask the coordinator as it has the Pid sooner
931+
#{name := StreamId} = amqqueue:get_type_state(Q),
932+
get_writer_pid(StreamId, 50);
933+
Pid ->
934+
Pid
935+
end.
936+
937+
get_writer_pid(_StreamId, 0) ->
938+
stream_not_found;
939+
get_writer_pid(StreamId, N) ->
940+
case rabbit_stream_coordinator:writer_pid(StreamId) of
941+
{ok, Pid} ->
942+
Pid;
943+
_ ->
944+
timer:sleep(100),
945+
get_writer_pid(StreamId, N - 1)
946+
end.
947+
948+
926949
init(Q) when ?is_amqqueue(Q) ->
927-
Leader = amqqueue:get_pid(Q),
950+
Leader = get_writer_pid(Q),
928951
QName = amqqueue:get_name(Q),
929952
#{name := StreamId} = amqqueue:get_type_state(Q),
930953
%% tell us about leader changes so we can fail over

0 commit comments

Comments
 (0)