diff options
author | Jun Omae <jun66j5@gmail.com> | 2022-03-19 08:18:40 +0900 |
---|---|---|
committer | Jeff Forcier <jeff@bitprophet.org> | 2022-05-13 13:40:39 -0400 |
commit | b30b8e7c314be021f8e6e62246229085374857a3 (patch) | |
tree | 64ad70d79efec6d80c64cd135eadc13d43e31150 | |
parent | 19490cfa97b26fc93be2ca860ccbd4ac62e463aa (diff) |
Fix `Errno 22` when connecting pipe of openssh ssh agent
-rw-r--r-- | paramiko/win_openssh.py | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/paramiko/win_openssh.py b/paramiko/win_openssh.py index ece7c8fd..eca7b86a 100644 --- a/paramiko/win_openssh.py +++ b/paramiko/win_openssh.py @@ -18,23 +18,39 @@ # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. import os.path +import time PIPE_NAME = r"\\.\pipe\openssh-ssh-agent" def can_talk_to_agent(): - return os.path.exists(PIPE_NAME) + # use os.listdir() instead of os.path.exists(), because os.path.exists() + # uses CreateFileW() API and the pipe cannot be reopen unless the server + # calls DisconnectNamedPipe(). + dir_, name = os.path.split(PIPE_NAME) + name = name.lower() + return any(name == n.lower() for n in os.listdir(dir_)) class OpenSSHAgentConnection: def __init__(self): - self._pipe = open(PIPE_NAME, "rb+", buffering=0) + while True: + try: + self._pipe = os.open(PIPE_NAME, os.O_RDWR | os.O_BINARY) + except OSError as e: + # retry when errno 22 which means that the server has not + # called DisconnectNamedPipe() yet. + if e.errno != 22: + raise + else: + break + time.sleep(0.1) def send(self, data): - return self._pipe.write(data) + return os.write(self._pipe, data) def recv(self, n): - return self._pipe.read(n) + return os.read(self._pipe, n) def close(self): - return self._pipe.close() + return os.close(self._pipe) |