Skip to content

[machinelearningservices] Add retry logic to az ml ssh client websocket connection - #10259

Open
lavakumarrepala wants to merge 1 commit into
Azure:mainfrom
lavakumarrepala:feature/ssh-connector-retry-logic
Open

[machinelearningservices] Add retry logic to az ml ssh client websocket connection#10259
lavakumarrepala wants to merge 1 commit into
Azure:mainfrom
lavakumarrepala:feature/ssh-connector-retry-logic

Conversation

@lavakumarrepala

@lavakumarrepala lavakumarrepala commented Aug 26, 2026

Copy link
Copy Markdown
Member

🤖 PR Validation — ️✔️ All clear

Breaking Changes
️✔️ None

Summary

�z ml job connect-ssh (and connect-ssh for compute instances) proxies SSH traffic over a websocket tunnel via _ssh_connector.py. Today, if the initial websocket handshake to the NBIP proxy/broker is refused or times out, the client fails immediately with no retry.

SRE Agent investigation of SSH connection instability (ADO work item https://msdata.visualstudio.com/Vienna/_workitems/edit/5533560/) identified this as a remaining client-side gap: backend forwarder/broker stability fixes already improved connection duration stats, but the az ml SSH client still lacks the retry behavior that SSH clients such as VS Code Remote-SSH have (which show meaningfully better long-duration connection stats as a result).

Change

  • Added _connect_with_retry(), an async context manager that retries the initial websocket connection with exponential backoff + jitter on transient failures (\OSError, \TimeoutError, websocket handshake/connection-closed errors during connect).
  • Retry behavior is configurable via env vars: \AZUREML_SSH_CONNECT_MAX_RETRIES\ (default 5), \AZUREML_SSH_CONNECT_RETRY_BASE_DELAY_SECONDS\ (default 1), \AZUREML_SSH_CONNECT_RETRY_MAX_DELAY_SECONDS\ (default 30).
  • Once retries are exhausted, the original exception is re-raised so behavior is unchanged for persistent failures.
  • Deliberately out of scope: reconnecting once the tunnel is open and actively relaying SSH bytes. This tunnel carries the raw SSH transport, so a new websocket connection cannot resume an in-flight SSH session — a silent mid-stream reconnect would corrupt the SSH session rather than heal it. Recovering from mid-session drops requires server-side session continuity, tracked separately under the NBIP rearchitecture work item (https://msdata.visualstudio.com/Vienna/_workitems/edit/5533562).

Testing

  • \python -m py_compile\ on the modified file.
  • \pyflakes\ shows no new warnings (pre-existing unused TYPE_CHECKING import only).
  • Manually verified retry behavior with a mocked \websockets.client.connect:
    • Transient failures (e.g. simulated \TimeoutError) are retried with backoff and the connection succeeds once the mock stops failing.
    • When failures persist past \AZUREML_SSH_CONNECT_MAX_RETRIES, the original exception propagates as before.

Opening as draft for review/discussion before merge.

The az ml job connect-ssh / connect-ssh-compute proxy (_ssh_connector.py)
would immediately fail if the initial websocket handshake to the
NBIP proxy/broker was refused or timed out, with no retry. SRE Agent
investigation of SSH connection instability found this to be a
remaining gap versus SSH clients such as VS Code Remote-SSH, which do
retry the initial connection and see meaningfully better long-duration
connection stats.

Add _connect_with_retry(), an async context manager that retries the
initial websocket connection with exponential backoff + jitter
(configurable via AZUREML_SSH_CONNECT_MAX_RETRIES,
AZUREML_SSH_CONNECT_RETRY_BASE_DELAY_SECONDS,
AZUREML_SSH_CONNECT_RETRY_MAX_DELAY_SECONDS env vars) on transient
failures (OSError, TimeoutError, websocket handshake/connection-closed
errors), then re-raises once retries are exhausted.

Once the tunnel is open and actively relaying SSH bytes we do not
attempt a silent reconnect: this tunnel carries the raw SSH transport,
so a new websocket connection cannot resume an in-flight SSH session.
Recovering from mid-session drops requires server-side session
continuity, tracked separately (NBIP rearchitecture work item).
@azure-client-tools-bot-prd

Copy link
Copy Markdown

Hi lavakumarrepala,
Please write the description of changes which can be perceived by customers into HISTORY.rst.
If you want to release a new extension version, please update the version in pyproject.toml (or setup.py, if the extension has not migrated yet) as well.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@a0x1ab

Copy link
Copy Markdown
Member

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).

@lavakumarrepala
lavakumarrepala marked this pull request as ready for review September 2, 2026 00:10
Copilot AI lite review requested due to automatic review settings September 2, 2026 00:10
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new retry delay computation and env var handling can yield invalid sleep durations (and exceed the configured max delay), which can break retries under certain configurations.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR improves resiliency of az ml job connect-ssh (and compute instance connect-ssh) by adding retry logic around the initial WebSocket tunnel establishment in _ssh_connector.py, addressing transient proxy/broker handshake failures without attempting mid-session reconnects.

Changes:

  • Introduces an async context manager _connect_with_retry() to retry initial WebSocket connections with exponential backoff + jitter on retryable connect-time exceptions.
  • Adds env var configuration for retry count and backoff parameters (AZUREML_SSH_CONNECT_MAX_RETRIES, ...BASE_DELAY_SECONDS, ...MAX_DELAY_SECONDS).
  • Switches the SSH tunnel setup to use _connect_with_retry() instead of calling websockets.client.connect() directly.
File summaries
File Description
src/machinelearningservices/azext_mlv2/manual/custom/_ssh_connector.py Adds a retrying WebSocket connect wrapper and uses it for the SSH-over-WebSocket tunnel.
Review details

Suppressed comments (1)

src/machinelearningservices/azext_mlv2/manual/custom/_ssh_connector.py:103

  • delay is clamped to max_delay before adding jitter, so the actual sleep can exceed AZUREML_SSH_CONNECT_RETRY_MAX_DELAY_SECONDS by up to base_delay. If max_delay is meant to be a hard cap, clamp after applying jitter.
            delay = min(max_delay, base_delay * (2 ** (attempt - 1))) + random.uniform(0, base_delay)
  • Files reviewed: 1/1 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +86 to +88
max_retries = _get_int_env("AZUREML_SSH_CONNECT_MAX_RETRIES", 5)
base_delay = _get_float_env("AZUREML_SSH_CONNECT_RETRY_BASE_DELAY_SECONDS", 1.0)
max_delay = _get_float_env("AZUREML_SSH_CONNECT_RETRY_MAX_DELAY_SECONDS", 30.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants