diff options
author | Stan Lo <stan.lo@shopify.com> | 2022-12-08 19:11:47 +0000 |
---|---|---|
committer | git <svn-admin@ruby-lang.org> | 2022-12-08 19:11:51 +0000 |
commit | a87f802f1ecc2f1442f4590dc58068a72f68eae9 (patch) | |
tree | c8aa24e551aa2f7599fc62d1991022249d72fff0 | |
parent | 3956bb859c2442d34ea171db8f92f3e5895c43d9 (diff) | |
download | ruby-a87f802f1ecc2f1442f4590dc58068a72f68eae9.tar.gz |
[ruby/irb] Kill PTY process after test is finished
(https://github.com/ruby/irb/pull/471)
The killing/waiting logic is borrowed from ruby/debug:
https://github.com/ruby/debug/blob/ec5ae5aebd61a99dc84028d8dffa8e7e165c1ec6/test/support/test_case.rb#L107-L136
-rw-r--r-- | test/irb/test_debug_cmd.rb | 34 |
1 files changed, 33 insertions, 1 deletions
diff --git a/test/irb/test_debug_cmd.rb b/test/irb/test_debug_cmd.rb index 0242baa261..3ba8742304 100644 --- a/test/irb/test_debug_cmd.rb +++ b/test/irb/test_debug_cmd.rb @@ -198,6 +198,8 @@ module TestIRB private + TIMEOUT_SEC = 3 + def run_ruby_file(&block) cmd = [EnvUtil.rubybin, "-I", LIB, @ruby_file.to_path] tmp_dir = Dir.mktmpdir @@ -211,7 +213,7 @@ module TestIRB 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 @@ -226,6 +228,7 @@ module TestIRB ensure read.close write.close + kill_safely(pid) end lines.join @@ -251,6 +254,35 @@ module TestIRB 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 |