-
Notifications
You must be signed in to change notification settings - Fork 2.1k
sys/xtimer/xtimer.c: _mutex_timeout() bug fix #11992
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sys/xtimer/xtimer.c: _mutex_timeout() bug fix #11992
Conversation
de321cb
to
e6e0346
Compare
rebase because of #12008 got merged |
e6e0346
to
0a3827d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Finally found the time to test this one. I could reproduce the bug and verify that the fix is working. Nice one.
After struggling a bit to understand how the mutex/queue handling works here and what is implied by that, I wonder If we should just add some comments explaining what happens there to make it easier for the next one that looks at this code.
@MichelRottleuthner I will create a function for removing a thread from a mutex list ( this is what the |
Sure go ahead. |
Prevent a possible race condition when _mutex_timeout fires just after the mutex was locked but before the xtimer was removed The flow int xtimer_mutex_lock_timeout(mutex_t *mutex, uint64_t timeout) { ... mutex_lock(mutex); /* mutex locked */ /* _mutex_timeout fires and tries to remove thread from mutex queue */ /* DEBUG: simulate callback call between lock and remove */ xtimer_spin(xtimer_ticks_from_usec(timeout*2)); xtimer_remove(&t); ... }
no longer into the "if" when the mutex is free
0a3827d
to
7cd3e8b
Compare
@MichelRottleuthner I rebased. And removed REMOVE_ME commits |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR was tested and changes are valid -> ACK
HAS "REMOVE ME" COMMIT TO SHOW BUG
This includes pr: #10872
Tracking: pr #11660
Contribution description
Fixes BUG.
The function
_mutex_timeout
removes a thread from the waiting list of a mutex and unblocks the thread (sets status toSTATUS_PENDING
) when the thread is waiting for the mutex. This is used forxtimer_mutex_lock_timeout
a mutex lock function with timeout. This pr fixes a bug that happens because the function_mutex_timeout
never checks if the mutex has a waiting list. This pr adds this check.When the Timer triggers after the mutex is acquired but before the timer is removed.. You can see the bug with the "REMOVE ME" commit (asserts when the mutex is acquired and the timer triggers after).
The Function (
_mutex_timeout
) does nothing when the thread got the mutex (and no thread is waiting for itmutex->queue.next == MUTEX_LOCKED
) and when the mutex is free (mutex->queue.next == NULL
).list_remove
should not be called with a mutex wheremutex->queue.next == MUTEX_LOCKED
.Testing procedure
BOARD=native make -C tests/xtimer_mutex_lock_timeout/ flash test
output:
Revert the
Revert "REMOVE ME"
commit to see it work after fix.output:
Revert to
REMOVE ME
commit and run the test to see the bug.output:
Issues/PRs references
Depends on PR #11807
Tracking PR #11660
Includes PR #10872
my old pr fixing same bug: #11543