Skip to content

Commit eba8283

Browse files
authored
fix(android): prevent adb::device_list deadlock due to shell stuck (#493)
* fix(android): prevent adb::device_list deadlock due to shell stuck when an emulator gets in a weird state and disconnects from adb, `adb shell` gets stuck and never resolves; we're now adding a timeout to prevent the CLI from appear in a deadlock state, and return the error instead * import
1 parent 33428c9 commit eba8283

File tree

6 files changed

+164
-26
lines changed

6 files changed

+164
-26
lines changed

.changes/avoid-shell-stuck.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"cargo-mobile2": patch
3+
---
4+
5+
Prevent adb device list from getting stuck if any of the available devices are not properly connected.

Cargo.lock

Lines changed: 128 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ structopt = { version = "0.3", optional = true }
7575
textwrap = { version = "0.16", features = ["terminal_size"] }
7676
thiserror = "2.0"
7777
toml = { version = "0.9", features = ["preserve_order"] }
78-
duct = "1"
78+
duct = "1.1"
7979
which = "8"
8080
os_pipe = "1"
8181

src/android/adb/device_name.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::time::Duration;
2+
13
use super::adb;
24
use crate::{
35
android::env::Env,
@@ -51,7 +53,13 @@ pub fn device_name(env: &Env, serial_no: &str) -> Result<String, Error> {
5153
command: format!("{cmd:?}"),
5254
error,
5355
})?
54-
.wait()
56+
.wait_timeout(Duration::from_secs(3))
57+
.and_then(|output| {
58+
output.ok_or(std::io::Error::new(
59+
std::io::ErrorKind::TimedOut,
60+
"adb emu avd name timed out",
61+
))
62+
})
5563
.map_err(|error| Error::CommandFailed {
5664
command: format!("{cmd:?}"),
5765
error,

src/android/adb/get_prop.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::{
22
android::env::Env,
33
util::cli::{Report, Reportable},
44
};
5-
use std::str;
5+
use std::{str, time::Duration};
66
use thiserror::Error;
77

88
use super::adb;
@@ -54,10 +54,18 @@ pub fn get_prop(env: &Env, serial_no: &str, prop: &str) -> Result<String, Error>
5454
error,
5555
})?;
5656

57-
let output = handle.wait().map_err(|error| Error::CommandFailed {
58-
command: format!("{cmd:?}"),
59-
error,
60-
})?;
57+
let output = handle
58+
.wait_timeout(Duration::from_secs(3))
59+
.and_then(|output| {
60+
output.ok_or(std::io::Error::new(
61+
std::io::ErrorKind::TimedOut,
62+
"adb shell getprop timed out",
63+
))
64+
})
65+
.map_err(|error| Error::CommandFailed {
66+
command: format!("{cmd:?}"),
67+
error,
68+
})?;
6169
super::check_authorized(output).map_err(|source| Error::LookupFailed {
6270
prop: prop.to_owned(),
6371
source,

0 commit comments

Comments
 (0)