Skip to content

Commit 81cbc0a

Browse files
committed
Close MQTT connection if sending fails
Why: #9196 reports that failures in sending can cause huge log amounts and eventually even memory alarms. How: Similar to rabbit_writer and rabbit_amqp1_0_writer, when sending on a network connection fails, the connection process will be terminated immediately.
1 parent e2416e5 commit 81cbc0a

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

deps/rabbitmq_mqtt/src/rabbit_mqtt_reader.erl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -336,13 +336,14 @@ process_received_bytes(Bytes, State = #state{socket = Socket,
336336
connect_packet_unprocessed ->
337337
Send = fun(Data) ->
338338
try rabbit_net:port_command(Socket, Data)
339-
catch error:Error ->
339+
catch error:Reason ->
340340
?LOG_ERROR("writing to MQTT socket ~p failed: ~p",
341-
[Socket, Error])
341+
[Socket, Reason]),
342+
exit({send_failed, Reason})
342343
end,
343344
ok
344345
end,
345-
case rabbit_mqtt_processor:init(Packet, Socket, ConnName, Send) of
346+
try rabbit_mqtt_processor:init(Packet, Socket, ConnName, Send) of
346347
{ok, ProcState1} ->
347348
?LOG_INFO("Accepted MQTT connection ~ts for client ID ~ts",
348349
[ConnName, rabbit_mqtt_processor:info(client_id, ProcState1)]),
@@ -358,9 +359,11 @@ process_received_bytes(Bytes, State = #state{socket = Socket,
358359
?LOG_ERROR("Rejected MQTT connection ~ts with Connect Reason Code ~p",
359360
[ConnName, ConnectReasonCode]),
360361
{stop, shutdown, {_SendWill = false, State}}
362+
catch exit:{send_failed, Reason} ->
363+
network_error(Reason, State)
361364
end;
362365
_ ->
363-
case rabbit_mqtt_processor:process_packet(Packet, ProcState) of
366+
try rabbit_mqtt_processor:process_packet(Packet, ProcState) of
364367
{ok, ProcState1} ->
365368
process_received_bytes(
366369
Rest,
@@ -377,6 +380,8 @@ process_received_bytes(Bytes, State = #state{socket = Socket,
377380
{stop, {shutdown, Reason}, pstate(State, ProcState1)};
378381
{stop, {disconnect, {client_initiated, SendWill}}, ProcState1} ->
379382
{stop, normal, {SendWill, pstate(State, ProcState1)}}
383+
catch exit:{send_failed, Reason} ->
384+
network_error(Reason, State)
380385
end
381386
end;
382387
{error, {disconnect_reason_code, ReasonCode} = Reason} ->

0 commit comments

Comments
 (0)