Skip to content

Fix false llvm.assumes in wake_thread! (segfault on Julia nightly) - #67

Merged
ranocha merged 2 commits into
JuliaSIMD:mainfrom
vchuravy:vc/fix-wake-thread-assume
Oct 14, 2026
Merged

ranocha merged 2 commits into
JuliaSIMD:mainfrom
vchuravy:vc/fix-wake-thread-assume

Conversation

@vchuravy

@vchuravy vchuravy commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #66.

#64 changed the bound in wake_thread! to assume(unsigned(length(Base.Workqueues())) > unsigned(tid)). On Julia 1.13+, Base.Workqueues() is the current thread's work queue, and its length is the number of tasks queued on that thread, not the number of threads. So the assumption is false, which is undefined behaviour.

  • Nightly (LLVM 22): LLVM acts on the assumption. It deletes the empty-list check in the inlined length(::IntrusiveLinkedList) loop, which then segfaults (Fault at memory address: 0x1d3) on the first Polyester.@batch launch with 2 or more threads.
  • Julia 1.13 (LLVM 20): has the same undefined behaviour but happens not to crash.

This PR:

  • Assumes Threads.maxthreadid() > tid instead. The function reads Base.Workqueues[tid + 1], which needs exactly that bound.
  • Fixes the off-by-one in assume(length(TASKS) > tid + 1). That assumption is false for the last worker (always at -t 2). It is now length(TASKS) >= tid, which matches the TASKS[tid] access.
  • Adds comments explaining both bounds.
  • Runs the existing Julia nightly workflow (ci-julia-nightly.yml) weekly on a schedule, and allows starting it by hand.

Testing

JULIA_NUM_THREADS=4, Pkg.test():

Julia before after
1.10.12 pass pass
1.12.7 pass pass
1.13.1 pass pass
1.14.0-DEV.3384 segfault in wake_thread! pass

With the fix, the reproducers in #66 and Trixi.jl (Polyester backend, -t 2 and -t 8) also run on nightly.

The nightly workflow (ci-julia-nightly.yml) runs only on pull requests and pushes to main. Its last run was on 2026-05-29, before the nightly LLVM update that exposes this bug, so it never saw the crash. The second commit adds a weekly schedule (and workflow_dispatch) to that workflow so that changes in Julia nightly are caught without a change in this repository. A patch release after merging would unblock Polyester users on nightly.

This PR was prepared with assistance from Claude Code (Anthropic's AI coding assistant). I reviewed the changes.

🤖 Generated with Claude Code

Base.Workqueues() is the current thread's work queue, so its length is
the number of queued tasks, not the number of threads. The assumption
length(Base.Workqueues()) > tid is false in general; LLVM 22 (Julia
1.14) uses it to drop the nothing check in the IntrusiveLinkedList
length loop, which then dereferences nothing and segfaults in the first
@Batch launch. Use Threads.maxthreadid() instead, and fix the TASKS
bound (tid, not tid + 1, indexes TASKS).

Assisted-by: Claude Code (Opus 5.5)
Assisted-by: Claude Code (Opus 5.5)
@vchuravy
vchuravy force-pushed the vc/fix-wake-thread-assume branch from 15fd22d to 4f1a898 Compare October 13, 2026 18:46
@codecov

codecov Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.44%. Comparing base (cb9cf25) to head (4f1a898).
⚠️ Report is 8 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #67      +/-   ##
==========================================
- Coverage   99.20%   94.44%   -4.77%     
==========================================
  Files           4        4              
  Lines         126      126              
==========================================
- Hits          125      119       -6     
- Misses          1        7       +6     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

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

@ranocha ranocha left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot!

@ranocha
ranocha merged commit d41c069 into JuliaSIMD:main Oct 14, 2026
17 of 20 checks passed
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.

Segfault in wake_thread! on Julia nightly (LLVM 22): assume(length(Base.Workqueues()) > tid) is false

2 participants