Skip to content

Replace the generation-slot cancellation API with a caller-owned flag - #6307

Open
vtjnash wants to merge 1 commit into
OpenMathLib:developfrom
vtjnash:cancellation-flag-pointer
Open

vtjnash wants to merge 1 commit into
OpenMathLib:developfrom
vtjnash:cancellation-flag-pointer

Conversation

@vtjnash

@vtjnash vtjnash commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

The cancellation API added in #5882 gives every thread a generation slot that the level-3 drivers advance at operation entry, and cancels by a compare-and-set against a slot value loaded by the canceller. That protocol cannot be used safely from any thread: the slot lives in the issuing thread's storage, so another thread canceller must stop that thread inside the call while it loads and updates the slot, or it may touch a dead thread's storage or cancel a later, unrelated operation. With that exclusion in place the generation check is redundant, and without it the only safe caller would be a handler running on the issuing thread itself, i.e. in async-signal context. However, calling openblas_cancel_slot calls malloc (either explicitly, or via __thread implementation details), so it isn't async-signal-safe. That leaves no places where this code was ever valid to use. Additionally, advancing the slot at entry as done in #5882 would have miss a cancellation requested before the driver starts.

This replaces it with an API function char *openblas_set_thread_cancel_flag$_suffix(char *), which designates a byte pointer, owned by the caller, as the calling thread's cancellation flag (NULL: none) and returns the previous designation, so that nested designations can restore it. The level-3 drivers read the issuing thread's flag pointer at entry, forward it to the worker threads, and poll it with a relaxed atomic load at the same points as before, returning early once it is nonzero. OpenBLAS never writes the flag, so there is nothing to clear, reuse or disambiguate: the caller sets it from any thread and keeps it valid while it is designated.

openblas_cancel and openblas_cancel_token are removed; neither has been in a release yet, and it seems that potentially weren't actually in the exported API currently either, due to mistakes in the gensymbol edits (which also get reverted here since they were not necessary or desirable).

The downstream PR driving this change is up at JuliaLang/LinearAlgebra.jl#2008

Assisted-by: Claude Code (Opus 5.5)

The cancellation API added in OpenMathLib#5882 gives every thread a generation slot
that the level-3 drivers advance at operation entry, and cancels by a
compare-and-set against a slot value loaded by the canceller. That
protocol cannot be used safely from another thread: the slot lives in
the issuing thread's storage, so a canceller must hold that thread
inside the call while it loads and updates the slot, or it may touch a
dead thread's storage or cancel a later, unrelated operation. With that
exclusion in place the generation check is redundant, and without it the
only safe caller is a handler running on the issuing thread itself, i.e.
in async-signal context. Advancing the slot at entry also erases a
cancellation requested before the driver starts.

Replace it with `char *openblas_set_thread_cancel_flag(char *)`, which
designates a byte owned by the caller as the calling thread's
cancellation flag (NULL: none) and returns the previous designation, so
that nested designations can restore it. The level-3 drivers read the
issuing thread's flag pointer at entry, forward it to the worker
threads, and poll it with a relaxed atomic load at the same points as
before, returning early once it is nonzero. OpenBLAS never writes the
flag, so there is nothing to clear, reuse or disambiguate: the caller
sets it from any thread and keeps it valid while it is designated.

openblas_cancel and openblas_cancel_token are removed; neither has been
in a release, and due to mistakes in the gensymbol edits, wasn't
previously actually in the exported API either.

Assisted-by: Claude Code (Opus 5.5)

This branch has not been deployed

No deployments
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