Skip to content

Commit 7ac2a16

Browse files
committed
Check if control socket exists
start_new_session is POSIX-only (ignored on Windows), and Windows-specific CREATE_NEW_PROCESS_GROUP flag doesn't work as expected (it seems that ssh still receives CTRL_C_EVENT, at least it exits immediately on Ctrl+C event).
1 parent 10934da commit 7ac2a16

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

  • src/dstack/_internal/core/services/ssh

src/dstack/_internal/core/services/ssh/tunnel.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,6 @@ def open(self) -> None:
174174
stdout=subprocess.DEVNULL,
175175
stderr=subprocess.DEVNULL,
176176
timeout=SSH_TIMEOUT,
177-
# We don't want the ssh process to receive SIGINT from the controlling terminal
178-
# (e.g., when SSHTunnel is used via CLI->Python API's SSHAttach and the dstack CLI
179-
# process is a foreground process group leader). Starting a new session ensures
180-
# that we don't have the controlling terminal at all.
181-
start_new_session=True,
182177
)
183178
except subprocess.TimeoutExpired as e:
184179
msg = f"SSH tunnel to {self.destination} did not open in {SSH_TIMEOUT} seconds"
@@ -209,6 +204,11 @@ async def aopen(self) -> None:
209204
raise get_ssh_error(stderr)
210205

211206
def close(self) -> None:
207+
if not os.path.exists(self.control_sock_path):
208+
logger.debug(
209+
"Control socket does not exist, it seems that ssh process has already exited"
210+
)
211+
return
212212
proc = subprocess.run(
213213
self.close_command(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT
214214
)
@@ -220,6 +220,11 @@ def close(self) -> None:
220220
)
221221

222222
async def aclose(self) -> None:
223+
if not os.path.exists(self.control_sock_path):
224+
logger.debug(
225+
"Control socket does not exist, it seems that ssh process has already exited"
226+
)
227+
return
223228
proc = await asyncio.create_subprocess_exec(
224229
*self.close_command(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT
225230
)

0 commit comments

Comments
 (0)