sandbox: handle SIGTTOU and SIGTTIN to avoid hangs
This commit is contained in:
parent
c40b2baf9e
commit
8eb4756d3e
@ -115,17 +115,25 @@ class Sandbox
|
|||||||
end
|
end
|
||||||
|
|
||||||
write_to_pty = proc do
|
write_to_pty = proc do
|
||||||
|
# Don't hang if stdin is not able to be used - throw EIO instead.
|
||||||
|
old_ttin = trap(:TTIN, "IGNORE")
|
||||||
|
|
||||||
# Update the window size whenever the parent terminal's window size changes.
|
# Update the window size whenever the parent terminal's window size changes.
|
||||||
old_winch = trap(:WINCH, &winch)
|
old_winch = trap(:WINCH, &winch)
|
||||||
winch.call(nil)
|
winch.call(nil)
|
||||||
|
|
||||||
stdin_thread = Thread.new { IO.copy_stream($stdin, w) }
|
stdin_thread = Thread.new do
|
||||||
|
IO.copy_stream($stdin, w)
|
||||||
|
rescue Errno::EIO
|
||||||
|
# stdin is unavailable - move on.
|
||||||
|
end
|
||||||
|
|
||||||
r.each_char { |c| print(c) }
|
r.each_char { |c| print(c) }
|
||||||
|
|
||||||
Process.wait(pid)
|
Process.wait(pid)
|
||||||
ensure
|
ensure
|
||||||
stdin_thread&.kill
|
stdin_thread&.kill
|
||||||
|
trap(:TTIN, old_ttin)
|
||||||
trap(:WINCH, old_winch)
|
trap(:WINCH, old_winch)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -134,7 +142,13 @@ class Sandbox
|
|||||||
# mode while we copy the input/output of the process spawned in the
|
# mode while we copy the input/output of the process spawned in the
|
||||||
# PTY. After we've finished copying to/from the PTY process, io.raw
|
# PTY. After we've finished copying to/from the PTY process, io.raw
|
||||||
# will restore the stdin TTY to its original state.
|
# will restore the stdin TTY to its original state.
|
||||||
$stdin.raw(&write_to_pty)
|
begin
|
||||||
|
# Ignore SIGTTOU as setting raw mode will hang if the process is in the background.
|
||||||
|
old_ttou = trap(:TTOU, "IGNORE")
|
||||||
|
$stdin.raw(&write_to_pty)
|
||||||
|
ensure
|
||||||
|
trap(:TTOU, old_ttou)
|
||||||
|
end
|
||||||
else
|
else
|
||||||
write_to_pty.call
|
write_to_pty.call
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user