fix(cli): add timeout on interprocess communication (#5090)

This commit is contained in:
Lucas Fernandes Nogueira 2022-08-29 14:46:37 -03:00 committed by GitHub
parent b36ccb7e99
commit e22d21beaf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -179,14 +179,24 @@ fn read_options(config: &TauriConfig, target: Target) -> CliOptions {
.set_nonblocking(true)
.expect("failed to set local socket stream to nonblocking");
let mut conn = BufReader::new(conn);
let mut attempt = 0;
let max_tries = 5;
let buffer = loop {
let mut buffer = String::new();
conn.read_line(&mut buffer).unwrap_or_else(|_| {
if conn.read_line(&mut buffer).is_ok() {
break buffer;
}
std::thread::sleep(std::time::Duration::from_secs(1));
attempt += 1;
if attempt == max_tries {
log::error!(
"failed to connect to local socket. You must keep the Tauri CLI alive with the `{cmd} dev` or `{cmd} build --open` commands.",
cmd = target.command_name()
);
std::process::exit(1);
});
}
};
let options: CliOptions = serde_json::from_str(&buffer).expect("invalid CLI options");
for (k, v) in &options.vars {
set_var(k, v);