Skip to content

[Unix] Forward signals to pre-installed handlers + add --handle-signals=minimal#61294

Open
topolarity wants to merge 3 commits intoJuliaLang:masterfrom
topolarity:ct/forward-signals
Open

[Unix] Forward signals to pre-installed handlers + add --handle-signals=minimal#61294
topolarity wants to merge 3 commits intoJuliaLang:masterfrom
topolarity:ct/forward-signals

Conversation

@topolarity
Copy link
Member

@topolarity topolarity commented Mar 12, 2026

This adds a --handle-signals=min option to restrict Julia to installing only those signals that are:

  • essential to Julia's operation
  • can be (somewhat) reliably forwarded to outer / pre-existing handlers

The most important of these is our SIGSEGV / SIGBUS handler (used by the GC as a safepoint trigger), but this also changes divide-by-zero, SIGILL, and similar faults to prefer forwarding these to a previously installed handler, if one was found, rather than immediately throwing an exception in Julia.

This is intended to be used in a "Julia as a library" scenario.

From a user's perspective the limitations of "minimal" signal handling are:

  • Julia will not receive Ctrl-C signals
  • in-process profiling (Profile.@profile) is not supported
  • Julia will not respond (gracefully or otherwise) to SIGTERM / SIGQUIT
  • Julia may not throw DivideError (and similar) exceptions despite triggering a fault

In the future, it may be a good idea to keep a stricter notion of "execution state" on each thread, so that Julia can distinguish, e.g., a DivideError fault in its own code vs. faults in other libraries (or parallel copies of the Julia runtime).

Python handles this "thread attach / detach" via PyGILState_Ensure / PyGILState_Release and Java does this via AttachCurrentThread / DetachCurrentThread and of course Windows has the SEH stack but the Julia API has no equivalent (jl_adopt_thread / jl_destroy_thread are called based on the thread lifetime, not the active execution state).

P.S. This is Linux / BSD-only as is. macOS will need additional work, since forwarding mach exceptions is somewhat more complicated.

Co-developed with Claude.

@topolarity topolarity requested a review from gbaraldi March 12, 2026 15:49
In theory, this significantly improves our Julia-as-a-library behavior by
allowing Julia to forward signals to other copies of the Julia runtime, or
an outer application that may have loaded us.

Covers all fault handlers:
  SIGSEGV, SIGBUS, SIGFPE, SIGILL, SIGABRT, SIGSYS, SIGTRAP
as well as SIGUSR2

Also adds forwarding for SIGINT, SIGTERM, SIGQUIT, SIGINFO, and SIGUSR1
although these `sigwait()`'d signals will typically be blocked anyway so
that forwarding will not happen.
@topolarity topolarity added the needs tests Unit tests are required for this change label Mar 12, 2026
@topolarity topolarity removed the needs tests Unit tests are required for this change label Mar 12, 2026
This adds a `--handle-signals=minimal` option to restrict Julia to
installing only those signals that are essential to its operation and
which we can reliably forward to outer / pre-existing handlers.

The signals excluded are generally the "I/O"-like signals, incl. SIGINT,
SIGUSR1/2, SIGINFO, and SIGTERM / SIGQUIT and received by `sigwait()`
which cannot forward signals to a competing `sigwait()` loop anyway.

For an end user, this means that Julia will not respond to `Ctrl-C`,
SIGTERM / SIGQUIT (requests to terminate), or support in-process
profiling. This is appropriate behavior for a library, which should
typically defer these signals to the overall application anyway.
This test checks the "sweet spot" we're interested in:
  - Installed handlers can reliably forward signals
  - Installed handlers are sufficient for GC + multi-threading

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
@topolarity topolarity changed the title [Unix] Forward signals to pre-installed handlers (+ add --handle-signals=min) [Unix] Forward signals to pre-installed handlers (+ add --handle-signals=minimal) Mar 13, 2026
@topolarity topolarity changed the title [Unix] Forward signals to pre-installed handlers (+ add --handle-signals=minimal) [Unix] Forward signals to pre-installed handlers + add --handle-signals=minimal Mar 13, 2026
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