# Buggy code
def _run_helper(self, final_cmd, with_output=False, exit_on_fail=False, silent=False):
    """Run a command that was already setup with SSH and `bash` settings.

    Args:
        cmd (List[str]):
            Full command to run. Should include SSH options and other
            processing that we do.
        with_output (bool):
            If `with_output` is `True`, command stdout and stderr
            will be captured and returned.
        exit_on_fail (bool):
            If `exit_on_fail` is `True`, the process will exit
            if the command fails (exits with a code other than 0).

    Raises:
        ProcessRunnerError if using new log style and disabled
            login shells.
        click.ClickException if using login shells.
    """
    try:
        # For now, if the output is needed we just skip the new logic.
        # In the future we could update the new logic to support
        # capturing output, but it is probably not needed.
        if not cli_logger.old_style and not with_output:
            return run_cmd_redirected(
                final_cmd,
                process_runner=self.process_runner,
                silent=silent,
                use_login_shells=is_using_login_shells(),
            )
        if with_output:
            return self.process_runner.check_output(final_cmd)
        else:
            return self.process_runner.check_call(final_cmd)
    except subprocess.CalledProcessError as e:
        quoted_cmd = " ".join(final_cmd[:-1] + [quote(final_cmd[-1])])
        if not cli_logger.old_style and not is_using_login_shells():
            raise ProcessRunnerError(
                "Command failed",
                "ssh_command_failed",
                code=e.returncode,
                command=quoted_cmd,
            )

        if exit_on_fail:
            raise click.ClickException(
                "Command failed:\n\n  {}\n".format(quoted_cmd)
            ) from None
        else:
            fail_msg = "SSH command failed."
            if is_output_redirected():
                fail_msg += " See above for the output from the failure."
            raise click.ClickException(fail_msg) from None


# Traceback (subprocess.CalledProcessError)
Traceback (most recent call last):
File "XXX/lib/python3.7/site-packages/ray/autoscaler/command_runner.py", line 248, in run
self.process_runner.check_call(final_cmd, shell=True)
File "/Users/mkoh/.pyenv/versions/3.7.7/lib/python3.7/subprocess.py", line 363, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command 'kubectl -n nlp exec -it ray-head-22r7w -- bash --login -c -i 'true &amp;&amp; source ~/.bashrc &amp;&amp; export OMP_NUM_THREADS=1 PYTHONWARNINGS=ignore &amp;&amp; (tmux kill-session -t flambe)'' returned non-zero exit status 1.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/Users/mkoh/projects/flambe-internal/flambe/runner/run.py", line 86, in main
save_to_db=args.save,
File "/Users/mkoh/projects/flambe-internal/flambe/workflow/workflow.py", line 124, in run_remote_experiment
save_to_db=save_to_db,
File "/Users/mkoh/projects/flambe-internal/flambe/cluster/ray/ray_util/ray_cluster.py", line 263, in run
self.kill("flambe")
File "/Users/mkoh/projects/flambe-internal/flambe/cluster/ray/ray_util/ray_cluster.py", line 163, in kill
self.exec_cluster(cmd=cmd)
File "/Users/mkoh/projects/flambe-internal/flambe/cluster/ray/ray_util/ray_cluster.py", line 60, in exec_cluster
with_output=with_output,
File "XXX/lib/python3.7/site-packages/ray/autoscaler/commands.py", line 868, in exec_cluster
shutdown_after_run=shutdown_after_run)
File "XXX/lib/python3.7/site-packages/ray/autoscaler/commands.py", line 919, in _exec
shutdown_after_run=shutdown_after_run)
File "XXX/lib/python3.7/site-packages/ray/autoscaler/command_runner.py", line 252, in run
[quote(final_cmd[-1])])
TypeError: can only concatenate str (not "list") to str

# Fixed code
def _run_helper(self, final_cmd, with_output=False, exit_on_fail=False, silent=False):
    """Run a command that was already setup with SSH and `bash` settings.

    Args:
        cmd (List[str]):
            Full command to run. Should include SSH options and other
            processing that we do.
        with_output (bool):
            If `with_output` is `True`, command stdout and stderr
            will be captured and returned.
        exit_on_fail (bool):
            If `exit_on_fail` is `True`, the process will exit
            if the command fails (exits with a code other than 0).

    Raises:
        ProcessRunnerError if using new log style and disabled
            login shells.
        click.ClickException if using login shells.
    """
    try:
        # For now, if the output is needed we just skip the new logic.
        # In the future we could update the new logic to support
        # capturing output, but it is probably not needed.
        if not cli_logger.old_style and not with_output:
            return run_cmd_redirected(
                final_cmd,
                process_runner=self.process_runner,
                silent=silent,
                use_login_shells=is_using_login_shells(),
            )
        if with_output:
            return self.process_runner.check_output(final_cmd)
        else:
            return self.process_runner.check_call(final_cmd)
    except subprocess.CalledProcessError as e:
        joined_cmd = " ".join(final_cmd)
        if not cli_logger.old_style and not is_using_login_shells():
            raise ProcessRunnerError(
                "Command failed",
                "ssh_command_failed",
                code=e.returncode,
                command=joined_cmd,
            )

        if exit_on_fail:
            raise click.ClickException(
                "Command failed:\n\n  {}\n".format(joined_cmd)
            ) from None
        else:
            fail_msg = "SSH command failed."
            if is_output_redirected():
                fail_msg += " See above for the output from the failure."
            raise click.ClickException(fail_msg) from None