Skip to content

Commit 159548a

Browse files
committed
use backchannel for shutting down monitor
1 parent cc27462 commit 159548a

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

171-
std::thread::sleep(std::time::Duration::from_millis(10));
172171
match reason {
173172
StopReason::Break(err) => match err.try_into() {
174173
Ok(msg) => {
@@ -187,6 +186,14 @@ pub(super) fn exec_monitor(
187186
}
188187
}
189188

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

192199
// 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
@@ -349,10 +349,19 @@ impl ParentClosure {
349349
}
350350

351351
fn run(&mut self, registry: EventRegistry<Self>) -> io::Result<ExitReason> {
352-
match registry.event_loop(self) {
352+
let result = match registry.event_loop(self) {
353353
StopReason::Break(err) | StopReason::Exit(ParentExit::Backchannel(err)) => Err(err),
354354
StopReason::Exit(ParentExit::Command(exit_reason)) => Ok(exit_reason),
355-
}
355+
};
356+
// Send red light to the monitor after processing all events
357+
retry_while_interrupted(|| self.backchannel.send(&MonitorMessage::ExecCommand)).map_err(
358+
|err| {
359+
dev_error!("cannot send red light to monitor: {err}");
360+
err
361+
},
362+
)?;
363+
364+
result
356365
}
357366

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

0 commit comments

Comments
 (0)