Skip to content

Commit

Permalink
Fix SSHAttach.reuse_ports_lock() when no grep matches (#1700)
Browse files Browse the repository at this point in the history
Fixes: #1689
  • Loading branch information
un-def authored Sep 18, 2024
1 parent 958a292 commit 854c812
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/dstack/_internal/core/services/ssh/attach.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def reuse_ports_lock(cls, run_name: str) -> Optional[PortsLock]:
output: bytes
if IS_WINDOWS:
filter_prefix = "powershell"
# This script always returns zero exit status.
output = subprocess.check_output(
[
"powershell",
Expand All @@ -47,10 +48,21 @@ def reuse_ports_lock(cls, run_name: str) -> Optional[PortsLock]:
else:
filter_prefix = "grep"
ps = subprocess.Popen(("ps", "-A", "-o", "command"), stdout=subprocess.PIPE)
output = subprocess.check_output(
["grep", "--", f"-S {control_sock_path}"], stdin=ps.stdout
cp = subprocess.run(
["grep", "-F", "--", f"-S {control_sock_path}"],
stdin=ps.stdout,
stdout=subprocess.PIPE,
)
ps.wait()
# From grep man page: "the exit status is 0 if a line is selected,
# 1 if no lines were selected, and 2 if an error occurred".
if cp.returncode == 1:
return None
if cp.returncode != 0:
raise SSHError(
f"Unexpected grep exit status {cp.returncode} while searching for ssh process"
)
output = cp.stdout
commands = list(
filter(lambda s: not s.startswith(filter_prefix), output.decode().strip().split("\n"))
)
Expand Down

0 comments on commit 854c812

Please sign in to comment.