Skip to content

Commit

Permalink
Use the same reserved signals as CRuby and add specs for it
Browse files Browse the repository at this point in the history
  • Loading branch information
eregon committed Dec 6, 2023
1 parent 070598d commit 7ddfad7
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 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
1 change: 0 additions & 1 deletion test/mri/excludes/TestSignal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@
exclude :test_signal2, "needs investigation"
exclude :test_trap, "needs investigation"
exclude :test_trap_system_default, "needs investigation"
exclude :test_reserved_signal, "needs investigation"

0 comments on commit 7ddfad7

Please sign in to comment.