From 8e8ff08973c06951aea98a05c3523ff1db5d4792 Mon Sep 17 00:00:00 2001 From: "Zaiming (Stone) Shi" Date: Sun, 9 Oct 2022 11:31:54 +0200 Subject: [PATCH 1/2] fix(shared): check sticky if sticky pid is still a member Prior to this fix, in case of a subscriber unsubscribes without disconnect, the sticky dispatch strategy will continue to pick the old member. This commit fixes it by checking if the member is still in the group --- src/emqx_shared_sub.erl | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/emqx_shared_sub.erl b/src/emqx_shared_sub.erl index cc57e001f..2d85a834a 100644 --- a/src/emqx_shared_sub.erl +++ b/src/emqx_shared_sub.erl @@ -301,7 +301,8 @@ fetch_sender_ref({Sender, Ref}) -> {Sender, Ref}. pick(sticky, ClientId, SourceTopic, Group, Topic, FailedSubs) -> Sub0 = erlang:get({shared_sub_sticky, Group, Topic}), - case is_active_sub(Sub0, FailedSubs) of + All = subscribers(Group, Topic), + case is_active_sub(Sub0, FailedSubs, All) of true -> %% the old subscriber is still alive %% keep using it for sticky strategy @@ -471,8 +472,10 @@ update_stats(State) -> State. %% Return 'true' if the subscriber process is alive AND not in the failed list -is_active_sub(Pid, FailedSubs) -> - not maps:is_key(Pid, FailedSubs) andalso is_alive_sub(Pid). +is_active_sub(Pid, FailedSubs, All) -> + lists:member(Pid, All) andalso + (not maps:is_key(Pid, FailedSubs)) andalso + is_alive_sub(Pid). %% erlang:is_process_alive/1 does not work with remote pid. is_alive_sub(Pid) when ?IS_LOCAL_PID(Pid) -> From 761283f61639de11411d650e115b1d8a30383f54 Mon Sep 17 00:00:00 2001 From: "Zaiming (Stone) Shi" Date: Sun, 9 Oct 2022 11:49:11 +0200 Subject: [PATCH 2/2] docs: update change log --- CHANGES-4.3.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGES-4.3.md b/CHANGES-4.3.md index 26dd529a2..c15ac220b 100644 --- a/CHANGES-4.3.md +++ b/CHANGES-4.3.md @@ -58,6 +58,12 @@ File format: - For wildcard deliveries, the re-dispatch used the wrong topic (the publishing topic, but not the subscribing topic), caused messages to be lost when dispatching. +- Fix shared subscription group member unsubscribe issue when 'sticky' strategy is used. + Prior to this fix, if a previously picked member unsubscribes from the group (without reconnect) + the message is still dispatched to it. + This issue only occurs when unsubscribe with the session kept. + Fixed in [#9119](https://github.com/emqx/emqx/pull/9119) + ## v4.3.20 ### Bug fixes