From de6aaa1657e30dcae88c8d55dd69435f3d032853 Mon Sep 17 00:00:00 2001 From: Mike Robbins Date: Sun, 22 Dec 2024 12:49:10 -0500 Subject: [PATCH 1/2] Fix error handling for LibC.clock_gettime(CLOCK_MONOTONIC) calls --- src/crystal/system/unix/pthread_condition_variable.cr | 3 ++- src/crystal/system/unix/time.cr | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/crystal/system/unix/pthread_condition_variable.cr b/src/crystal/system/unix/pthread_condition_variable.cr index a09811c79281..abfdc23b1d12 100644 --- a/src/crystal/system/unix/pthread_condition_variable.cr +++ b/src/crystal/system/unix/pthread_condition_variable.cr @@ -42,7 +42,8 @@ class Thread LibC.pthread_cond_timedwait_relative_np(self, mutex, pointerof(ts)) {% else %} - LibC.clock_gettime(LibC::CLOCK_MONOTONIC, out ts) + clock_gettime_ret = LibC.clock_gettime(LibC::CLOCK_MONOTONIC, out ts) + raise RuntimeError.from_errno("clock_gettime(CLOCK_MONOTONIC)") unless clock_gettime_ret == 0 ts.tv_sec += time.to_i ts.tv_nsec += time.nanoseconds diff --git a/src/crystal/system/unix/time.cr b/src/crystal/system/unix/time.cr index 2ead3bdb0fa2..165f605fd7f6 100644 --- a/src/crystal/system/unix/time.cr +++ b/src/crystal/system/unix/time.cr @@ -39,9 +39,8 @@ module Crystal::System::Time nanoseconds = total_nanoseconds.remainder(NANOSECONDS_PER_SECOND) {seconds.to_i64, nanoseconds.to_i32} {% else %} - if LibC.clock_gettime(LibC::CLOCK_MONOTONIC, out tp) == 1 - raise RuntimeError.from_errno("clock_gettime(CLOCK_MONOTONIC)") - end + ret = LibC.clock_gettime(LibC::CLOCK_MONOTONIC, out tp) + raise RuntimeError.from_errno("clock_gettime(CLOCK_MONOTONIC)") unless ret == 0 {tp.tv_sec.to_i64, tp.tv_nsec.to_i32} {% end %} end @@ -51,7 +50,8 @@ module Crystal::System::Time info = mach_timebase_info LibC.mach_absolute_time &* info.numer // info.denom {% else %} - LibC.clock_gettime(LibC::CLOCK_MONOTONIC, out tp) + ret = LibC.clock_gettime(LibC::CLOCK_MONOTONIC, out tp) + raise RuntimeError.from_errno("clock_gettime(CLOCK_MONOTONIC)") unless ret == 0 tp.tv_sec.to_u64! &* NANOSECONDS_PER_SECOND &+ tp.tv_nsec.to_u64! {% end %} end From 0b89f8d09fe0d75ec30338e0a91f2ec677f77e36 Mon Sep 17 00:00:00 2001 From: Mike Robbins Date: Sun, 22 Dec 2024 19:33:13 -0500 Subject: [PATCH 2/2] Only fix the specifically broken error handling on Crystal::System::Time.monotonic --- src/crystal/system/unix/pthread_condition_variable.cr | 3 +-- src/crystal/system/unix/time.cr | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/crystal/system/unix/pthread_condition_variable.cr b/src/crystal/system/unix/pthread_condition_variable.cr index abfdc23b1d12..a09811c79281 100644 --- a/src/crystal/system/unix/pthread_condition_variable.cr +++ b/src/crystal/system/unix/pthread_condition_variable.cr @@ -42,8 +42,7 @@ class Thread LibC.pthread_cond_timedwait_relative_np(self, mutex, pointerof(ts)) {% else %} - clock_gettime_ret = LibC.clock_gettime(LibC::CLOCK_MONOTONIC, out ts) - raise RuntimeError.from_errno("clock_gettime(CLOCK_MONOTONIC)") unless clock_gettime_ret == 0 + LibC.clock_gettime(LibC::CLOCK_MONOTONIC, out ts) ts.tv_sec += time.to_i ts.tv_nsec += time.nanoseconds diff --git a/src/crystal/system/unix/time.cr b/src/crystal/system/unix/time.cr index 165f605fd7f6..5ffcc6f373a2 100644 --- a/src/crystal/system/unix/time.cr +++ b/src/crystal/system/unix/time.cr @@ -50,8 +50,7 @@ module Crystal::System::Time info = mach_timebase_info LibC.mach_absolute_time &* info.numer // info.denom {% else %} - ret = LibC.clock_gettime(LibC::CLOCK_MONOTONIC, out tp) - raise RuntimeError.from_errno("clock_gettime(CLOCK_MONOTONIC)") unless ret == 0 + LibC.clock_gettime(LibC::CLOCK_MONOTONIC, out tp) tp.tv_sec.to_u64! &* NANOSECONDS_PER_SECOND &+ tp.tv_nsec.to_u64! {% end %} end