Skip to content

Commit

Permalink
Fix trap API when used with debug gem (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
noteflakes committed Jun 17, 2023
1 parent 6bb7a14 commit 99787a6
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 17 deletions.
4 changes: 2 additions & 2 deletions lib/polyphony.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ def exit_forked_process
# @!visibility private
def install_terminating_signal_handlers
trap('SIGTERM') { raise SystemExit }
orig_trap('SIGINT') do
orig_trap('SIGINT') { exit! }
Kernel.orig_trap('SIGINT') do
Kernel.orig_trap('SIGINT') { exit! }
Fiber.schedule_priority_oob_fiber { raise Interrupt }
end
end
Expand Down
66 changes: 51 additions & 15 deletions lib/polyphony/extensions/kernel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@

require 'open3'

module Polyphony
module TrapInterceptor
def trap(sig, command = nil, &block)
return super(sig, command) if command.is_a? String

block = command if !block && command.respond_to?(:call)

# The signal trap can be invoked at any time, including while the system
# backend is blocking while polling for events. In order to deal with this
# correctly, we run the signal handler code in an out-of-band, priority
# scheduled fiber, that will pass any uncaught exception (including
# SystemExit and Interrupt) to the main thread's main fiber. See also
# `Fiber#schedule_priority_oob_fiber`.
super(sig) do
Fiber.schedule_priority_oob_fiber(&block)
end
end
end
end

# Kernel extensions (methods available to all objects / call sites)
module ::Kernel
# @!visibility private
Expand Down Expand Up @@ -72,6 +92,9 @@ def system(*args)
end

class << self
# @!visibility private
alias_method :orig_trap, :trap

# @!visibility private
alias_method :orig_system, :system

Expand All @@ -89,30 +112,43 @@ def system(*args)
end
end


# @!visibility private
alias_method :orig_trap, :trap

# @!visibility private
def trap(sig, command = nil, &block)
return orig_trap(sig, command) if command.is_a? String

block = command if !block && command.respond_to?(:call)

# The signal trap can be invoked at any time, including while the system
# backend is blocking while polling for events. In order to deal with this
# correctly, we run the signal handler code in an out-of-band, priority
# scheduled fiber, that will pass any uncaught exception (including
# SystemExit and Interrupt) to the main thread's main fiber. See also
# `Fiber#schedule_priority_oob_fiber`.
orig_trap(sig) do
Fiber.schedule_priority_oob_fiber(&block)
end
prepend Polyphony::TrapInterceptor

class << self
prepend Polyphony::TrapInterceptor
end

# # @!visibility private
# def trap(sig, command = nil, &block)
# return orig_trap(sig, command) if command.is_a? String

# block = command if !block && command.respond_to?(:call)

# # The signal trap can be invoked at any time, including while the system
# # backend is blocking while polling for events. In order to deal with this
# # correctly, we run the signal handler code in an out-of-band, priority
# # scheduled fiber, that will pass any uncaught exception (including
# # SystemExit and Interrupt) to the main thread's main fiber. See also
# # `Fiber#schedule_priority_oob_fiber`.
# orig_trap(sig) do
# Fiber.schedule_priority_oob_fiber(&block)
# end
# end

private

# @!visibility private
def pipe_to_eof(src, dest)
src.read_loop { |data| dest << data }
end
end

module ::Process
class << self
prepend Polyphony::TrapInterceptor
end
end

0 comments on commit 99787a6

Please sign in to comment.