Skip to content

Improve adb port forwarding teardown logic in SnippetClientV2 #971

@devk-op

Description

@devk-op

The _stop_port_forwarding() method in mobly.controllers.android_device_lib.snippet_client_v2.SnippetClientV2 currently attempts to remove adb port forwarding without verifying whether the port is still in use.

This can lead to noisy errors or misleading logs when the port is already released (or was never forwarded).

Proposal

Modify _stop_port_forwarding() to:

  • Check adb.list_occupied_adb_ports() before attempting removal.
  • Skip the --remove call if the port is not found.
  • Ensure proper debug logging and safe cleanup via finally.

Additional Improvements

  • Minor readability enhancements (docstrings, logging).
  • Related unit tests updated to match logic changes.
  • All tests pass via tox, and code is linted via pyink==24.3.0.

Rationale

This change improves robustness, avoids unnecessary adb commands, and aligns with the defensive coding pattern Mobly uses elsewhere (e.g., list_occupied_adb_ports()'s intended usage).

I’d like to contribute this improvement. Please let me know if you'd like it broken into smaller parts or if there's existing related work I should refer to.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions