[autoscaler] Flush stdout and stdin when running commands. (#19473)

Flush command stdout/stderr before exiting CommandRunner.run, so that setup command output is less likely to get swallowed.
This commit is contained in:
Dmitri Gekhtman 2022-05-23 23:17:30 -07:00 committed by GitHub
parent b05b28b209
commit 806c187878
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 13 additions and 10 deletions

View file

@ -443,8 +443,8 @@ class SSHCommandRunner(CommandRunnerInterface):
Full command to run. Should include SSH options and other Full command to run. Should include SSH options and other
processing that we do. processing that we do.
with_output (bool): with_output (bool):
If `with_output` is `True`, command stdout and stderr If `with_output` is `True`, command stdout will be captured and
will be captured and returned. returned.
exit_on_fail (bool): exit_on_fail (bool):
If `exit_on_fail` is `True`, the process will exit If `exit_on_fail` is `True`, the process will exit
if the command fails (exits with a code other than 0). if the command fails (exits with a code other than 0).
@ -465,10 +465,8 @@ class SSHCommandRunner(CommandRunnerInterface):
silent=silent, silent=silent,
use_login_shells=is_using_login_shells(), use_login_shells=is_using_login_shells(),
) )
if with_output:
return self.process_runner.check_output(final_cmd)
else: else:
return self.process_runner.check_call(final_cmd) return self.process_runner.check_output(final_cmd)
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
joined_cmd = " ".join(final_cmd) joined_cmd = " ".join(final_cmd)
if not is_using_login_shells(): if not is_using_login_shells():
@ -488,6 +486,11 @@ class SSHCommandRunner(CommandRunnerInterface):
if is_output_redirected(): if is_output_redirected():
fail_msg += " See above for the output from the failure." fail_msg += " See above for the output from the failure."
raise click.ClickException(fail_msg) from None raise click.ClickException(fail_msg) from None
finally:
# Do our best to flush output to terminal.
# See https://github.com/ray-project/ray/pull/19473.
sys.stdout.flush()
sys.stderr.flush()
def run( def run(
self, self,

View file

@ -325,7 +325,7 @@ def run_cmd_redirected(
process_runner: Process runner used for executing commands. process_runner: Process runner used for executing commands.
silent (bool): If true, the command output will be silenced completely silent (bool): If true, the command output will be silenced completely
(redirected to /dev/null), unless verbose logging (redirected to /dev/null), unless verbose logging
is enabled. Use this for runnign utility commands like is enabled. Use this for running utility commands like
rsync. rsync.
""" """
if silent and cli_logger.verbosity < 1: if silent and cli_logger.verbosity < 1:

View file

@ -436,7 +436,7 @@ class NodeUpdater:
_numbered=("[]", 4, NUM_SETUP_STEPS), _numbered=("[]", 4, NUM_SETUP_STEPS),
) )
with cli_logger.group( with cli_logger.group(
"Initalizing command runner", "Initializing command runner",
# todo: fix command numbering # todo: fix command numbering
_numbered=("[]", 5, NUM_SETUP_STEPS), _numbered=("[]", 5, NUM_SETUP_STEPS),
): ):

View file

@ -32,7 +32,7 @@ Acquiring an up-to-date head node
\[3/7\] No worker file mounts to sync \[3/7\] No worker file mounts to sync
New status: setting-up New status: setting-up
\[4/7\] Running initialization commands \[4/7\] Running initialization commands
\[5/7\] Initalizing command runner \[5/7\] Initializing command runner
\[6/7\] Running setup commands \[6/7\] Running setup commands
\(0/4\) echo a \(0/4\) echo a
\(1/4\) echo b \(1/4\) echo b

View file

@ -32,7 +32,7 @@ Acquiring an up-to-date head node
\[3/7\] No worker file mounts to sync \[3/7\] No worker file mounts to sync
New status: setting-up New status: setting-up
\[4/7\] Running initialization commands \[4/7\] Running initialization commands
\[5/7\] Initalizing command runner \[5/7\] Initializing command runner
\[6/7\] Running setup commands \[6/7\] Running setup commands
\(0/4\) echo a \(0/4\) echo a
\(1/4\) echo b \(1/4\) echo b

View file

@ -58,7 +58,7 @@
.+\.py.*Running `echo init` .+\.py.*Running `echo init`
.+\.py.*Full command is `ssh.+` .+\.py.*Full command is `ssh.+`
.+\.py.*NodeUpdater: i-.+: Initialization commands succeeded \[LogTimer=.+\] .+\.py.*NodeUpdater: i-.+: Initialization commands succeeded \[LogTimer=.+\]
.+\.py.*\[5/7\] Initalizing command runner .+\.py.*\[5/7\] Initializing command runner
.+\.py.*\[6/7\] Running setup commands .+\.py.*\[6/7\] Running setup commands
.+\.py.*\(0/4\) echo a .+\.py.*\(0/4\) echo a
.+\.py.*Running `echo a` .+\.py.*Running `echo a`