Skip to content

Commit 68de3fd

Browse files
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]>
1 parent 3ecbe7b commit 68de3fd

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
@@ -926,8 +926,31 @@ readers(QName) ->
926926
{node(), 0}
927927
end.
928928

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

0 commit comments

Comments
 (0)