Skip to content

Commit 423280f

Browse files
committed
use backchannel for shutting down monitor
1 parent 88dd244 commit 423280f

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
@@ -152,7 +152,6 @@ pub(super) fn exec_monitor(
152152
// Disable nonblocking assetions as we will not poll the backchannel anymore.
153153
closure.backchannel.set_nonblocking_assertions(false);
154154

155-
std::thread::sleep(std::time::Duration::from_millis(10));
156155
match reason {
157156
StopReason::Break(err) => match err.try_into() {
158157
Ok(msg) => {
@@ -171,6 +170,14 @@ pub(super) fn exec_monitor(
171170
}
172171
}
173172

173+
// Wait for the parent to give us red light before shutting down. This avoids missing
174+
// output when the monitor exits too quickly.
175+
let event = retry_while_interrupted(|| backchannel.recv()).map_err(|err| {
176+
dev_warn!("cannot receive red light from parent: {err}");
177+
err
178+
})?;
179+
debug_assert_eq!(event, MonitorMessage::ExecCommand);
180+
174181
// FIXME (ogsudo): The tty is restored here if selinux is available.
175182

176183
// 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
@@ -344,10 +344,19 @@ impl ParentClosure {
344344
}
345345

346346
fn run(&mut self, registry: EventRegistry<Self>) -> io::Result<ExitReason> {
347-
match registry.event_loop(self) {
347+
let result = match registry.event_loop(self) {
348348
StopReason::Break(err) | StopReason::Exit(ParentExit::Backchannel(err)) => Err(err),
349349
StopReason::Exit(ParentExit::Command(exit_reason)) => Ok(exit_reason),
350-
}
350+
};
351+
// Send red light to the monitor after processing all events
352+
retry_while_interrupted(|| self.backchannel.send(&MonitorMessage::ExecCommand)).map_err(
353+
|err| {
354+
dev_error!("cannot send red light to monitor: {err}");
355+
err
356+
},
357+
)?;
358+
359+
result
351360
}
352361

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

0 commit comments

Comments
 (0)