Skip to content

Commit adf23e4

Browse files
authored
fix: spawn shell processes outside blocking workers (#4307)
1 parent bebdc66 commit adf23e4

1 file changed

Lines changed: 35 additions & 37 deletions

File tree

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{ffi::OsString, process::Stdio};
22

33
use anyhow::Result;
4-
use tokio::process::{Child, Command};
4+
use tokio::{process::{Child, Command}, task};
55
use yazi_fs::Cwd;
66
use yazi_macro::impl_data_any;
77
use yazi_shared::url::{AsUrl, UrlBuf};
@@ -30,42 +30,40 @@ impl ShellOpt {
3030
}
3131

3232
pub(crate) async fn shell(opt: ShellOpt) -> Result<Child> {
33-
tokio::task::spawn_blocking(move || {
34-
let cwd = Cwd::ensure(opt.cwd.as_url());
33+
let (cwd, opt) =
34+
task::spawn_blocking(move || (Cwd::ensure(opt.cwd.as_url()).into_owned(), opt)).await?;
3535

36-
#[cfg(unix)]
37-
return Ok(unsafe {
38-
Command::new("sh")
39-
.stdin(opt.stdio())
40-
.stdout(opt.stdio())
41-
.stderr(opt.stdio())
42-
.arg("-c")
43-
.arg(opt.cmd)
44-
.current_dir(cwd)
45-
.kill_on_drop(!opt.orphan)
46-
.pre_exec(move || {
47-
if !opt.block && libc::setsid() < 0 {
48-
return Err(std::io::Error::last_os_error());
49-
}
50-
Ok(())
51-
})
52-
.spawn()?
53-
});
36+
#[cfg(unix)]
37+
return Ok(unsafe {
38+
Command::new("sh")
39+
.stdin(opt.stdio())
40+
.stdout(opt.stdio())
41+
.stderr(opt.stdio())
42+
.arg("-c")
43+
.arg(opt.cmd)
44+
.current_dir(cwd)
45+
.kill_on_drop(!opt.orphan)
46+
.pre_exec(move || {
47+
if !opt.block && libc::setsid() < 0 {
48+
return Err(std::io::Error::last_os_error());
49+
}
50+
Ok(())
51+
})
52+
.spawn()?
53+
});
5454

55-
#[cfg(windows)]
56-
return Ok(
57-
Command::new("cmd.exe")
58-
.stdin(opt.stdio())
59-
.stdout(opt.stdio())
60-
.stderr(opt.stdio())
61-
.env("=", r#""^\n\n""#)
62-
.raw_arg(r#"/Q /S /D /V:OFF /E:ON /C ""#)
63-
.raw_arg(opt.cmd)
64-
.raw_arg(r#"""#)
65-
.current_dir(cwd)
66-
.kill_on_drop(!opt.orphan)
67-
.spawn()?,
68-
);
69-
})
70-
.await?
55+
#[cfg(windows)]
56+
return Ok(
57+
Command::new("cmd.exe")
58+
.stdin(opt.stdio())
59+
.stdout(opt.stdio())
60+
.stderr(opt.stdio())
61+
.env("=", r#""^\n\n""#)
62+
.raw_arg(r#"/Q /S /D /V:OFF /E:ON /C ""#)
63+
.raw_arg(opt.cmd)
64+
.raw_arg(r#"""#)
65+
.current_dir(cwd)
66+
.kill_on_drop(!opt.orphan)
67+
.spawn()?,
68+
);
7169
}

0 commit comments

Comments
 (0)