SSH connection is not being reused when sending multiple commands
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 7.2k
- Forks
- 1.7k
- Avg merge
- 13d 8h
- Merged PRs (30d)
- 2
Description
cmds.sh connects through SSH and executes several commands:
#!/usr/bin/env python3
import docker
if __name__ == "__main__":
client = docker.DockerClient("ssh://localhost", use_ssh_client=True)
client.ping()
client.images.list()
client.containers.list()
Running this script spawns a new ssh connection for each command which takes a lot of time because the whole login sequence is repeated:
$ strace -fs256 ./cmds.py 2>&1 | grep exec.*ssh
[pid 35337] execve("/bin/sh", ["/bin/sh", "-c", "ssh -p 22 -- localhost docker system dial-stdio"], 0x14d3a90 /* 72 vars */ <unfinished ...>
[pid 35338] execve("/usr/bin/ssh", ["ssh", "-p", "22", "--", "localhost", "docker", "system", "dial-stdio"], 0x561272c46448 /* 72 vars */ <unfinished ...>
[pid 35405] execve("/bin/sh", ["/bin/sh", "-c", "ssh -p 22 -- localhost docker system dial-stdio"], 0x14d8090 /* 72 vars */ <unfinished ...>
[pid 35406] execve("/usr/bin/ssh", ["ssh", "-p", "22", "--", "localhost", "docker", "system", "dial-stdio"], 0x55c16b5e1448 /* 72 vars */ <unfinished ...>
[pid 35465] execve("/bin/sh", ["/bin/sh", "-c", "ssh -p 22 -- localhost docker system dial-stdio"], 0x14d8090 /* 72 vars */ <unfinished ...>
[pid 35466] execve("/usr/bin/ssh", ["ssh", "-p", "22", "--", "localhost", "docker", "system", "dial-stdio"], 0x55c40c8e4448 /* 72 vars */ <unfinished ...>
[pid 35524] execve("/bin/sh", ["/bin/sh", "-c", "ssh -p 22 -- localhost docker system dial-stdio"], 0x14d8090 /* 72 vars */ <unfinished ...>
[pid 35525] execve("/usr/bin/ssh", ["ssh", "-p", "22", "--", "localhost", "docker", "system", "dial-stdio"], 0x55ae78291448 /* 72 vars */ <unfinished ...>
[pid 35583] execve("/bin/sh", ["/bin/sh", "-c", "ssh -p 22 -- localhost docker system dial-stdio"], 0x14dcfd0 /* 72 vars */ <unfinished ...>
[pid 35584] execve("/usr/bin/ssh", ["ssh", "-p", "22", "--", "localhost", "docker", "system", "dial-stdio"], 0x55b6878d6448 /* 72 vars */ <unfinished ...>
[pid 35642] execve("/bin/sh", ["/bin/sh", "-c", "ssh -p 22 -- localhost docker system dial-stdio"], 0x14dabc0 /* 72 vars */ <unfinished ...>
[pid 35643] execve("/usr/bin/ssh", ["ssh", "-p", "22", "--", "localhost", "docker", "system", "dial-stdio"], 0x562746674448 /* 72 vars */ <unfinished ...>
[pid 35702] execve("/bin/sh", ["/bin/sh", "-c", "ssh -p 22 -- localhost docker system dial-stdio"], 0x14d8090 /* 72 vars */ <unfinished ...>
[pid 35703] execve("/usr/bin/ssh", ["ssh", "-p", "22", "--", "localhost", "docker", "system", "dial-stdio"], 0x555dd2d9a448 /* 72 vars */ <unfinished ...>
I expected it to reuse the same pipe for all commands.
Even using the paramiko client (use_ssh_client=False) seems to be similar because SSHConnectionPool are created for every URL. Those URLs are different for every request.
So I don't really understand how this was supposed to work efficiently. Is there something fundamental that precludes the reuse of the SSH connection?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the cmds.py example and trace how DockerClient requests create SSH transports, including SSHConnectionPool when use_ssh_client=False. Compare the connection lifecycle across ping(), images.list(), and containers.list(); done means repeated commands reuse one SSH pipe, or the transport constraint is documented if sharing is not possible.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- docker, python
- Domain
- api, backend, networking
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100