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}]}]
```
This commit is contained in:
Thales Macedo Garitezi 2022-03-04 11:00:49 -03:00
parent e298ff9dca
commit 0c76b6cf4d
No known key found for this signature in database
GPG Key ID: DD279F8152A9B6DD
2 changed files with 28 additions and 2 deletions

View File

@ -78,14 +78,14 @@ cancel_alarm_congestion(Socket, Transport, Channel, Reason) ->
do_alarm_congestion(Socket, Transport, Channel, Reason) -> do_alarm_congestion(Socket, Transport, Channel, Reason) ->
ok = update_alarm_sent_at(Reason), ok = update_alarm_sent_at(Reason),
AlarmDetails = tcp_congestion_alarm_details(Socket, Transport, Channel), 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), emqx_alarm:activate(?ALARM_CONN_CONGEST(Channel, Reason), AlarmDetails, Message),
ok. ok.
do_cancel_alarm_congestion(Socket, Transport, Channel, Reason) -> do_cancel_alarm_congestion(Socket, Transport, Channel, Reason) ->
ok = remove_alarm_sent_at(Reason), ok = remove_alarm_sent_at(Reason),
AlarmDetails = tcp_congestion_alarm_details(Socket, Transport, Channel), 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), emqx_alarm:deactivate(?ALARM_CONN_CONGEST(Channel, Reason), AlarmDetails, Message),
ok. ok.

View File

@ -432,6 +432,32 @@ t_oom_shutdown(_) ->
end, Opts), end, Opts),
ok. 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 %% Helper functions
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------