@cryptotaxi247 / netdata / commits / 5001ae138

Improve job completion handling with timeout mechanism (#20657)

Stelios Fragkakis committed Jul 11, 2025 at 23:05 UTC 5001ae1388f2b322883dd010d5ec7add817bd24b
1 file changed +15 -8
src/libnetdata/completion/completion.c
+15 -8
@@ -92,20 +92,27 @@ ALWAYS_INLINE unsigned completion_wait_for_a_job(struct completion *p, unsigned
92 ALWAYS_INLINE unsigned completion_wait_for_a_job_with_timeout(struct completion *p, unsigned completed_jobs, uint64_t timeout_ms)
93 {
94 uint64_t timeout_ns = timeout_ms * NSEC_PER_MSEC;
95 - if(!timeout_ns) timeout_ns = 1;
95 + if (timeout_ns == 0) timeout_ns = 1;
96
97 - uint64_t start_time_ns = uv_hrtime();
97 + uint64_t deadline_ns = uv_hrtime() + timeout_ns;
98
99 uv_mutex_lock(&p->mutex);
100 - while (0 == p->completed && p->completed_jobs <= completed_jobs) {
101 - int rc = uv_cond_timedwait(&p->cond, &p->mutex, timeout_ns);
102 - if(rc == UV_ETIMEDOUT)
100 +
101 + while (p->completed == 0 && p->completed_jobs <= completed_jobs) {
102 + uint64_t current_time_ns = uv_hrtime();
103 +
104 + // Check if we've already exceeded the deadline
105 + if (current_time_ns >= deadline_ns) {
106 break;
107 + }
108
105 - uint64_t elapsed = uv_hrtime() - start_time_ns;
106 - if (elapsed >= timeout_ns) break;
107 - timeout_ns -= elapsed;
109 + uint64_t remaining_timeout_ns = deadline_ns - current_time_ns;
110 +
111 + int rc = uv_cond_timedwait(&p->cond, &p->mutex, remaining_timeout_ns);
112 + if (rc == UV_ETIMEDOUT)
113 + break;
114 }
115 +
116 completed_jobs = p->completed_jobs;
117 uv_mutex_unlock(&p->mutex);
118