Skip to content

Commit

Permalink
[GR-17457] Use the same reserved signals as CRuby and add specs for it
Browse files Browse the repository at this point in the history
PullRequest: truffleruby/4085
  • Loading branch information
eregon committed Dec 11, 2023
2 parents b7cb7fe + 23f49e2 commit 32dd3e1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 deletions.
14 changes: 7 additions & 7 deletions doc/user/compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,15 @@ Error message strings will sometimes differ from MRI, as these are not generally

### Signals

The set of signals that TruffleRuby can handle is different from MRI.
When using the native configuration, TruffleRuby allows trapping all the same signals that MRI does, as well as a few that MRI does not.
The only signals that can't be trapped are `KILL`, `STOP`, and `VTALRM`.
First, `KILL` and `STOP` can never be trapped, per POSIX (`man 2 signal`).
Some signals are reserved on CRuby, and they are also reserved on TruffleRuby, because trapping those would cause all sorts of problems: `SEGV`, `BUS`, `ILL`, `FPE` and `VTALRM`.

When using the native configuration, TruffleRuby allows trapping all the same signals that MRI does.
Consequently, any signal handling code that runs on MRI can run on TruffleRuby without modification in the native configuration.

However, when run on the JVM, TruffleRuby is unable to trap `USR1` or `QUIT`, as these signals are reserved by the JVM.
In such a case `trap(:USR1) {}` will raise an `ArgumentError`.
Any code that relies on being able to trap those signals will need to fall back to another available signal.
Additionally, `FPE`, `ILL`, `KILL`, `SEGV`, `STOP`, and `VTALRM` cannot be trapped, but these signals are also unavailable on MRI.
However, when run on the JVM, TruffleRuby is unable to trap `QUIT`, as this signal is reserved by the JVM.
In such a case `trap(:QUIT) {}` will raise an `ArgumentError`.
Any code that relies on being able to trap this signal will need to fall back to another available signal.

When TruffleRuby is run as part of a polyglot application, any signals that are handled by another language become unavailable for TruffleRuby to trap.

Expand Down
8 changes: 8 additions & 0 deletions spec/ruby/core/signal/trap_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,14 @@
end
end

%w[SEGV BUS ILL FPE VTALRM].each do |signal|
it "raises ArgumentError for SIG#{signal} which is reserved by Ruby" do
-> {
Signal.trap(signal, -> {})
}.should raise_error(ArgumentError, "can't trap reserved signal: SIG#{signal}")
end
end

it "allows to register a handler for all known signals, except reserved signals for which it raises ArgumentError" do
out = ruby_exe(fixture(__FILE__, "trap_all.rb"), args: "2>&1")
out.should == "OK\n"
Expand Down
5 changes: 2 additions & 3 deletions src/main/ruby/truffleruby/core/signal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,8 @@ def self.trap(signal, handler = nil, &block)

signame = self.signame(number)

if signame == 'VTALRM'
# Used internally to unblock native calls, like MRI
raise ArgumentError, "can't trap reserved signal: SIGVTALRM"
if %w[SEGV BUS ILL FPE VTALRM].include?(signame)
raise ArgumentError, "can't trap reserved signal: SIG#{signame}"
end

handler ||= block
Expand Down
23 changes: 11 additions & 12 deletions test/mri/excludes/TestSignal.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
exclude :test_hup_me, "needs investigation"
exclude :test_invalid_signal_name, "needs investigation"
exclude :test_kill_immediately_before_termination, "needs investigation"
exclude :test_sigchld_ignore, "needs investigation"
exclude :test_sigexit, "needs investigation"
exclude :test_signal_exception, "needs investigation"
exclude :test_trap_uncatchable_KILL, "needs investigation"
exclude :test_trap_uncatchable_STOP, "needs investigation"
exclude :test_signal2, "needs investigation"
exclude :test_trap, "needs investigation"
exclude :test_trap_system_default, "needs investigation"
exclude :test_reserved_signal, "needs investigation"
exclude :test_kill_immediately_before_termination, "pid 43233 exit 0."
exclude :test_trap_uncatchable_STOP, "SIGSTOP is not allowed to be caught."
exclude :test_hup_me, "assert_separately failed"
exclude :test_sigexit, "pid 43488 exit 0."
exclude :test_trap_uncatchable_KILL, "SIGKILL is not allowed to be caught."
exclude :test_sigchld_ignore, "Errno::ECHILD expected but nothing was raised."
exclude :test_invalid_signal_name, "Expected Exception(ArgumentError) was raised, but the message doesn't match."
exclude :test_signal_exception, "Expected /SIG-SIG/ to not match \"invalid signal name SIG-SIGEXIT\"."
exclude :test_trap, "ArgumentError: Unsupported command ''"
exclude :test_signal2, "ArgumentError: ArgumentError"
exclude :test_trap_system_default, "ArgumentError: Signal already used by VM or OS: SIGQUIT"

0 comments on commit 32dd3e1

Please sign in to comment.