module DEBUGGER__::ForkInterceptor

Public Instance Methods

fork(&given_block) click to toggle source
Calls superclass method
# File debug-1.4.0/lib/debug/session.rb, line 2066
def fork(&given_block)
  return super unless defined?(SESSION) && SESSION.active?

  unless fork_mode = CONFIG[:fork_mode]
    if CONFIG[:parent_on_fork]
      fork_mode = :parent
    else
      fork_mode = :both
    end
  end

  parent_pid = Process.pid

  # before fork
  case fork_mode
  when :parent
    parent_hook = -> child_pid {
      # Do nothing
    }
    child_hook = -> {
      DEBUGGER__.warn "Detaching after fork from child process #{Process.pid}"
      SESSION.deactivate
    }
  when :child
    SESSION.before_fork false

    parent_hook = -> child_pid {
      DEBUGGER__.warn "Detaching after fork from parent process #{Process.pid}"
      SESSION.after_fork_parent
      SESSION.deactivate
    }
    child_hook = -> {
      DEBUGGER__.warn "Attaching after process #{parent_pid} fork to child process #{Process.pid}"
      SESSION.activate on_fork: true
    }
  when :both
    SESSION.before_fork

    parent_hook = -> child_pid {
      SESSION.process_group.after_fork
      SESSION.after_fork_parent
    }
    child_hook = -> {
      DEBUGGER__.warn "Attaching after process #{parent_pid} fork to child process #{Process.pid}"
      SESSION.process_group.after_fork child: true
      SESSION.activate on_fork: true
    }
  end

  if given_block
    new_block = proc {
      # after fork: child
      child_hook.call
      given_block.call
    }
    pid = super(&new_block)
    parent_hook.call(pid)
    pid
  else
    if pid = super
      # after fork: parent
      parent_hook.call pid
    else
      # after fork: child
      child_hook.call
    end

    pid
  end
end