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

Kill PTY process after test is finished #471

Merged
merged 2 commits into from
Dec 8, 2022
Merged
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
34 changes: 33 additions & 1 deletion test/irb/test_debug_cmd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ def test_catch

private

TIMEOUT_SEC = 3

def run_ruby_file(&block)
cmd = [EnvUtil.rubybin, "-I", LIB, @ruby_file.to_path]
tmp_dir = Dir.mktmpdir
Expand All @@ -211,7 +213,7 @@ def run_ruby_file(&block)
yield

PTY.spawn(IRB_AND_DEBUGGER_OPTIONS.merge("IRBRC" => rc_file.to_path), *cmd) do |read, write, pid|
Timeout.timeout(3) do
Timeout.timeout(TIMEOUT_SEC) do
while line = safe_gets(read)
lines << line

Expand All @@ -226,6 +228,7 @@ def run_ruby_file(&block)
ensure
read.close
write.close
kill_safely(pid)
end

lines.join
Expand All @@ -251,6 +254,35 @@ def safe_gets(read)
nil
end

def kill_safely pid
return if wait_pid pid, TIMEOUT_SEC

Process.kill :TERM, pid
return if wait_pid pid, 0.2

Process.kill :KILL, pid
Process.waitpid(pid)
rescue Errno::EPERM, Errno::ESRCH
end

def wait_pid pid, sec
total_sec = 0.0
wait_sec = 0.001 # 1ms

while total_sec < sec
if Process.waitpid(pid, Process::WNOHANG) == pid
return true
end
sleep wait_sec
total_sec += wait_sec
wait_sec *= 2
end

false
rescue Errno::ECHILD
true
end

def type(command)
@commands << command
end
Expand Down