# Buggy code
def connect(
    runner: Runner,
    remote_info: RemoteInfo,
    is_container_mode: bool,
    expose: PortMapping,
) -> Tuple[int, SSH]:
    """
    Start all the processes that handle remote proxying.

    Return (local port of SOCKS proxying tunnel, SSH instance).
    """
    span = runner.span()
    # Keep local copy of pod logs, for debugging purposes:
    runner.launch(
        "kubectl logs",
        runner.kubectl(
            "logs",
            "-f",
            remote_info.pod_name,
            "--container",
            remote_info.container_name,
            "--tail=10",
        ),
        bufsize=0,
    )

    ssh = SSH(runner, find_free_port())

    # forward remote port to here, by tunneling via remote SSH server:
    runner.launch(
        "kubectl port-forward",
        runner.kubectl(
            "port-forward", remote_info.pod_name, "{}:8022".format(ssh.port)
        ),
    )

    if not ssh.wait():
        raise RuntimeError("SSH to the cluster failed to start.")

    # Create ssh tunnels. In the case of the container method, just show the
    # associated messages; the tunnels will be created in the network
    # container, where those messages are not visible to the user.
    expose_local_services(
        runner, ssh, list(expose.local_to_remote()), show_only=is_container_mode
    )

    # Start tunnels for the SOCKS proxy (local -> remote)
    # and the local server for the proxy to poll (remote -> local).
    socks_port = find_free_port()
    local_server_port = find_free_port()

    launch_local_server(runner, local_server_port)
    forward_args = [
        "-L127.0.0.1:{}:127.0.0.1:9050".format(socks_port),
        "-R9055:127.0.0.1:{}".format(local_server_port),
    ]
    runner.launch(
        "SSH port forward (socks and proxy poll)", ssh.bg_command(forward_args)
    )

    span.end()
    return socks_port, ssh


# Traceback (subprocess.CalledProcessError)
$ telepresence --docker-run --rm -it alpine sh
Volumes are rooted at $TELEPRESENCE_ROOT. See https://telepresence.io/howto/volumes.html for details.

Password:
docker: Error response from daemon: OCI runtime create failed: container_linux.go:348: starting container process caused "process_linux.go:301: running exec setns process for init caused \"exit status 21\"": unknown.
Proxy to Kubernetes exited. This is typically due to a lost connection.
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
File "/usr/local/Cellar/telepresence/0.83/libexec/lib/python3.6/site-packages/telepresence/container.py", line 183, in terminate_if_alive
mount_cleanup()
File "/usr/local/Cellar/telepresence/0.83/libexec/lib/python3.6/site-packages/telepresence/remote.py", line 284, in cleanup
runner.get_output(sudo_prefix + ["umount", "-f", mount_dir])
File "/usr/local/Cellar/telepresence/0.83/libexec/lib/python3.6/site-packages/telepresence/runner.py", line 249, in get_output
track, "Capturing", "captured", out_cb, err_cb, args, **kwargs
File "/usr/local/Cellar/telepresence/0.83/libexec/lib/python3.6/site-packages/telepresence/runner.py", line 227, in run_command
raise CalledProcessError(retcode, args)
subprocess.CalledProcessError: Command '['sudo', 'umount', '-f', '/tmp/tmpi4a80ekr']' returned non-zero exit status 1.
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
File "/usr/local/Cellar/telepresence/0.83/libexec/lib/python3.6/site-packages/telepresence/cleanup.py", line 46, in killall
killer()
File "/usr/local/Cellar/telepresence/0.83/libexec/lib/python3.6/site-packages/telepresence/container.py", line 44, in kill
runner.check_call(sudo + ["docker", "stop", "--time=1", name])
File "/usr/local/Cellar/telepresence/0.83/libexec/lib/python3.6/site-packages/telepresence/runner.py", line 236, in check_call
track, "Running", "ran", out_cb, err_cb, args, **kwargs
File "/usr/local/Cellar/telepresence/0.83/libexec/lib/python3.6/site-packages/telepresence/runner.py", line 227, in run_command
raise CalledProcessError(retcode, args)
subprocess.CalledProcessError: Command '['docker', 'stop', '--time=1', 'telepresence-1523999897-453038-83847']' returned non-zero exit status 1.

# Fixed code
def connect(
    runner: Runner,
    remote_info: RemoteInfo,
    is_container_mode: bool,
    expose: PortMapping,
) -> Tuple[int, SSH]:
    """
    Start all the processes that handle remote proxying.

    Return (local port of SOCKS proxying tunnel, SSH instance).
    """
    span = runner.span()
    # Keep local copy of pod logs, for debugging purposes. Set is_critical to
    # False so logs failing doesn't bring down the Telepresence session.
    runner.launch(
        "kubectl logs",
        runner.kubectl(
            "logs",
            "-f",
            remote_info.pod_name,
            "--container",
            remote_info.container_name,
            "--tail=10",
        ),
        bufsize=0,
        is_critical=False,
    )

    ssh = SSH(runner, find_free_port())

    # forward remote port to here, by tunneling via remote SSH server:
    runner.launch(
        "kubectl port-forward",
        runner.kubectl(
            "port-forward", remote_info.pod_name, "{}:8022".format(ssh.port)
        ),
    )

    if not ssh.wait():
        raise RuntimeError("SSH to the cluster failed to start.")

    # Create ssh tunnels. In the case of the container method, just show the
    # associated messages; the tunnels will be created in the network
    # container, where those messages are not visible to the user.
    expose_local_services(
        runner, ssh, list(expose.local_to_remote()), show_only=is_container_mode
    )

    # Start tunnels for the SOCKS proxy (local -> remote)
    # and the local server for the proxy to poll (remote -> local).
    socks_port = find_free_port()
    local_server_port = find_free_port()

    launch_local_server(runner, local_server_port)
    forward_args = [
        "-L127.0.0.1:{}:127.0.0.1:9050".format(socks_port),
        "-R9055:127.0.0.1:{}".format(local_server_port),
    ]
    runner.launch(
        "SSH port forward (socks and proxy poll)", ssh.bg_command(forward_args)
    )

    span.end()
    return socks_port, ssh