Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

terminal: Move PTY spawning to the background thread and retain exit status (#58004)

Anthony Eidb32570d931a3
diff --git a/Cargo.lock b/Cargo.lock
index 0cc384eef22..5f11b1956c8 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -597,15 +597,14 @@ dependencies = [
[[package]]
name = "alacritty_terminal"
-version = "0.25.1"
-source = "git+https://github.com/zed-industries/alacritty?rev=9d9640d4#9d9640d4e56d67a09d049f9c0a300aae08d4f61e"
+version = "0.26.1-dev"
+source = "git+https://github.com/zed-industries/alacritty?rev=fcf32feacb367b75ec84dd40f041e4fd411d3cc1#fcf32feacb367b75ec84dd40f041e4fd411d3cc1"
dependencies = [
"base64 0.22.1",
"bitflags 2.10.0",
"home",
"libc",
"log",
- "mach2 0.5.0",
"miow",
"parking_lot",
"piper",
@@ -17341,9 +17340,9 @@ dependencies = [
[[package]]
name = "signal-hook"
-version = "0.3.18"
+version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d881a16cf4426aa584979d30bd82cb33429027e42122b169753d6ef1085ed6e2"
+checksum = "b2a0c28ca5908dbdbcd52e6fdaa00358ab88637f8ab33e1f188dd510eb44b53d"
dependencies = [
"libc",
"signal-hook-registry",
diff --git a/Cargo.toml b/Cargo.toml
index 521128c90df..a3bd7b2e32f 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -511,7 +511,7 @@ accesskit_unix = "0.21.0"
accesskit_windows = "0.32.1"
agent-client-protocol = { version = "=0.12.1", features = ["unstable"] }
aho-corasick = "1.1"
-alacritty_terminal = { git = "https://github.com/zed-industries/alacritty", rev = "9d9640d4" }
+alacritty_terminal = { git = "https://github.com/zed-industries/alacritty", rev = "fcf32feacb367b75ec84dd40f041e4fd411d3cc1" }
any_vec = "0.14"
anyhow = "1.0.86"
ashpd = { version = "0.13", default-features = false, features = [
diff --git a/crates/terminal/src/terminal.rs b/crates/terminal/src/terminal.rs
index 271e2df0e9a..7d22a86d7db 100644
--- a/crates/terminal/src/terminal.rs
+++ b/crates/terminal/src/terminal.rs
@@ -453,6 +453,13 @@ impl TerminalBuilder {
) -> Task<Result<TerminalBuilder>> {
let version = release_channel::AppVersion::global(cx);
let background_executor = cx.background_executor().clone();
+ #[cfg(not(windows))]
+ let child_signal_mask = match tty::SignalMask::current()
+ .context("failed to capture terminal child signal mask")
+ {
+ Ok(signal_mask) => Some(signal_mask),
+ Err(error) => return Task::ready(Err(error)),
+ };
let fut = async move {
// Remove SHLVL so the spawned shell initializes it to 1, matching
// the behavior of standalone terminal emulators like iTerm2/Kitty/Alacritty.
@@ -542,6 +549,12 @@ impl TerminalBuilder {
working_directory: working_directory.clone(),
drain_on_exit: true,
env: env.clone().into_iter().collect(),
+ // We pass in the foreground thread's signal mask to the child process via pty_options,
+ // so terminal construction can run on a background thread without breaking Ctrl-C and other signals
+ // otherwise the terminal would inherit the background executor's signal mask which blocks
+ // some terminal signals
+ #[cfg(not(windows))]
+ child_signal_mask,
#[cfg(windows)]
escape_args: shell_kind.tty_escape_args(),
}
@@ -688,12 +701,7 @@ impl TerminalBuilder {
events_rx,
})
};
- // the thread we spawn things on has an effect on signal handling
- if !cfg!(target_os = "windows") {
- cx.spawn(async move |_| fut.await)
- } else {
- cx.background_spawn(fut)
- }
+ cx.background_spawn(fut)
}
pub fn subscribe(mut self, cx: &Context<Terminal>) -> Terminal {
@@ -985,7 +993,7 @@ impl Terminal {
AlacTermEvent::Bell => {
cx.emit(Event::Bell);
}
- AlacTermEvent::Exit => self.register_task_finished(Some(9), cx),
+ AlacTermEvent::Exit => self.register_task_finished(None, cx),
AlacTermEvent::MouseCursorDirty => {
//NOOP, Handled in render
}
@@ -1009,8 +1017,8 @@ impl Terminal {
.unwrap_or_else(|| to_alac_rgb(get_color_at_index(index, cx.theme().as_ref())));
self.write_to_pty(format(color).into_bytes());
}
- AlacTermEvent::ChildExit(raw_status) => {
- self.register_task_finished(Some(raw_status), cx);
+ AlacTermEvent::ChildExit(exit_status) => {
+ self.register_task_finished(Some(exit_status), cx);
}
}
}
@@ -2262,18 +2270,11 @@ impl Terminal {
Task::ready(None)
}
- fn register_task_finished(&mut self, raw_status: Option<i32>, cx: &mut Context<Terminal>) {
- let exit_status: Option<ExitStatus> = raw_status.map(|value| {
- #[cfg(unix)]
- {
- std::os::unix::process::ExitStatusExt::from_raw(value)
- }
- #[cfg(windows)]
- {
- std::os::windows::process::ExitStatusExt::from_raw(value as u32)
- }
- });
-
+ fn register_task_finished(
+ &mut self,
+ exit_status: Option<ExitStatus>,
+ cx: &mut Context<Terminal>,
+ ) {
if let Some(tx) = &self.completion_tx {
tx.try_send(exit_status).ok();
}