Skip to content

[App Service] Fix SSH tunnel reliability, signal handling, and instance targeting#33070

Open
seligj95 wants to merge 2 commits intoAzure:devfrom
seligj95:fix/ssh-tunnel-improvements
Open

[App Service] Fix SSH tunnel reliability, signal handling, and instance targeting#33070
seligj95 wants to merge 2 commits intoAzure:devfrom
seligj95:fix/ssh-tunnel-improvements

Conversation

@seligj95
Copy link
Copy Markdown
Contributor

@seligj95 seligj95 commented Mar 26, 2026

Description

Changes

Tunnel reliability (#8831):

  • Added WebSocket retry logic with exponential backoff (up to 5 attempts, 1s→30s delay) in TunnelServer._create_websocket_connection()
  • Added keepalive pings every 30s to prevent idle WebSocket disconnects
  • Listener loop now uses socket timeouts to support clean shutdown

Clean Ctrl+C shutdown (#13662):

  • Registered SIGINT/SIGTERM signal handlers that call tunnel_server.close()
  • Added atexit fallback cleanup
  • Added TunnelServer.close() method with idempotent, exception-safe resource cleanup
  • Wrapped main sleep loops in try/finally to ensure cleanup on KeyboardInterrupt

Instance targeting docs (#13008):

  • Improved --instance and --timeout parameter descriptions
  • Added --instance usage examples to help text for both webapp ssh and webapp create-remote-connection

Testing

  • Added 7 unit tests in test_webapp_commands_thru_mock.py covering:
    • TunnelServer.close() idempotency and resource cleanup
    • WebSocket retry logic (success on retry, failure after max attempts)
    • Keepalive ping stop behavior
    • Signal handler registration

Files Changed

  • tunnel.py — retry logic, keepalive, close(), closing event
  • custom.py — signal handlers, atexit, try/finally cleanup
  • _params.py — improved parameter help text
  • _help.py — added --instance examples
  • test_webapp_commands_thru_mock.py — new unit tests

Fixes #8831
Fixes #13662
Fixes #13008

…h`: tunnel reliability and instance targeting

- Add WebSocket retry logic with exponential backoff for tunnel connections (Azure#8831)
- Add keepalive pings to prevent idle disconnects (Azure#8831)
- Register signal handlers (SIGINT/SIGTERM) and atexit for clean tunnel shutdown (Azure#13662)
- Wrap main loops in try/finally to ensure cleanup on KeyboardInterrupt (Azure#13662)
- Add close() method to TunnelServer for deterministic resource cleanup (Azure#13662)
- Improve --instance and --timeout parameter help text (Azure#13008)
- Add --instance examples to help text for webapp ssh and create-remote-connection (Azure#13008)
- Add unit tests for tunnel retry, keepalive, close, and signal registration

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@seligj95 seligj95 requested a review from NoriZC as a code owner March 26, 2026 15:29
Copilot AI review requested due to automatic review settings March 26, 2026 15:29
@azure-client-tools-bot-prd
Copy link
Copy Markdown

azure-client-tools-bot-prd bot commented Mar 26, 2026

️✔️AzureCLI-FullTest
️✔️acr
️✔️latest
️✔️3.12
️✔️3.13
️✔️acs
️✔️latest
️✔️3.12
️✔️3.13
️✔️advisor
️✔️latest
️✔️3.12
️✔️3.13
️✔️ams
️✔️latest
️✔️3.12
️✔️3.13
️✔️apim
️✔️latest
️✔️3.12
️✔️3.13
️✔️appconfig
️✔️latest
️✔️3.12
️✔️3.13
️✔️appservice
️✔️latest
️✔️3.12
️✔️3.13
️✔️aro
️✔️latest
️✔️3.12
️✔️3.13
️✔️backup
️✔️latest
️✔️3.12
️✔️3.13
️✔️batch
️✔️latest
️✔️3.12
️✔️3.13
️✔️batchai
️✔️latest
️✔️3.12
️✔️3.13
️✔️billing
️✔️latest
️✔️3.12
️✔️3.13
️✔️botservice
️✔️latest
️✔️3.12
️✔️3.13
️✔️cdn
️✔️latest
️✔️3.12
️✔️3.13
️✔️cloud
️✔️latest
️✔️3.12
️✔️3.13
️✔️cognitiveservices
️✔️latest
️✔️3.12
️✔️3.13
️✔️compute_recommender
️✔️latest
️✔️3.12
️✔️3.13
️✔️computefleet
️✔️latest
️✔️3.12
️✔️3.13
️✔️config
️✔️latest
️✔️3.12
️✔️3.13
️✔️configure
️✔️latest
️✔️3.12
️✔️3.13
️✔️consumption
️✔️latest
️✔️3.12
️✔️3.13
️✔️container
️✔️latest
️✔️3.12
️✔️3.13
️✔️containerapp
️✔️latest
️✔️3.12
️✔️3.13
️✔️core
️✔️latest
️✔️3.12
️✔️3.13
️✔️cosmosdb
️✔️latest
️✔️3.12
️✔️3.13
️✔️databoxedge
️✔️latest
️✔️3.12
️✔️3.13
️✔️dls
️✔️latest
️✔️3.12
️✔️3.13
️✔️dms
️✔️latest
️✔️3.12
️✔️3.13
️✔️eventgrid
️✔️latest
️✔️3.12
️✔️3.13
️✔️eventhubs
️✔️latest
️✔️3.12
️✔️3.13
️✔️feedback
️✔️latest
️✔️3.12
️✔️3.13
️✔️find
️✔️latest
️✔️3.12
️✔️3.13
️✔️hdinsight
️✔️latest
️✔️3.12
️✔️3.13
️✔️identity
️✔️latest
️✔️3.12
️✔️3.13
️✔️iot
️✔️latest
️✔️3.12
️✔️3.13
️✔️keyvault
️✔️latest
️✔️3.12
️✔️3.13
️✔️lab
️✔️latest
️✔️3.12
️✔️3.13
️✔️managedservices
️✔️latest
️✔️3.12
️✔️3.13
️✔️maps
️✔️latest
️✔️3.12
️✔️3.13
️✔️marketplaceordering
️✔️latest
️✔️3.12
️✔️3.13
️✔️monitor
️✔️latest
️✔️3.12
️✔️3.13
️✔️mysql
️✔️latest
️✔️3.12
️✔️3.13
️✔️netappfiles
️✔️latest
️✔️3.12
️✔️3.13
️✔️network
️✔️latest
️✔️3.12
️✔️3.13
️✔️policyinsights
️✔️latest
️✔️3.12
️✔️3.13
️✔️postgresql
️✔️latest
️✔️3.12
️✔️3.13
️✔️privatedns
️✔️latest
️✔️3.12
️✔️3.13
️✔️profile
️✔️latest
️✔️3.12
️✔️3.13
️✔️rdbms
️✔️latest
️✔️3.12
️✔️3.13
️✔️redis
️✔️latest
️✔️3.12
️✔️3.13
️✔️relay
️✔️latest
️✔️3.12
️✔️3.13
️✔️resource
️✔️latest
️✔️3.12
️✔️3.13
️✔️role
️✔️latest
️✔️3.12
️✔️3.13
️✔️search
️✔️latest
️✔️3.12
️✔️3.13
️✔️security
️✔️latest
️✔️3.12
️✔️3.13
️✔️servicebus
️✔️latest
️✔️3.12
️✔️3.13
️✔️serviceconnector
️✔️latest
️✔️3.12
️✔️3.13
️✔️servicefabric
️✔️latest
️✔️3.12
️✔️3.13
️✔️signalr
️✔️latest
️✔️3.12
️✔️3.13
️✔️sql
️✔️latest
️✔️3.12
️✔️3.13
️✔️sqlvm
️✔️latest
️✔️3.12
️✔️3.13
️✔️storage
️✔️latest
️✔️3.12
️✔️3.13
️✔️synapse
️✔️latest
️✔️3.12
️✔️3.13
️✔️telemetry
️✔️latest
️✔️3.12
️✔️3.13
️✔️util
️✔️latest
️✔️3.12
️✔️3.13
️✔️vm
️✔️latest
️✔️3.12
️✔️3.13

@azure-client-tools-bot-prd
Copy link
Copy Markdown

Hi @seligj95,
Since the current milestone time is less than 7 days, this pr will be reviewed in the next milestone.

@yonzhan
Copy link
Copy Markdown
Collaborator

yonzhan commented Mar 26, 2026

Thank you for your contribution! We will review the pull request and get back to you soon.

@azure-client-tools-bot-prd
Copy link
Copy Markdown

azure-client-tools-bot-prd bot commented Mar 26, 2026

️✔️AzureCLI-BreakingChangeTest
️✔️Non Breaking Changes

@github-actions
Copy link
Copy Markdown

The git hooks are available for azure-cli and azure-cli-extensions repos. They could help you run required checks before creating the PR.

Please sync the latest code with latest dev branch (for azure-cli) or main branch (for azure-cli-extensions).
After that please run the following commands to enable git hooks:

pip install azdev --upgrade
azdev setup -c <your azure-cli repo path> -r <your azure-cli-extensions repo path>

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR improves the reliability and shutdown behavior of App Service tunneling used by az webapp ssh / az webapp create-remote-connection, and enhances instance-targeting guidance in CLI help.

Changes:

  • Adds WebSocket reconnect (exponential backoff) plus periodic keepalive pings in the App Service tunnel server.
  • Introduces explicit tunnel cleanup via TunnelServer.close() and registers cleanup on Ctrl+C / termination paths.
  • Updates parameter help and command examples for --instance / --timeout, and adds unit tests for the new tunnel behaviors.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/azure-cli/azure/cli/command_modules/appservice/tunnel.py Adds retry/backoff WebSocket creation, keepalive pings, shutdown event, and close() cleanup.
src/azure-cli/azure/cli/command_modules/appservice/custom.py Registers signal/atexit cleanup and ensures tunnel close on KeyboardInterrupt via try/finally.
src/azure-cli/azure/cli/command_modules/appservice/_params.py Improves --timeout / --instance argument descriptions for tunnel-related commands.
src/azure-cli/azure/cli/command_modules/appservice/_help.py Adds examples demonstrating --instance usage for SSH and remote connection commands.
src/azure-cli/azure/cli/command_modules/appservice/tests/latest/test_webapp_commands_thru_mock.py Adds unit tests for retry, keepalive, close() idempotency, and signal cleanup registration.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@seligj95 seligj95 changed the title [App Service] Fix #8831, #13662, #13008: az webapp ssh: tunnel reliability and instance targeting [App Service] Fix SSH tunnel reliability, signal handling, and instance targeting Mar 26, 2026
… handler

- Replace time.sleep(delay) with self._closing.wait(delay) in retry
  backoff so shutdown wakes immediately when closing event is set
- Remove sys.exit(0) from signal handler; let close() set the event
  and the main loop exit naturally
- Remove unused 'import time' from tunnel.py
- Update tests to mock _closing.wait instead of time.sleep

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@yonzhan yonzhan assigned yanzhudd and unassigned zhoxing-ms Mar 26, 2026
@yanzhudd
Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 3 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

5 participants