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

Revert "Add default interrupt handlers" #13673

Merged
merged 1 commit into from
Jul 18, 2023
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
61 changes: 0 additions & 61 deletions spec/std/kernel_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -294,64 +294,3 @@ describe "hardware exception" do
error.should contain("Stack overflow")
end
end

private def compile_and_run_exit_handler(&block : Process -> _)
with_tempfile("source_file") do |source_file|
File.write(source_file, <<-CRYSTAL)
at_exit { print "Exiting" }
print "."
STDOUT.flush
sleep
CRYSTAL
output = nil
compile_file(source_file) do |executable_file|
error = IO::Memory.new
process = Process.new executable_file, output: :pipe, error: error

spawn do
process.output.read_byte
block.call(process)
output = process.output.gets_to_end
end

status = process.wait
{status, output, error.to_s}
end
end
end

describe "default interrupt handlers", tags: %w[slow] do
# TODO: Implementation for sending console control commands on Windows.
# So long this behaviour can only be tested manually.
#
# ```
# lib LibC
# fun GenerateConsoleCtrlEvent(dwCtrlEvent : DWORD, dwProcessGroupId : DWORD) : BOOL
# end

# at_exit { print "Exiting"; }
# print "."
# STDOUT.flush
# LibC.GenerateConsoleCtrlEvent(LibC::CTRL_C_EVENT, 0)
# sleep
# ```
{% unless flag?(:windows) %}
it "handler for SIGINT" do
status, output, _ = compile_and_run_exit_handler(&.signal(Signal::INT))
output.should eq "Exiting"
status.inspect.should eq "Process::Status[130]"
end

it "handler for SIGTERM" do
status, output, _ = compile_and_run_exit_handler(&.terminate)
output.should eq "Exiting"
status.inspect.should eq "Process::Status[143]"
end
{% end %}

it "no handler for SIGKILL" do
status, output, _ = compile_and_run_exit_handler(&.terminate(graceful: false))
output.should eq ""
status.inspect.should eq {{ flag?(:unix) ? "Process::Status[Signal::KILL]" : "Process::Status[1]" }}
end
end
3 changes: 0 additions & 3 deletions src/crystal/system/process.cr
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ struct Crystal::System::Process
# thread.
# def self.start_interrupt_loop

# Trap interrupt to exit normally with `at_exit` handlers being executed.
# def self.setup_default_interrupt_handler

# Whether the process identified by *pid* is still registered in the system.
# def self.exists?(pid : Int) : Bool

Expand Down
6 changes: 0 additions & 6 deletions src/crystal/system/unix/process.cr
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,6 @@ struct Crystal::System::Process
# do nothing; `Crystal::System::Signal.start_loop` takes care of this
end

def self.setup_default_interrupt_handlers
# Status 128 + signal number indicates process exit was caused by the signal.
::Signal::INT.trap { ::exit 128 + ::Signal::INT.value }
::Signal::TERM.trap { ::exit 128 + ::Signal::TERM.value }
end

def self.exists?(pid)
ret = LibC.kill(pid, 0)
if ret == 0
Expand Down
7 changes: 0 additions & 7 deletions src/crystal/system/win32/process.cr
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,6 @@ struct Crystal::System::Process
end
end

def self.setup_default_interrupt_handlers
on_interrupt do
# Exit code 3 indicates `std::abort` was called which corresponds to the interrupt handler
::exit 3
end
end

def self.exists?(pid)
handle = LibC.OpenProcess(LibC::PROCESS_QUERY_INFORMATION, 0, pid)
return false unless handle
Expand Down
1 change: 0 additions & 1 deletion src/kernel.cr
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,6 @@ end
{% else %}
Crystal::System::Signal.setup_default_handlers
{% end %}
Crystal::System::Process.setup_default_interrupt_handlers

# load debug info on start up of the program is executed with CRYSTAL_LOAD_DEBUG_INFO=1
# this will make debug info available on print_frame that is used by Crystal's segfault handler
Expand Down
20 changes: 4 additions & 16 deletions src/process.cr
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ class Process
# * On Unix-like systems, this traps `SIGINT`.
# * On Windows, this captures <kbd>Ctrl</kbd> + <kbd>C</kbd> and
# <kbd>Ctrl</kbd> + <kbd>Break</kbd> signals sent to a console application.
#
# The default interrupt handler calls `::exit` to ensure `at_exit` handlers
# execute. It returns a platform-specific status code indicating an interrupt
# (`130` on Unix, `3` on Windows).
def self.on_interrupt(&handler : ->) : Nil
Crystal::System::Process.on_interrupt(&handler)
end
Expand All @@ -76,14 +72,8 @@ class Process
end

# Restores default handling of interrupt requests.
#
# The default interrupt handler calls `::exit` to ensure `at_exit` handlers
# execute. It returns a platform-specific status code indicating an interrupt
# (`130` on Unix, `3` on Windows).
def self.restore_interrupts! : Nil
Crystal::System::Process.restore_interrupts!

Crystal::System::Process.setup_default_interrupt_handlers
end

# Returns `true` if the process identified by *pid* is valid for
Expand Down Expand Up @@ -311,12 +301,10 @@ class Process
end
end

{% if flag?(:unix) %}
# :nodoc:
def initialize(pid : LibC::PidT)
@process_info = Crystal::System::Process.new(pid)
end
{% end %}
# :nodoc:
def initialize(pid : LibC::PidT)
@process_info = Crystal::System::Process.new(pid)
end

# Sends *signal* to this process.
#
Expand Down
Loading