From 392b146860afc24bb7d13654c76eb14b3f2a2b8f Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Sun, 8 Mar 2020 17:53:53 +0100 Subject: [PATCH] BUG/MINOR: checks/threads: use ha_random() and not rand() In order to honor spread_checks we currently call rand() which is not thread safe and which must never turn its internal state to zero. This is not thread safe, let's use ha_random() instead. This is a complement to commimt 52bf839394 ("BUG/MEDIUM: random: implement a thread-safe and process-safe PRNG") and may be backported with it. (cherry picked from commit 5a6d3e797edc7ac52560cc1a5bd90a984b6b350b) Signed-off-by: Willy Tarreau --- src/checks.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/checks.c b/src/checks.c index 952114e..4f2296a 100644 --- a/src/checks.c +++ b/src/checks.c @@ -2203,7 +2203,7 @@ static struct task *process_chk_proc(struct task *t, void *context, unsigned sho rv = 0; if (global.spread_checks > 0) { rv = srv_getinter(check) * global.spread_checks / 100; - rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0))); + rv -= (int) (2 * rv * (ha_random32() / 4294967295.0)); } t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv)); } @@ -2411,7 +2411,7 @@ static struct task *process_chk_conn(struct task *t, void *context, unsigned sho rv = 0; if (global.spread_checks > 0) { rv = srv_getinter(check) * global.spread_checks / 100; - rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0))); + rv -= (int) (2 * rv * (ha_random32() / 4294967295.0)); } t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv)); } -- 1.7.10.4