Skip to content

Commit

Permalink
stackwalk: fix jl_thread_suspend_and_get_state race (JuliaLang#56047) (
Browse files Browse the repository at this point in the history
…#192)

There was a missing re-assignment of old = -1; at the end of that loop
which means in the ABA case, we accidentally actually acquire the lock
on the thread despite not actually having stopped the thread; or in the
counter-case, we try to run through this logic with old==-1 on the next
iteration, and that isn't valid either (jl_thread_suspend_and_get_state
should return failure and the loop will abort too early).

Fix JuliaLang#56046

Co-authored-by: Jameson Nash <[email protected]>
  • Loading branch information
kpamnany and vtjnash authored Oct 17, 2024
1 parent 75cf8dc commit 01980b3
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/stackwalk.c
Original file line number Diff line number Diff line change
Expand Up @@ -881,8 +881,8 @@ static void jl_rec_backtrace(jl_task_t *t) JL_NOTSAFEPOINT
}
bt_context_t *context = NULL;
bt_context_t c;
int16_t old = -1;
while (!jl_atomic_cmpswap(&t->tid, &old, ptls->tid) && old != ptls->tid) {
int16_t old;
for (old = -1; !jl_atomic_cmpswap(&t->tid, &old, ptls->tid) && old != ptls->tid; old = -1) {
int lockret = jl_lock_stackwalk();
// if this task is already running somewhere, we need to stop the thread it is running on and query its state
if (!jl_thread_suspend_and_get_state(old, 0, &c)) {
Expand Down

0 comments on commit 01980b3

Please sign in to comment.