diff options
-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) |