Skip to content

Instantly share code, notes, and snippets.

@eregon
Created May 22, 2021 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eregon/9022f5709fa054fc4e488d7de085b254 to your computer and use it in GitHub Desktop.
Save eregon/9022f5709fa054fc4e488d7de085b254 to your computer and use it in GitHub Desktop.
$ready = false
thread = Thread.new do
loop do
$ready = true
begin
Thread.pass
rescue => e
p e
ensure
Thread.handle_interrupt(Object => :never) do # backtrace reports it happens here
begin
Thread.pass
rescue => e
abort "ensure saw #{e}"
end
end
end
end
end
1000.times {
Thread.pass until $ready
thread.raise "timeout"
Thread.pass
}
ruby -v interrupt.rb |& cat
ruby 3.0.1p64 (2021-04-05 revision 0fb782ee38) [x86_64-linux]
#<RuntimeError: timeout>
#<Thread:0x00000000025c32b0 interrupt.rb:2 run> terminated with exception (report_on_exception is true):
interrupt.rb:10:in `handle_interrupt': timeout (RuntimeError)
from interrupt.rb:10:in `ensure in block (2 levels) in <main>'
from interrupt.rb:10:in `block (2 levels) in <main>'
from interrupt.rb:3:in `loop'
from interrupt.rb:3:in `block in <main>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment