From 0c76b6cf4d5338e21414a833dd1b4f0386a95944 Mon Sep 17 00:00:00 2001 From: Thales Macedo Garitezi Date: Fri, 4 Mar 2022 11:00:49 -0300 Subject: [PATCH] fix(congestion): fix congestion message formatting It seems that the `~ts` in the format string does not handle maps, which results in a crash when trying to format the congestion message. ```erlang 28> io_lib:format("~ts", [#{}]). ** exception error: bad argument in function io_lib:format/2 called as io_lib:format("~ts",[#{}]) ``` Example: ``` 2022-03-04T12:43:26.900609+00:00 [warning] '$kind': unclean_terminate, clientid: 192.168.2.116_bench_sub_11412_1782328074, context: badarg, exception: error, line: 582, mfa: emqx_connection:terminate/2, peername: 192.168.2.116:47358, stacktrace: [{io_lib,format,["connection congested: ~ts",[#{buffer => 4096,clientid => <<"192.168.2.116_bench_sub_11412_1782328074">>,conn_state => disconnected,connected_at => 1646397524470,high_msgq_watermark => 8192,high_watermark => 1048576,memory => 57064,message_queue_len => 20,peername => <<"192.168.2.116:47358">>,pid => <<"<0.259.28>">>,proto_name => <<"MQTT">>,proto_ver => 5,recbuf => 4096,recv_cnt => 2,recv_oct => 81,reductions => 267523,send_cnt => 333,send_oct => 149869,send_pend => 128,sndbuf => 332800,sockname => <<"10.10.1.13:1883">>,socktype => tcp,username => undefined}]],[{file,"io_lib.erl"},{line,187}]},{emqx_congestion,do_cancel_alarm_congestion,4,[{file,"/emqx/apps/emqx/src/emqx_congestion.erl"},{line,88}]},{lists,foreach,2,[{file,"lists.erl"},{line,1342}]},{emqx_connection,terminate,2,[{file,"/emqx/apps/emqx/src/emqx_connection.erl"},{line,576}]},{proc_lib,wake_up,3,[{file,"proc_lib.erl"},{line,236}]}] ``` --- apps/emqx/src/emqx_congestion.erl | 4 ++-- apps/emqx/test/emqx_connection_SUITE.erl | 26 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/apps/emqx/src/emqx_congestion.erl b/apps/emqx/src/emqx_congestion.erl index fa2788bc1..4f4423b12 100644 --- a/apps/emqx/src/emqx_congestion.erl +++ b/apps/emqx/src/emqx_congestion.erl @@ -78,14 +78,14 @@ cancel_alarm_congestion(Socket, Transport, Channel, Reason) -> do_alarm_congestion(Socket, Transport, Channel, Reason) -> ok = update_alarm_sent_at(Reason), AlarmDetails = tcp_congestion_alarm_details(Socket, Transport, Channel), - Message = io_lib:format("connection congested: ~ts", [AlarmDetails]), + Message = io_lib:format("connection congested: ~p", [AlarmDetails]), emqx_alarm:activate(?ALARM_CONN_CONGEST(Channel, Reason), AlarmDetails, Message), ok. do_cancel_alarm_congestion(Socket, Transport, Channel, Reason) -> ok = remove_alarm_sent_at(Reason), AlarmDetails = tcp_congestion_alarm_details(Socket, Transport, Channel), - Message = io_lib:format("connection congested: ~ts", [AlarmDetails]), + Message = io_lib:format("connection congested: ~p", [AlarmDetails]), emqx_alarm:deactivate(?ALARM_CONN_CONGEST(Channel, Reason), AlarmDetails, Message), ok. diff --git a/apps/emqx/test/emqx_connection_SUITE.erl b/apps/emqx/test/emqx_connection_SUITE.erl index 5d2c31441..57ae9a817 100644 --- a/apps/emqx/test/emqx_connection_SUITE.erl +++ b/apps/emqx/test/emqx_connection_SUITE.erl @@ -432,6 +432,32 @@ t_oom_shutdown(_) -> end, Opts), ok. +t_cancel_congestion_alarm(_) -> + Opts = #{trap_exit => false}, + ok = meck:expect(emqx_transport, getstat, + fun(_Sock, [send_pend]) -> + %% simulate congestion + {ok, [{send_pend, 999}]}; + (_Sock, Options) -> + {ok, [{K, 0} || K <- Options]} + end), + with_conn( + fun(Pid) -> + #{ channel := Channel + , transport := Transport + , socket := Socket + } = emqx_connection:get_state(Pid), + %% precondition + Zone = emqx_channel:info(zone, Channel), + true = emqx_config:get_zone_conf(Zone, [conn_congestion, enable_alarm]), + %% should not raise errors + ok = emqx_congestion:maybe_alarm_conn_congestion(Socket, Transport, Channel), + %% should not raise errors either + ok = emqx_congestion:cancel_alarms(Socket, Transport, Channel), + ok + end, Opts), + ok. + %%-------------------------------------------------------------------- %% Helper functions %%--------------------------------------------------------------------