Skip to content

Commit 181e1d7

Browse files
fix(nr_large): use spawn_blocking also around bash script execution (#8595)
Similarly to #8547, this PR spawns the execution of the bash script itself as a blocking task since it also uses blocking I/O. Unfortunately, I could not simply write ```rust tokio::task::spawn_blocking(move || self.block_on_bash_script_from_session(&session, &script)) .await .expect("Executing bash script task panicked") ``` because I also had to clone `self`, which is not `Clone`. Removing the `self` parameter from `block_on_bash_script_from_session` (which is not used) would not be practical either as we would have to adapt every call site.
1 parent a0c9133 commit 181e1d7

1 file changed

Lines changed: 26 additions & 19 deletions

File tree

rs/tests/driver/src/driver/test_env_api.rs

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,6 +1491,26 @@ pub fn load_wasm<P: AsRef<Path>>(p: P) -> Vec<u8> {
14911491
wasm_bytes
14921492
}
14931493

1494+
fn execute_bash_script_from_session(session: &Session, script: &str) -> Result<String> {
1495+
let mut channel = session.channel_session()?;
1496+
channel.exec("bash").map_err(|e| {
1497+
anyhow::anyhow!("Failed to execute bash on SSH channel: {:?}. This may occur during system reboot or network instability.", e)
1498+
})?;
1499+
1500+
channel.write_all(script.as_bytes())?;
1501+
channel.flush()?;
1502+
channel.send_eof()?;
1503+
let mut out = String::new();
1504+
channel.read_to_string(&mut out)?;
1505+
let mut err = String::new();
1506+
channel.stderr().read_to_string(&mut err)?;
1507+
let exit_status = channel.exit_status()?;
1508+
if exit_status != 0 {
1509+
bail!("block_on_bash_script: exit_status = {exit_status:?}. Output: {out} Err: {err}");
1510+
}
1511+
Ok(out)
1512+
}
1513+
14941514
#[async_trait]
14951515
pub trait SshSession: HasTestEnv {
14961516
/// Return the address of the SSH server to connect to.
@@ -1550,32 +1570,19 @@ pub trait SshSession: HasTestEnv {
15501570

15511571
fn block_on_bash_script(&self, script: &str) -> Result<String> {
15521572
let session = self.block_on_ssh_session()?;
1553-
self.block_on_bash_script_from_session(&session, script)
1573+
execute_bash_script_from_session(&session, script)
15541574
}
15551575

15561576
async fn block_on_bash_script_async(&self, script: &str) -> Result<String> {
15571577
let session = self.block_on_ssh_session_async().await?;
1558-
self.block_on_bash_script_from_session(&session, script)
1578+
let script = script.to_string();
1579+
tokio::task::spawn_blocking(move || execute_bash_script_from_session(&session, &script))
1580+
.await
1581+
.expect("Executing bash script task panicked")
15591582
}
15601583

15611584
fn block_on_bash_script_from_session(&self, session: &Session, script: &str) -> Result<String> {
1562-
let mut channel = session.channel_session()?;
1563-
channel.exec("bash").map_err(|e| {
1564-
anyhow::anyhow!("Failed to execute bash on SSH channel: {:?}. This may occur during system reboot or network instability.", e)
1565-
})?;
1566-
1567-
channel.write_all(script.as_bytes())?;
1568-
channel.flush()?;
1569-
channel.send_eof()?;
1570-
let mut out = String::new();
1571-
channel.read_to_string(&mut out)?;
1572-
let mut err = String::new();
1573-
channel.stderr().read_to_string(&mut err)?;
1574-
let exit_status = channel.exit_status()?;
1575-
if exit_status != 0 {
1576-
bail!("block_on_bash_script: exit_status = {exit_status:?}. Output: {out} Err: {err}");
1577-
}
1578-
Ok(out)
1585+
execute_bash_script_from_session(session, script)
15791586
}
15801587

15811588
/// Is it accessible via ssh with the `admin` user.

0 commit comments

Comments
 (0)