Skip to content

Commit

Permalink
Prefer Thread.current.raise to avoid breaking `Thread.handle_interr…
Browse files Browse the repository at this point in the history
…upt`. (#30)
  • Loading branch information
ioquatix authored Feb 20, 2024
1 parent e7036c7 commit f0b4b5e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 7 deletions.
4 changes: 2 additions & 2 deletions examples/minimal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ def initialize(&block)
@status = nil

@pid = Process.fork do
Signal.trap(:INT) {raise Interrupt}
Signal.trap(:INT) {raise Terminate}
Signal.trap(:INT) {::Thread.current.raise(Interrupt)}
Signal.trap(:INT) {::Thread.current.raise(Terminate)}

@channel.in.close

Expand Down
7 changes: 4 additions & 3 deletions lib/async/container/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,16 +176,17 @@ def reload
# Enter the controller run loop, trapping `SIGINT` and `SIGTERM`.
def run
# I thought this was the default... but it doesn't always raise an exception unless you do this explicitly.
# We use `Thread.current.raise(...)` so that exceptions are filtered through `Thread.handle_interrupt` correctly.
interrupt_action = Signal.trap(:INT) do
raise Interrupt
::Thread.current.raise(Interrupt)
end

terminate_action = Signal.trap(:TERM) do
raise Terminate
::Thread.current.raise(Terminate)
end

hangup_action = Signal.trap(:HUP) do
raise Hangup
::Thread.current.raise(Hangup)
end

self.start
Expand Down
2 changes: 2 additions & 0 deletions lib/async/container/generic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ def initialize(**options)
@keyed = {}
end

attr :group

attr :state

# A human readable representation of the container.
Expand Down
5 changes: 3 additions & 2 deletions lib/async/container/process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ def exec(*arguments, ready: true, **options)
def self.fork(**options)
self.new(**options) do |process|
::Process.fork do
Signal.trap(:INT) {raise Interrupt}
Signal.trap(:TERM) {raise Terminate}
# We use `Thread.current.raise(...)` so that exceptions are filtered through `Thread.handle_interrupt` correctly.
Signal.trap(:INT) {::Thread.current.raise(Interrupt)}
Signal.trap(:TERM) {::Thread.current.raise(Terminate)}

begin
yield Instance.for(process)
Expand Down
27 changes: 27 additions & 0 deletions test/async/container/forked.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,33 @@
expect(container.statistics.restarts).to be == 2
end

it "can handle interrupts" do
finished = IO.pipe
interrupted = IO.pipe

container.spawn(restart: true) do |instance|
Thread.handle_interrupt(Interrupt => :never) do
instance.ready!

finished.first.gets
rescue ::Interrupt
interrupted.last.puts "incorrectly interrupted"
end
rescue ::Interrupt
interrupted.last.puts "correctly interrupted"
end

container.wait_until_ready

container.group.interrupt
sleep(0.001)
finished.last.puts "finished"

expect(interrupted.first.gets).to be == "correctly interrupted\n"

container.stop
end

it "should be multiprocess" do
expect(subject).to be(:multiprocess?)
end
Expand Down

0 comments on commit f0b4b5e

Please sign in to comment.