Skip to content

AsyncEventThread: fix race condition in stop#2994

Open
floitsch wants to merge 1 commit intomasterfrom
floitsch/fix-race-condition
Open

AsyncEventThread: fix race condition in stop#2994
floitsch wants to merge 1 commit intomasterfrom
floitsch/fix-race-condition

Conversation

@floitsch
Copy link
Member

The AsyncEventThread releases the lock while executing user callbacks. If stop() is called during this time, it acquires the lock, sets state_ = STOPPED, signals the condition variable, and waits in join().

However, when the callback returned, the worker thread would re-acquire the lock and unconditionally set state_ = IDLE. This overwrote the STOPPED state, causing the thread to continue its loop instead of exiting. As a result, stop() would hang indefinitely in join().
This hang led to resource exhaustion and eventual memory corruption (manifesting as "Double free or corruption" in SPI tests) when the system attempted to forcefully clean up or reuse the stuck resources.

This commit fixes the race by checking if the state is STOPPED after re-acquiring the lock, ensuring the thread exits correctly.

The AsyncEventThread releases the lock while executing user callbacks.
If stop() is called during this time, it acquires the lock, sets `state_ = STOPPED`, signals the condition variable, and waits in `join()`.

However, when the callback returned, the worker thread would re-acquire the lock and unconditionally set `state_ = IDLE`.
This overwrote the `STOPPED` state, causing the thread to continue its loop instead of exiting. As a result,
stop() would hang indefinitely in `join()`.
This hang led to resource exhaustion and eventual memory corruption (manifesting as "Double free or corruption" in SPI tests)
when the system attempted to forcefully clean up or reuse the stuck resources.

This commit fixes the race by checking if the state is `STOPPED` after re-acquiring the lock, ensuring the thread exits correctly.
@floitsch floitsch requested a review from kasperl February 15, 2026 14:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant