Skip to content

Commit a4c4bfe

Browse files
committed
fix(process-daemon): wait on events instead of 25 ms polls on Windows, so terminal echo stops lagging
1 parent bab0d04 commit a4c4bfe

2 files changed

Lines changed: 87 additions & 13 deletions

File tree

‎packages/process-daemon/src/service.rs‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1684,7 +1684,14 @@ fn receive_actor_message(
16841684
else {
16851685
return Ok(message);
16861686
};
1687-
let deadline = Instant::now() + OUTPUT_BATCH_DELAY;
1687+
// A deadline sleeps at least one timer tick on Windows (15.6 ms), which every echo waited for
1688+
// this 1 ms batch, so there the batch takes only what is already queued.
1689+
let deadline = Instant::now()
1690+
+ if cfg!(windows) {
1691+
Duration::ZERO
1692+
} else {
1693+
OUTPUT_BATCH_DELAY
1694+
};
16881695
while bytes.len() < OUTPUT_BATCH_BYTES {
16891696
match messages.recv_deadline(deadline) {
16901697
Ok(ActorMessage::Output {

‎packages/process-daemon/src/transport/windows.rs‎

Lines changed: 79 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use windows_sys::Win32::System::Pipes::{
2929
WaitNamedPipeW,
3030
};
3131
use windows_sys::Win32::System::Threading::{
32-
CreateEventW, INFINITE, SetEvent, WaitForMultipleObjects, WaitForSingleObject,
32+
CreateEventW, INFINITE, ResetEvent, SetEvent, WaitForMultipleObjects, WaitForSingleObject,
3333
};
3434

3535
pub(crate) mod security;
@@ -39,8 +39,6 @@ use security::PrivateSecurity;
3939
const BUFFER_BYTES: u32 = 64 * 1024;
4040
const CONNECT_TIMEOUT: Duration = Duration::from_secs(5);
4141
const COMPLETION_TIMEOUT: Duration = Duration::from_secs(2);
42-
/// Interim idle poll until `wait` blocks on the pending connect and a stop event.
43-
const POLL_INTERVAL: Duration = Duration::from_millis(25);
4442

4543
pub(crate) struct Listener {
4644
endpoint: Vec<u16>,
@@ -86,10 +84,25 @@ impl Listener {
8684
self.pending.operation.pipe.cancel();
8785
}
8886

89-
/// Windows keeps a 25 ms poll for now; the caller then polls `accept`.
90-
pub fn wait(&self, _wake: &Wake, listen: bool, timeout: Duration) -> io::Result<Readiness> {
91-
thread::sleep(timeout.min(POLL_INTERVAL));
92-
// `timed_out` stays false: Windows cannot tell, and the missed-wake detector must stay off.
87+
/// Sleeps until a client connects (the pending `ConnectNamedPipe`'s event), a wake, or
88+
/// `timeout`; the caller then polls `accept`. It slept 25 ms instead, and the server makes
89+
/// two connections per keystroke, so every echo in a terminal waited up to 50 ms for it.
90+
pub fn wait(&self, wake: &Wake, listen: bool, timeout: Duration) -> io::Result<Readiness> {
91+
let listening = listen && !self.stopped;
92+
if listening && self.pending.connected {
93+
return Ok(Readiness {
94+
connection: true,
95+
timed_out: false,
96+
});
97+
}
98+
let mut handles = vec![wake.event.as_raw_handle()];
99+
if listening {
100+
handles.push(self.pending.operation.event.as_raw_handle());
101+
}
102+
let milliseconds = timeout.as_millis().min(u128::from(INFINITE - 1)) as u32;
103+
// SAFETY: both events are owned by live objects for the whole call.
104+
unsafe { WaitForMultipleObjects(handles.len() as u32, handles.as_ptr(), 0, milliseconds) };
105+
// `timed_out` stays false: the missed-wake detector stays off, as before.
93106
Ok(Readiness {
94107
connection: listen,
95108
timed_out: false,
@@ -105,18 +118,31 @@ pub(crate) struct Readiness {
105118
pub timed_out: bool,
106119
}
107120

108-
/// Windows keeps a 25 ms poll for now, so notify is a no-op.
109-
pub(crate) struct Wake;
121+
/// The accept loop's wake: a manual-reset event `Listener::wait` sleeps on beside the pending
122+
/// connection.
123+
pub(crate) struct Wake {
124+
event: OwnedHandle,
125+
}
110126

111127
impl Wake {
112128
pub fn new() -> io::Result<Self> {
113-
Ok(Self)
129+
Ok(Self { event: event()? })
114130
}
115131

116-
pub fn notify(&self) {}
132+
pub fn notify(&self) {
133+
// SAFETY: the event is owned by self; signalling it is idempotent.
134+
unsafe { SetEvent(self.event.as_raw_handle()) };
135+
}
117136

137+
/// Whether a wake was pending, clearing it.
118138
pub fn drain(&self) -> bool {
119-
false
139+
// SAFETY: the event is owned by self; a zero timeout only queries it.
140+
let set = unsafe { WaitForSingleObject(self.event.as_raw_handle(), 0) } == WAIT_OBJECT_0;
141+
if set {
142+
// SAFETY: as above.
143+
unsafe { ResetEvent(self.event.as_raw_handle()) };
144+
}
145+
set
120146
}
121147
}
122148

@@ -616,6 +642,47 @@ mod tests {
616642
}
617643
}
618644

645+
#[test]
646+
fn wake_drain_reports_and_clears_a_notify() {
647+
let wake = Wake::new().unwrap();
648+
assert!(!wake.drain());
649+
for _ in 0..1_000 {
650+
wake.notify();
651+
}
652+
assert!(wake.drain());
653+
assert!(!wake.drain());
654+
}
655+
656+
#[test]
657+
fn listener_wait_ends_on_a_wake_or_a_connection_not_a_poll() {
658+
let endpoint = endpoint();
659+
let mut listener = Listener::bind(&endpoint).unwrap();
660+
let wake = Wake::new().unwrap();
661+
662+
// Nothing to report: it sleeps the whole timeout (it used to return after 25 ms).
663+
let started = Instant::now();
664+
listener
665+
.wait(&wake, true, Duration::from_millis(200))
666+
.unwrap();
667+
assert!(started.elapsed() >= Duration::from_millis(150));
668+
669+
wake.notify();
670+
let started = Instant::now();
671+
listener.wait(&wake, true, Duration::from_secs(5)).unwrap();
672+
assert!(started.elapsed() < Duration::from_secs(1));
673+
assert!(wake.drain());
674+
675+
let started = Instant::now();
676+
let connector = thread::spawn(move || {
677+
thread::sleep(Duration::from_millis(100));
678+
Connection::connect(&endpoint).unwrap()
679+
});
680+
let ready = listener.wait(&wake, true, Duration::from_secs(5)).unwrap();
681+
assert!(started.elapsed() < Duration::from_secs(1));
682+
assert!(ready.connection);
683+
let _client = connector.join().unwrap();
684+
assert!(listener.accept().unwrap().is_some());
685+
}
619686
#[test]
620687
fn multiple_byte_stream_connections_and_namespace_ownership() {
621688
let endpoint = endpoint();

0 commit comments

Comments
 (0)