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
case fork_mode
when :parent
parent_hook = -> child_pid {
}
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 {
child_hook.call
given_block.call
}
pid = super(&new_block)
parent_hook.call(pid)
pid
else
if pid = super
parent_hook.call pid
else
child_hook.call
end
pid
end
end