From a661f14d2c7b010334989018e3ebe12dde3ee49f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barnab=C3=A1s=20P=C5=91cze?= Date: Mon, 9 Mar 2026 21:59:43 +0100 Subject: [PATCH] spa: support: loop: check `enter_count` before iterating Calling "iterate()" on a loop that has not been entered by the calling thread is invalid. So try to diagnose misbehaving applications on a "best effort" basis by checking `impl::enter_count`. This is not a foolproof check, and can also technically cause data races while reading the variable. See #5148 --- spa/plugins/support/loop.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/spa/plugins/support/loop.c b/spa/plugins/support/loop.c index 57b87010c..88364b7b3 100644 --- a/spa/plugins/support/loop.c +++ b/spa/plugins/support/loop.c @@ -751,6 +751,8 @@ static int loop_iterate_cancel(void *object, int timeout) int i, nfds; uint32_t remove_count; + spa_return_val_if_fail(impl->enter_count > 0, -EPERM); + remove_count = impl->remove_count; spa_loop_control_hook_before(&impl->hooks_list); spa_assert_se(pthread_mutex_unlock(&impl->lock) == 0); @@ -799,6 +801,8 @@ static int loop_iterate(void *object, int timeout) int i, nfds; uint32_t remove_count; + spa_return_val_if_fail(impl->enter_count > 0, -EPERM); + remove_count = impl->remove_count; spa_loop_control_hook_before(&impl->hooks_list); spa_assert_se(pthread_mutex_unlock(&impl->lock) == 0);