Skip to content

Commit e7cba59

Browse files
bnoordhuisThomasdezeeuw
authored andcommitted
Work around 32 bits epoll_wait() bug (#1349)
A bug in kernels < 2.6.37 makes timeouts larger than LONG_MAX / CONFIG_HZ (approx. 30 minutes with CONFIG_HZ=1200) effectively infinite on 32 bits architectures. The magic number is the same constant used by libuv, see commit libuv/libuv@d1b5008e76.
1 parent 4879e0d commit e7cba59

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/sys/unix/epoll.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,16 @@ impl Selector {
6262

6363
/// Wait for events from the OS
6464
pub fn select(&self, evts: &mut Events, awakener: Token, timeout: Option<Duration>) -> io::Result<bool> {
65+
// A bug in kernels < 2.6.37 makes timeouts larger than LONG_MAX / CONFIG_HZ
66+
// (approx. 30 minutes with CONFIG_HZ=1200) effectively infinite on 32 bits
67+
// architectures. The magic number is the same constant used by libuv.
68+
#[cfg(target_pointer_width = "32")]
69+
const MAX_SAFE_TIMEOUT: u64 = 1789569;
70+
#[cfg(not(target_pointer_width = "32"))]
71+
const MAX_SAFE_TIMEOUT: u64 = c_int::max_value() as u64;
72+
6573
let timeout_ms = timeout
66-
.map(|to| cmp::min(millis(to), i32::MAX as u64) as i32)
74+
.map(|to| cmp::min(millis(to), MAX_SAFE_TIMEOUT) as c_int)
6775
.unwrap_or(-1);
6876

6977
// Wait for epoll events for at most timeout_ms milliseconds

0 commit comments

Comments
 (0)