Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
repl: Shutdown all kernels on app quit (#48760)
Kyle Kelley29f2048f8887
diff --git a/crates/repl/src/kernels/mod.rs b/crates/repl/src/kernels/mod.rsindex 250628e4f3a..87ed6ed398f 100644--- a/crates/repl/src/kernels/mod.rs+++ b/crates/repl/src/kernels/mod.rs@@ -170,6 +170,7 @@ pub trait RunningKernel: Send + Debug {fn kernel_info(&self) -> Option<&KernelInfoReply>;fn set_kernel_info(&mut self, info: KernelInfoReply);fn force_shutdown(&mut self, window: &mut Window, cx: &mut App) -> Task<anyhow::Result<()>>;+ fn kill(&mut self);}#[derive(Debug, Clone)]diff --git a/crates/repl/src/kernels/native_kernel.rs b/crates/repl/src/kernels/native_kernel.rsindex e5eb32d3499..c3fd57557a2 100644--- a/crates/repl/src/kernels/native_kernel.rs+++ b/crates/repl/src/kernels/native_kernel.rs@@ -370,17 +370,21 @@ impl RunningKernel for NativeRunningKernel {}fn force_shutdown(&mut self, _window: &mut Window, _cx: &mut App) -> Task<anyhow::Result<()>> {+ self.kill();+ Task::ready(Ok(()))+ }++ fn kill(&mut self) {self._process_status_task.take();self.request_tx.close_channel();- Task::ready(self.process.kill().context("killing the kernel process"))+ self.process.kill().ok();}}impl Drop for NativeRunningKernel {fn drop(&mut self) {std::fs::remove_file(&self.connection_path).ok();- self.request_tx.close_channel();- self.process.kill().ok();+ self.kill();}}diff --git a/crates/repl/src/kernels/remote_kernels.rs b/crates/repl/src/kernels/remote_kernels.rsindex 0be657bb8d3..165ca387d0e 100644--- a/crates/repl/src/kernels/remote_kernels.rs+++ b/crates/repl/src/kernels/remote_kernels.rs@@ -289,4 +289,8 @@ impl RunningKernel for RemoteRunningKernel {Ok(())})}++ fn kill(&mut self) {+ self.request_tx.close_channel();+ }}diff --git a/crates/repl/src/repl_store.rs b/crates/repl/src/repl_store.rsindex 9d40f059b74..abff0bdc3aa 100644--- a/crates/repl/src/repl_store.rs+++ b/crates/repl/src/repl_store.rs@@ -1,3 +1,4 @@+use std::future::Future;use std::sync::Arc;use anyhow::{Context as _, Result};@@ -10,7 +11,7 @@ use project::{Fs, Project, WorktreeId};use settings::{Settings, SettingsStore};use crate::kernels::{- list_remote_kernelspecs, local_kernel_specifications, python_env_kernel_specifications,+ Kernel, list_remote_kernelspecs, local_kernel_specifications, python_env_kernel_specifications,};use crate::{JupyterSettings, KernelSpecification, Session};@@ -47,9 +48,12 @@ impl ReplStore {}pub fn new(fs: Arc<dyn Fs>, cx: &mut Context<Self>) -> Self {- let subscriptions = vec![cx.observe_global::<SettingsStore>(move |this, cx| {- this.set_enabled(JupyterSettings::enabled(cx), cx);- })];+ let subscriptions = vec![+ cx.observe_global::<SettingsStore>(move |this, cx| {+ this.set_enabled(JupyterSettings::enabled(cx), cx);+ }),+ cx.on_app_quit(Self::shutdown_all_sessions),+ ];let this = Self {fs,@@ -281,6 +285,23 @@ impl ReplStore {self.sessions.remove(&entity_id);}+ fn shutdown_all_sessions(+ &mut self,+ cx: &mut Context<Self>,+ ) -> impl Future<Output = ()> + use<> {+ for session in self.sessions.values() {+ session.update(cx, |session, _cx| {+ if let Kernel::RunningKernel(mut kernel) =+ std::mem::replace(&mut session.kernel, Kernel::Shutdown)+ {+ kernel.kill();+ }+ });+ }+ self.sessions.clear();+ futures::future::ready(())+ }+#[cfg(test)]pub fn set_kernel_specs_for_testing(&mut self,