Skip to content

Commit b9189aa

Browse files
committed
use backchannel for shutting down monitor
1 parent 9f3b819 commit b9189aa

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/exec/use_pty/monitor.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ pub(super) fn exec_monitor(
164164
// Disable nonblocking assetions as we will not poll the backchannel anymore.
165165
closure.backchannel.set_nonblocking_assertions(false);
166166

167-
std::thread::sleep(std::time::Duration::from_millis(10));
168167
match reason {
169168
StopReason::Break(err) => match err.try_into() {
170169
Ok(msg) => {
@@ -183,6 +182,14 @@ pub(super) fn exec_monitor(
183182
}
184183
}
185184

185+
// Wait for the parent to give us red light before shutting down. This avoids missing
186+
// output when the monitor exits too quickly.
187+
let event = retry_while_interrupted(|| backchannel.recv()).map_err(|err| {
188+
dev_warn!("cannot receive red light from parent: {err}");
189+
err
190+
})?;
191+
debug_assert_eq!(event, MonitorMessage::ExecCommand);
192+
186193
// FIXME (ogsudo): The tty is restored here if selinux is available.
187194

188195
// We call `_exit` instead of `exit` to avoid flushing the parent's IO streams by accident.

src/exec/use_pty/parent.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,10 +353,19 @@ impl ParentClosure {
353353
}
354354

355355
fn run(&mut self, registry: EventRegistry<Self>) -> io::Result<ExitReason> {
356-
match registry.event_loop(self) {
356+
let result = match registry.event_loop(self) {
357357
StopReason::Break(err) | StopReason::Exit(ParentExit::Backchannel(err)) => Err(err),
358358
StopReason::Exit(ParentExit::Command(exit_reason)) => Ok(exit_reason),
359-
}
359+
};
360+
// Send red light to the monitor after processing all events
361+
retry_while_interrupted(|| self.backchannel.send(&MonitorMessage::ExecCommand)).map_err(
362+
|err| {
363+
dev_error!("cannot send red light to monitor: {err}");
364+
err
365+
},
366+
)?;
367+
368+
result
360369
}
361370

362371
/// Read an event from the backchannel and return the event if it should break the event loop.

0 commit comments

Comments
 (0)