Просмотр исходного кода

fix: check conn state before sending will message

Thales Macedo Garitezi 3 лет назад
Родитель
Сommit
87ab2e3a2d
1 измененных файлов с 12 добавлено и 3 удалено
  1. 12 3
      apps/emqx/src/emqx_channel.erl

+ 12 - 3
apps/emqx/src/emqx_channel.erl

@@ -1434,15 +1434,24 @@ terminate({shutdown, kicked}, Channel) ->
     run_terminate_hook(kicked, Channel);
 terminate({shutdown, Reason}, Channel) when
     Reason =:= discarded;
-    Reason =:= takenover;
-    Reason =:= not_authorized
+    Reason =:= takenover
 ->
     run_terminate_hook(Reason, Channel);
 terminate(Reason, Channel = #channel{will_msg = WillMsg}) ->
-    (WillMsg =/= undefined) andalso publish_will_msg(WillMsg),
+    should_publish_will_message(Reason, Channel) andalso publish_will_msg(WillMsg),
     (Reason =:= expired) andalso persist_if_session(Channel),
     run_terminate_hook(Reason, Channel).
 
+should_publish_will_message(TerminateReason, Channel) ->
+    not lists:member(TerminateReason, [
+        {shutdown, kicked},
+        {shutdown, discarded},
+        {shutdown, takenover},
+        {shutdown, not_authorized}
+    ]) andalso
+        not lists:member(info(conn_state, Channel), [idle, connecting]) andalso
+        info(will_msg, Channel) =/= undefined.
+
 persist_if_session(#channel{session = Session} = Channel) ->
     case emqx_session:is_session(Session) of
         true ->