Skip to content

Don't switch tasks when the context registry is contended - #735

Merged
maleadt merged 1 commit into
mainfrom
tb/nonyielding-locks
Dec 2, 2026
Merged

maleadt merged 1 commit into
mainfrom
tb/nonyielding-locks

Conversation

@maleadt

@maleadt maleadt commented Dec 2, 2026

Copy link
Copy Markdown
Member

GPUCompiler's test that compiles from 8 threads at once segfaulted on Julia 1.10 on aarch64 (https://github.com/JuliaGPU/GPUCompiler.jl/actions/runs/46842403015/job/143326648823):

signal 11 (1): Segmentation fault
jl_mutex_unlock_nogc at src/julia_locks.h:85 [inlined]
ijl_eh_restore_state at src/rtutils.c:275
#740 at LLVM/src/state.jl:137
...
JuliaContext at GPUCompiler/src/driver.jl:25

On Julia 1.10 and 1.11, GPUCompiler holds Julia's codegen lock while it generates and optimizes code. Julia records the runtime locks a thread holds in a per-thread list, which exception handlers save and restore, so a task must not switch while it holds one. LLVM.jl's registry of diagnostic states is locked with a ReentrantLock when a context is created or disposed of, and when running passes or emitting code looks up a context's diagnostic state. If another thread is creating or disposing of a context at that moment, the lock is contended and the compiling task switches. If it resumes on another thread, releasing the codegen lock removes it from that thread's list instead, which underflows the list's length when it is empty, after which restoring an exception handler on that thread can unlock garbage. Generated functions have the same constraint: on 1.10 and 1.11, a contended lock there throws "task switch not allowed from inside staged nor pure functions" when it attempts to switch to another task.

This uses Threads.SpinLock for that registry, and for the lock that records exceptions thrown by callbacks, which also run while compiling. Both critical sections are a few dictionary or field operations that don't switch tasks.

libraries_lock has the same problem in principle, as GPUCompiler looks up Julia's library while holding the codegen lock when generating output, but registering a library holds it across loading and initializing the library, so a spin lock doesn't fit there. That is left as is.

With Julia 1.10 on x86_64, compiling from 6 threads in a loop while 2 tasks create and dispose of contexts crashed 5 out of 24 runs, with a few hundred task switches under the codegen lock per run. With this change, 12 out of 12 runs passed with zero task switches under the codegen lock. The new test runs a generator that creates a context while another thread holds the registry; it fails with a ReentrantLock on 1.10 and 1.11, and passes on 1.10 through 1.12.

The registry of diagnostic states is locked when a context is created or
disposed of, and when running passes or emitting code looks up a context's
diagnostic state. It used a ReentrantLock, which switches tasks when it is
contended. That is not allowed in generated functions, and on Julia 1.10
and 1.11 GPUCompiler does this while holding Julia's codegen lock: a task
that switches holding a runtime lock and resumes on another thread
corrupts the runtime's per-thread lists of held locks, which crashed
concurrent compilations in jl_eh_restore_state.

The critical sections are a few dictionary operations that don't switch
tasks, so use a spin lock. Do the same for the lock that records
exceptions thrown by callbacks, which run while compiling.
@codecov

codecov Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

@@            Coverage Diff             @@
##             main     #735      +/-   ##
==========================================
+ Coverage   94.39%   94.47%   +0.08%     
==========================================
  Files          59       59              
  Lines       10020    10011       -9     
==========================================
  Hits         9458     9458              
+ Misses        562      553       -9     
Files with missing lines Coverage Δ
src/base.jl 97.31% <ø> (ø)
src/core/context.jl 89.55% <ø> (ø)

... and 3 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@maleadt
maleadt merged commit 99b524f into main Dec 2, 2026
54 checks passed
@maleadt
maleadt deleted the tb/nonyielding-locks branch December 2, 2026 19:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant