Skip to content

Commit 224ff57

Browse files
authored
stackwalk: fix jl_thread_suspend_and_get_state race (#56047)
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 #56046
1 parent 6fa4af5 commit 224ff57

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/stackwalk.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,8 +1196,8 @@ JL_DLLEXPORT size_t jl_record_backtrace(jl_task_t *t, jl_bt_element_t *bt_data,
11961196
}
11971197
bt_context_t *context = NULL;
11981198
bt_context_t c;
1199-
int16_t old = -1;
1200-
while (!jl_atomic_cmpswap(&t->tid, &old, ptls->tid) && old != ptls->tid) {
1199+
int16_t old;
1200+
for (old = -1; !jl_atomic_cmpswap(&t->tid, &old, ptls->tid) && old != ptls->tid; old = -1) {
12011201
int lockret = jl_lock_stackwalk();
12021202
// if this task is already running somewhere, we need to stop the thread it is running on and query its state
12031203
if (!jl_thread_suspend_and_get_state(old, 1, &c)) {

0 commit comments

Comments
 (0)