From a4b737213eb1d8f352f1f148c27e96a3f09f5d08 Mon Sep 17 00:00:00 2001 From: Koichi Sasada Date: Sat, 23 Dec 2023 05:23:53 +0900 Subject: [PATCH] MN: ceil timeout milli seconds `hrrel / RB_HRTIME_PER_MSEC` floor the timeout value and it can return wrong value by `Mutex#sleep` (return Integer even if it should return nil (timeout'ed)). This patch ceil the value and the issue was solved. --- thread_pthread.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/thread_pthread.c b/thread_pthread.c index d8ca403be6b037..81a90d194e3cde 100644 --- a/thread_pthread.c +++ b/thread_pthread.c @@ -2872,7 +2872,7 @@ timer_thread_set_timeout(rb_vm_t *vm) RUBY_DEBUG_LOG("th:%u now:%lu rel:%lu", rb_th_serial(th), (unsigned long)now, (unsigned long)hrrel); // TODO: overflow? - timeout = (int)(hrrel / RB_HRTIME_PER_MSEC); // ms + timeout = (int)((hrrel + RB_HRTIME_PER_MSEC - 1) / RB_HRTIME_PER_MSEC); // ms } } rb_native_mutex_unlock(&timer_th.waiting_lock);