From ca9ecdfd10215eb02cca51eb8ed8aeaa70718d8d Mon Sep 17 00:00:00 2001 From: Jiyong Park Date: Sat, 26 Jul 2025 09:05:00 +0900 Subject: [PATCH] wait-for-disconnect and then wait-for-device Previously, right after we do `adb reboot`, we immediately executed `adb wait-for-device`. This is problematic because the device may still be in the process of shutting down (shutdown itself is taking some time), in which case `adb wait-for-device` would immediately return, giving a false signal that the reboot was finished. Fixing this issue by firstly waiting for the adb connection to be lost (via adb wait-for-disconnect) and when executing adb-wait-for-device. Note that other test hardness like tradefed already has had this. ex: https://android.googlesource.com/platform/tools/tradefederation/+/refs/heads/main/src/com/android/tradefed/device/NativeDevice.java#4023 Test: tox passed --- mobly/controllers/android_device.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/mobly/controllers/android_device.py b/mobly/controllers/android_device.py index eea6d48f..d35a63da 100644 --- a/mobly/controllers/android_device.py +++ b/mobly/controllers/android_device.py @@ -76,6 +76,10 @@ # Default name for bug reports taken without a specified test name. DEFAULT_BUG_REPORT_NAME = 'bugreport' +# Default timeout to wait for device to go offline upon reboot. +# This number is from max_clean_shutdown_timeout system/core/init/reboot.cpp +DEFAULT_TIMEOUT_DISCONNECT_SECOND = 10 + # Default Timeout to wait for boot completion DEFAULT_TIMEOUT_BOOT_COMPLETION_SECOND = 15 * 60 @@ -728,6 +732,13 @@ def handle_reboot(self): try: yield finally: + # If we execute `wait-for-device` right after `reboot`, adbd in the device + # may still be running, which would cause `wait-for-device` to return + # immediately, even if the device hasn't finished rebooting. + # To avoid this situation, wait until the adb connection is actually + # lost, and then wait for the boot complete signal. + self.adb.wait_for_disconnect(timeout=DEFAULT_TIMEOUT_DISCONNECT_SECOND) + self.wait_for_boot_completion() # On boot completion, invalidate the `build_info` cache since any # value it had from before boot completion is potentially invalid.