diff --git a/crates/ide-dap/src/session.rs b/crates/ide-dap/src/session.rs index 6bf2a2e40d..9a4fa04c86 100644 --- a/crates/ide-dap/src/session.rs +++ b/crates/ide-dap/src/session.rs @@ -474,10 +474,7 @@ impl Session { let _ = c.start_kill(); } if let Some(mut program) = self.program.lock().take() { - let _ = program.child.kill(); - let _ = std::thread::Builder::new().name("openidea-debug-reap".into()).spawn(move || { - let _ = program.child.wait(); - }); + program.kill(); } } } @@ -574,7 +571,7 @@ fn launch_groups(s: &Session) -> Vec { for child in s.debuggees.lock().iter() { push(child.id()); } - push(s.program.lock().as_ref().and_then(|p| p.child.process_id())); + push(s.program.lock().as_ref().and_then(ProgramTerminal::process_id)); groups.retain(|g| *g > 0); groups } @@ -874,7 +871,10 @@ fn on_reverse_request(mgr: &Arc, s: &Arc, seq: i64, comma /// A `runInTerminal` program and the pseudo-terminal it runs in. pub(crate) struct ProgramTerminal { writer: ide_core::input::InputWriter, - child: Box, + killer: Box, + pid: Option, + /// Set once the program has exited and been reaped. + exited: Arc, _master: Box, } @@ -882,6 +882,28 @@ impl ProgramTerminal { pub(crate) fn write(&mut self, text: &str) -> std::io::Result<()> { self.writer.write(text.as_bytes()) } + + /// The program's process id while it runs. + pub(crate) fn process_id(&self) -> Option { + if self.exited.load(Ordering::Acquire) { None } else { self.pid } + } + + pub(crate) fn kill(&mut self) { + // After the reap the pid may belong to another process. + if !self.exited.load(Ordering::Acquire) { + let _ = self.killer.kill(); + } + } +} + +/// Reaps the program as soon as it exits. Until then it stays a zombie that `kill(pid, 0)` still +/// finds: js-debug polls the `processId` it was given and keeps the session (and its terminal) +/// open while that process looks alive, and Stop would wait for it as for a running program. +fn reap_on_exit(mut child: Box, exited: Arc) { + let _ = std::thread::Builder::new().name("openidea-debug-reap".into()).spawn(move || { + let _ = child.wait(); + exited.store(true, Ordering::Release); + }); } /// `runInTerminal`: starts the debuggee on a pseudo-terminal, as an IDE terminal would (CodeLLDB @@ -968,8 +990,12 @@ pub(crate) fn run_in_terminal(s: &Arc, args: &Value) -> Result = { type: "pwa-node", - console: "internalConsole", - outputCapture: "std", + console: "integratedTerminal", skipFiles: workflowSettings().debugger.skipLibraryCode ? ["/**", "**/node_modules/**"] : [], autoAttachChildProcesses: true, };