- count = lttng_ht_get_count(ltt_sessions_ht_by_id);
- LTTNG_ASSERT(count == lttng_ht_get_count(ltt_sessions_ht_by_name));
+ if (lttng_ht_get_count(ltt_sessions_ht_by_id) != 0) {
+ /* Not empty.*/
+ goto end;
+ }
+
+ /*
+ * At this point it is expected that the `ltt_sessions_ht_by_name` ht is
+ * empty.
+ *
+ * The removal from both hash tables is done in two different
+ * places:
+ * - removal from `ltt_sessions_ht_by_name` is done during
+ * `session_destroy()`
+ * - removal from `ltt_sessions_ht_by_id` is done later
+ * in `session_release()` on the last reference put.
+ *
+ * This means that it is possible for `ltt_sessions_ht_by_name` to be
+ * empty but for `ltt_sessions_ht_by_id` to still contain objects when
+ * multiple sessions exists. The reverse is false, hence this sanity
+ * check.
+ */
+ LTTNG_ASSERT(lttng_ht_get_count(ltt_sessions_ht_by_name) == 0);
+ empty = true;