Skip to content

Commit f34c00a

Browse files
committed
Fix blocking call from async code
Signed-off-by: Didier Wenzek <didier.wenzek@free.fr>
1 parent e5e1f94 commit f34c00a

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

crates/core/tedge/src/cli/certificate/c8y/download.rs

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl Command for DownloadCertCmd {
7373

7474
impl DownloadCertCmd {
7575
async fn download_device_certificate(&self) -> Result<(), Error> {
76-
let (common_name, security_token) = self.get_registration_data()?;
76+
let (common_name, security_token) = self.get_registration_data().await?;
7777
if self.generate_csr {
7878
create_device_csr(
7979
common_name.clone(),
@@ -129,25 +129,30 @@ impl DownloadCertCmd {
129129
/// Prompt the user for the device id and the security token
130130
///
131131
/// - unless already set on the command line or using env variables.
132-
fn get_registration_data(&self) -> Result<(String, String), std::io::Error> {
133-
let device_id = if self.device_id.is_empty() {
134-
print!("Enter device id: ");
135-
std::io::stdout().flush()?;
136-
let mut input = String::new();
137-
std::io::stdin().read_line(&mut input)?;
138-
input.trim_end_matches(['\n', '\r']).to_string()
139-
} else {
140-
self.device_id.clone()
141-
};
142-
143-
// Read the security token from /dev/tty
144-
let security_token = if self.security_token.is_empty() {
145-
rpassword::read_password_from_tty(Some("Enter security token: "))?
146-
} else {
147-
self.security_token.clone()
148-
};
149-
150-
Ok((device_id, security_token))
132+
async fn get_registration_data(&self) -> Result<(String, String), std::io::Error> {
133+
let self_device_id = self.device_id.clone();
134+
let self_security_token = self.security_token.clone();
135+
tokio::task::spawn_blocking(move || {
136+
let device_id = if self_device_id.is_empty() {
137+
print!("Enter device id: ");
138+
std::io::stdout().flush()?;
139+
let mut input = String::new();
140+
std::io::stdin().read_line(&mut input)?;
141+
input.trim_end_matches(['\n', '\r']).to_string()
142+
} else {
143+
self_device_id
144+
};
145+
146+
// Read the security token from /dev/tty
147+
let security_token = if self_security_token.is_empty() {
148+
rpassword::read_password_from_tty(Some("Enter security token: "))?
149+
} else {
150+
self_security_token
151+
};
152+
153+
Ok((device_id, security_token))
154+
})
155+
.await?
151156
}
152157

153158
/// Post the device CSR

0 commit comments

Comments
 (0)