[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:
ec5ae5aebd/test/support/test_case.rb (L107-L136)
This commit is contained in:
parent
3956bb859c
commit
a87f802f1e
@ -198,6 +198,8 @@ module TestIRB
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
TIMEOUT_SEC = 3
|
||||||
|
|
||||||
def run_ruby_file(&block)
|
def run_ruby_file(&block)
|
||||||
cmd = [EnvUtil.rubybin, "-I", LIB, @ruby_file.to_path]
|
cmd = [EnvUtil.rubybin, "-I", LIB, @ruby_file.to_path]
|
||||||
tmp_dir = Dir.mktmpdir
|
tmp_dir = Dir.mktmpdir
|
||||||
@ -211,7 +213,7 @@ module TestIRB
|
|||||||
yield
|
yield
|
||||||
|
|
||||||
PTY.spawn(IRB_AND_DEBUGGER_OPTIONS.merge("IRBRC" => rc_file.to_path), *cmd) do |read, write, pid|
|
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)
|
while line = safe_gets(read)
|
||||||
lines << line
|
lines << line
|
||||||
|
|
||||||
@ -226,6 +228,7 @@ module TestIRB
|
|||||||
ensure
|
ensure
|
||||||
read.close
|
read.close
|
||||||
write.close
|
write.close
|
||||||
|
kill_safely(pid)
|
||||||
end
|
end
|
||||||
|
|
||||||
lines.join
|
lines.join
|
||||||
@ -251,6 +254,35 @@ module TestIRB
|
|||||||
nil
|
nil
|
||||||
end
|
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)
|
def type(command)
|
||||||
@commands << command
|
@commands << command
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user