Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer thread current raise #30

Merged
merged 2 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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