Repository navigation
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1835 +/- ##
==========================================
- Coverage 99.58% 99.58% -0.01%
==========================================
Files 35 35
Lines 18712 18674 -38
==========================================
- Hits 18634 18596 -38
Misses 78 78 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Keno
left a comment
There was a problem hiding this comment.
This was a deliberate design choice. The PR text hand-waives at an alternative, but does not work it out. The corner cases are a bit nasty.
vtjnash
left a comment
There was a problem hiding this comment.
Being deliberate in calling a @cfunction from a signal handler doesn't make the implementation seem any less unsound. I don't know the specifics either, just that we can't introduce new regressions when we introduce new features.
|
Windows runs a new thread (in our terms, a task) to handle signals to avoid the unix design issue where hijacking the state of an existing thread makes it almost unusable for mere mortals. Would that work for you -- sending cancellation runs a new thread/task so that it inherits neither the caller nor callee's unknown state when handling a signal? |
|
That might work, we should survey other libraries and see. Are we comfortable that we can always spin up a new thread even when threading might otherwise not be enabled? |
Deliberate or not, do you agree w.r.t. the problems? Naively, seems extremely buggy to me for us to try to do |
…rappers `gemm!`, `symm!` and `hemm!` were annotated `@ccall cancel_handler=...` with a Julia `@cfunction` as the handler, so that cancelling the calling task would run `_cancel_handler` on the thread blocked in the BLAS call, which set the cancel bit of that thread's OpenBLAS token slot. This implementation was unsound in several ways, and relanding can take a much simpler approach still using the changes to libopenblas, so those stay in the binaries. The handler ran like a signal handler: on Unix inside the SIGUSR2 handler, on macOS and Windows on the frozen thread's stack after a context rewrite. The option's contract was that the handler is async-signal-safe. `_cancel_handler` itself is integer-only code, but the wrapper Julia emits for any `@cfunction` compares the global world counter with the world cached in its `cfuncdata` on every call and, on a mismatch, re-resolves the target through `jl_get_abi_converter`: method lookup, possibly inference and compilation, locks, malloc. The world counter moves on every method definition in the process, so after any package load or REPL definition the first delivery took that path inside a signal handler, on a thread interrupted at an arbitrary instruction of OpenBLAS, which allocates its own buffers. The wrapper also pushes a GC frame and rewrites the task's world age from the handler. No Julia function can meet the contract, however small its body. The signal was never needed for what the handler did. `openblas_cancel` takes the token slot explicitly and performs an atomic store, and the slot is the calling thread's, so the cancelling thread could have set the bit itself. Running on the victim thread only bought passing `C_NULL` as the state and looking the slot up from the current thread inside the handler, at the cost of all of the above. Remove the handler, the extension detection in `_cancel_detect` and its reset in `lbt_forward`, and the per-call `_cancel_prepare` and `@cancel_check` pair around the three calls, which were unconditional and charged every small `gemm!` for a feature gated on large ones. The calls go back to plain `@ccall`s. Cancellation of a long BLAS call now waits for the call to return, as it did before the annotation; a cooperative check at a call site belongs to callers that know their work is long. A reland should drive the cancellation from the cancelling thread, without signals or handlers. The protocol the OpenBLAS patch already provides needs one adjustment: the generation of the slot should be advanced by the caller before the call, not by the driver at entry, so the wrapper can learn it. The wrapper then obtains its thread's slot with `openblas_cancel_token()` and the fresh generation on the calling thread, publishes the pair in a task field for the duration of the call, and clears it afterwards. `cancel!` reads the pair from the task on its own thread and calls `openblas_cancel(slot, gen)`, whose compare-and-set on the generation makes a late sender a no-op: a slot that has moved on to another operation, possibly another task's, is never touched. The post-call check then unwinds a genuinely cancelled call before its output escapes, exactly as the wrappers did here, and a cancellation that arrives before the call starts is observed by the pre-call check. That design has no victim-thread execution, no contract on the handler, and no cross-task hazard, and it costs the same handful of stores this annotation did. Assisted-by: Claude Code (Fable 5.1)
83763d7 to
56f7246
Compare
gemm!,symm!andhemm!were annotated@ccall cancel_handler=...with a Julia@cfunctionas the handler, so that cancelling the calling task would run_cancel_handleron the thread blocked in the BLAS call, which set the cancel bit of that thread's OpenBLAS token slot. This implementation was unsound in several ways, and relanding can take a much simpler approach still using the changes to libopenblas, so those stay in the binaries.The handler ran like a signal handler: on Unix inside the SIGUSR2 handler, on macOS and Windows on the frozen thread's stack after a context rewrite. The option's contract was that the handler is async-signal-safe.
_cancel_handleritself is integer-only code, but the wrapper Julia emits for any@cfunctioncompares the global world counter with the world cached in itscfuncdataon every call and, on a mismatch, re-resolves the target throughjl_get_abi_converter: method lookup, possibly inference and compilation, locks, malloc. The world counter moves on every method definition in the process, so after any package load or REPL definition the first delivery took that path inside a signal handler, on a thread interrupted at an arbitrary instruction of OpenBLAS, which allocates its own buffers. The wrapper also pushes a GC frame and rewrites the task's world age from the handler. No Julia function can meet the contract, however small its body.The signal was never needed for what the handler did.
openblas_canceltakes the token slot explicitly and performs an atomic store, and the slot is the calling thread's, so the cancelling thread could have set the bit itself. Running on the victim thread only bought passingC_NULLas the state and looking the slot up from the current thread inside the handler, at the cost of all of the above.Remove the handler, the extension detection in
_cancel_detectand its reset inlbt_forward, and the per-call_cancel_prepareand@cancel_checkpair around the three calls, which were unconditional and charged every smallgemm!for a feature gated on large ones. The calls go back to plain@ccalls. Cancellation of a long BLAS call now waits for the call to return, as it did before the annotation; a cooperative check at a call site belongs to callers that know their work is long.A reland should drive the cancellation from the cancelling thread, without signals or handlers. The protocol the OpenBLAS patch already provides needs one adjustment: the generation of the slot should be advanced by the caller before the call, not by the driver at entry, so the wrapper can learn it. The wrapper then obtains its thread's slot with
openblas_cancel_token()and the fresh generation on the calling thread, publishes the pair in a task field for the duration of the call, and clears it afterwards.cancel!reads the pair from the task on its own thread and callsopenblas_cancel(slot, gen), whose compare-and-set on the generation makes a late sender a no-op: a slot that has moved on to another operation, possibly another task's, is never touched. The post-call check then unwinds a genuinely cancelled call before its output escapes, exactly as the wrappers did here, and a cancellation that arrives before the call starts is observed by the pre-call check. That design has no victim-thread execution, no contract on the handler, and no cross-task hazard, and it costs the same handful of stores this annotation did.Assisted-by: Claude Code (Fable 5.1)