Round up when converting timeout to ms for epoll_wait()

This commit is contained in:
yump
2022-09-03 09:34:30 -05:00
committed by GitHub
parent 0b344747e7
commit 436aef4b26
+13 -1
View File
@@ -87,7 +87,19 @@ impl Selector {
const MAX_SAFE_TIMEOUT: u128 = libc::c_int::max_value() as u128; const MAX_SAFE_TIMEOUT: u128 = libc::c_int::max_value() as u128;
let timeout = timeout let timeout = timeout
.map(|to| cmp::min(to.as_millis(), MAX_SAFE_TIMEOUT) as libc::c_int) .map(|to| {
let to_ms = to.as_millis();
// as_millis() truncates, so round up to 1 ms as the documentation says can happen.
// This avoids turning submillisecond timeouts into immediate returns unless the
// caller explicitly requests that by specifying a zero timeout.
let to_ms = to_ms
+ if to_ms == 0 && to.subsec_nanos() != 0 {
1
} else {
0
};
cmp::min(MAX_SAFE_TIMEOUT, to_ms) as libc::c_int
})
.unwrap_or(-1); .unwrap_or(-1);
events.clear(); events.clear();