Skip to content

fix(core): Command::output suspend while wait for response#8539

Merged
lucasfernog merged 3 commits intotauri-apps:1.xfrom
dyxushuai:fix/command_output_suspend
Feb 1, 2024
Merged

fix(core): Command::output suspend while wait for response#8539
lucasfernog merged 3 commits intotauri-apps:1.xfrom
dyxushuai:fix/command_output_suspend

Conversation

@dyxushuai
Copy link
Contributor

@dyxushuai dyxushuai commented Jan 4, 2024

spawn_pipe_reader(
tx.clone(),
guard.clone(),
stdout_reader,
CommandEvent::Stdout,
encoding,
);
spawn_pipe_reader(
tx.clone(),
guard.clone(),
stderr_reader,
CommandEvent::Stderr,
encoding,
);

These two methods will hold the tx(Sender) and never drop them because the method spawn_pipe_reader will be lost in the loop and unable to get out without break;

loop {
buf.clear();
match tauri_utils::io::read_line(&mut reader, &mut buf) {
Ok(n) => {
if n == 0 {
break;
}
let tx_ = tx.clone();
let line = match character_encoding {
Some(encoding) => Ok(encoding.decode_with_bom_removal(&buf).0.into()),
None => String::from_utf8(buf.clone()),
};
block_on_task(async move {
let _ = match line {
Ok(line) => tx_.send(wrapper(line)).await,
Err(e) => tx_.send(CommandEvent::Error(e.to_string())).await,
};
});
}
Err(e) => {
let tx_ = tx.clone();
let _ = block_on_task(async move { tx_.send(CommandEvent::Error(e.to_string())).await });
}
}
}

So, the loop in output method will never receive a None value from Reciver.

while let Some(event) = rx.recv().await {
match event {
CommandEvent::Terminated(payload) => {
code = payload.code;
}
CommandEvent::Stdout(line) => {
stdout.push_str(line.as_str());
stdout.push('\n');
}
CommandEvent::Stderr(line) => {
stderr.push_str(line.as_str());
stderr.push('\n');
}
CommandEvent::Error(_) => {}
}
}

@dyxushuai
Copy link
Contributor Author

Maybe resolves #8572

@amrbashir amrbashir requested a review from lucasfernog January 16, 2024 01:23
lucasfernog added a commit to tauri-apps/plugins-workspace that referenced this pull request Jan 31, 2024
@lucasfernog lucasfernog merged commit cc3d8e7 into tauri-apps:1.x Feb 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants