Skip to content

Commit

Permalink
Prefer Thread.current.raise(...).
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Feb 20, 2024
1 parent df48675 commit 8e2a492
Show file tree
Hide file tree
Showing 3 changed files with 9 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
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

0 comments on commit 8e2a492

Please sign in to comment.