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
This commit is contained in:
Barnabás Pőcze 2026-03-09 21:59:43 +01:00
parent dc1738ce57
commit a661f14d2c

View file

@ -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);