From 9744040795acd7eb06809c3a26a5c3c26d9317f0 Mon Sep 17 00:00:00 2001 From: DDDHuang <44492639+DDDHuang@users.noreply.github.com> Date: Fri, 13 May 2022 10:28:29 +0800 Subject: [PATCH] fix: clean session when destroy mqtt bridge --- .../src/mqtt/emqx_connector_mqtt_worker.erl | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/apps/emqx_connector/src/mqtt/emqx_connector_mqtt_worker.erl b/apps/emqx_connector/src/mqtt/emqx_connector_mqtt_worker.erl index a434dd762..1ff4e7f6a 100644 --- a/apps/emqx_connector/src/mqtt/emqx_connector_mqtt_worker.erl +++ b/apps/emqx_connector/src/mqtt/emqx_connector_mqtt_worker.erl @@ -276,6 +276,20 @@ code_change(_Vsn, State, Data, _Extra) -> terminate(_Reason, _StateName, #{replayq := Q} = State) -> _ = disconnect(State), _ = replayq:close(Q), + maybe_destroy_session(State). + +maybe_destroy_session(#{connect_opts := ConnectOpts = #{clean_start := false}} = State) -> + try + %% Destroy session if clean_start is not set. + %% Ignore any crashes, just refresh the clean_start = true. + _ = do_connect(State#{connect_opts => ConnectOpts#{clean_start => true}}), + _ = disconnect(State), + ok + catch + _:_ -> + ok + end; +maybe_destroy_session(_State) -> ok. %% ensure_started will be deprecated in the future