Skip to content

Commit 7ddfad7

Browse files
committed
Use the same reserved signals as CRuby and add specs for it
1 parent 070598d commit 7ddfad7

File tree

4 files changed

+17
-11
lines changed

4 files changed

+17
-11
lines changed

doc/user/compatibility.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,15 @@ Error message strings will sometimes differ from MRI, as these are not generally
141141

142142
### Signals
143143

144-
The set of signals that TruffleRuby can handle is different from MRI.
145-
When using the native configuration, TruffleRuby allows trapping all the same signals that MRI does, as well as a few that MRI does not.
146-
The only signals that can't be trapped are `KILL`, `STOP`, and `VTALRM`.
144+
First, `KILL` and `STOP` can never be trapped, per POSIX (`man 2 signal`).
145+
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`.
146+
147+
When using the native configuration, TruffleRuby allows trapping all the same signals that MRI does.
147148
Consequently, any signal handling code that runs on MRI can run on TruffleRuby without modification in the native configuration.
148149

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

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

spec/ruby/core/signal/trap_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,14 @@
264264
end
265265
end
266266

267+
%w[SEGV BUS ILL FPE VTALRM].each do |signal|
268+
it "raises ArgumentError for SIG#{signal} which is reserved by Ruby" do
269+
-> {
270+
Signal.trap(signal, -> {})
271+
}.should raise_error(ArgumentError, "can't trap reserved signal: SIG#{signal}")
272+
end
273+
end
274+
267275
it "allows to register a handler for all known signals, except reserved signals for which it raises ArgumentError" do
268276
out = ruby_exe(fixture(__FILE__, "trap_all.rb"), args: "2>&1")
269277
out.should == "OK\n"

src/main/ruby/truffleruby/core/signal.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,8 @@ def self.trap(signal, handler = nil, &block)
8484

8585
signame = self.signame(number)
8686

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

9291
handler ||= block

test/mri/excludes/TestSignal.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,3 @@
99
exclude :test_signal2, "needs investigation"
1010
exclude :test_trap, "needs investigation"
1111
exclude :test_trap_system_default, "needs investigation"
12-
exclude :test_reserved_signal, "needs investigation"

0 commit comments

Comments
 (0)