Skip to content

Commit 3674bc2

Browse files
fix: accumulate rmw_wait timeout in int64 so RMW_DURATION_INFINITE keeps blocking (#8)
1 parent 3de7b77 commit 3674bc2

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

‎rmw_unix_socket_cpp/src/rmw_wait.cpp‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <chrono>
77
#include <cstring>
8+
#include <limits>
89

910
#include <sys/epoll.h>
1011
#include <sys/eventfd.h>
@@ -284,13 +285,19 @@ rmw_ret_t rmw_wait(
284285
}
285286
}
286287

287-
// Compute timeout
288+
// Compute timeout. -1 means block forever (epoll_wait sentinel).
288289
int timeout_ms = -1;
289290
if (wait_timeout) {
290-
timeout_ms = static_cast<int>(
291-
wait_timeout->sec * 1000 + wait_timeout->nsec / 1000000);
292-
if (timeout_ms == 0 && wait_timeout->nsec > 0) {
293-
timeout_ms = 1; // At least 1ms
291+
// Accumulate in int64_t; RMW_DURATION_INFINITE (~9.2e12 ms) overflows int.
292+
int64_t ms = static_cast<int64_t>(wait_timeout->sec) * 1000 +
293+
static_cast<int64_t>(wait_timeout->nsec) / 1000000;
294+
if (ms > std::numeric_limits<int>::max()) {
295+
timeout_ms = -1; // Infinite (or beyond epoll's range) -> block forever
296+
} else {
297+
timeout_ms = static_cast<int>(ms);
298+
if (timeout_ms == 0 && wait_timeout->nsec > 0) {
299+
timeout_ms = 1; // At least 1ms
300+
}
294301
}
295302
}
296303

0 commit comments

Comments
 (0)