fix(live_conn): fix live connection count on race condition
When multiple clients try to connect concurrently using the same
client ID, they all call `emqx_channel:ensure_connected`, increasing
the live connection count, but only one will successfully acquire the
lock for that client ID. This means that all other clients that
increased the live connection count will not get to call neither
`emqx_channel:ensure_disconnected` nor be monitored for `DOWN`
messages, effectively causing a count leak.
By moving the increment to `emqx_cm:register_channel`, which is only
called inside the lock, we can remove this leakage.
Also, during the handling of `DOWN` messages, we now iterate over all
channel PIDs returned by `eqmx_misc:drain_down`, since it could be
that one or more PIDs are not contained in the `pmon` state.
When multiple clients try to connect concurrently using the same
client ID, they all call `emqx_channel:ensure_connected`, increasing
the live connection count, but only one will successfully acquire the
lock for that client ID. This means that all other clients that
increased the live connection count will not get to call neither
`emqx_channel:ensure_disconnected` nor be monitored for `DOWN`
messages, effectively causing a count leak.
By moving the increment to `emqx_cm:register_channel`, which is only
called inside the lock, we can remove this leakage.
Also, during the handling of `DOWN` messages, we now iterate over all
channel PIDs returned by `eqmx_misc:drain_down`, since it could be
that one or more PIDs are not contained in the `pmon` state.
Sessions must not enqueue messages when another process is taking over
the client id, since it already passed on the message queue in the
session state.
Without this fix, messages arriving after `{takeover, 'begin'} to a
channel with no connection (i.e., a persistent session) would be lost.
chore(ekka): bump ekka to 0.8.1.6
We're doing this to remove some missing change warnings when updating
emqx v4.3.10. Ekka's appup was updated in emqx/ekka#140 .
<details>
<summary> `update_appup.escript` output after update (running against 4.3.10) </summary>
```
Found the previous appup file: _build/emqx/rel/emqx/lib/ekka-0.8.1.6/ebin/ekka.appup
INFO: Application 'ekka' has been updated: "0.8.1.4" -> "0.8.1.6"
... elided ...
ERROR: Appup file for the external dependency 'ehttpc' is not complete.
Missing changes: #{down =>
[{"0.1.10",
[{load_module,ehttpc_pool,brutal_purge,soft_purge,[]},
{load_module,ehttpc,brutal_purge,soft_purge,[]}]},
{<<"0\\.1\\.0">>,
[{load_module,ehttpc_pool,brutal_purge,soft_purge,[]},
{load_module,ehttpc,brutal_purge,soft_purge,[]}]},
{<<"0\\.1\\.[1-7]">>,
[{load_module,ehttpc_pool,brutal_purge,soft_purge,[]},
{load_module,ehttpc,brutal_purge,soft_purge,[]}]}],
up =>
[{"0.1.10",
[{load_module,ehttpc_pool,brutal_purge,soft_purge,[]},
{load_module,ehttpc,brutal_purge,soft_purge,[]}]},
{<<"0\\.1\\.[0-7]">>,
[{load_module,ehttpc_pool,brutal_purge,soft_purge,[]},
{load_module,ehttpc,brutal_purge,soft_purge,[]}]}]}
NOTE: Some changes above might be already covered by regexes.
ERROR: Incomplete appups found. Please inspect the output for more details.
```
</details>