From: Willy Tarreau Date: Fri, 1 Mar 2024 15:17:47 +0000 (+0100) Subject: BUG/MINOR: tools: seed the statistical PRNG slightly better X-Git-Tag: v3.0-dev5~38 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=7151076522eface09dc3268e9d12286ff570e9b0;p=thirdparty%2Fhaproxy.git BUG/MINOR: tools: seed the statistical PRNG slightly better Thomas Baroux reported a very interesting issue. "balance random" would systematically assign the same server first upon restart. That comes from its use of statistical_prng() which is only seeded with the thread number, and since at low loads threads are assigned to incoming connections in round robin order, practically speaking, the same thread always gets the same request and will produce the same random number. We already have a much better RNG that's also way more expensive, but we can use it at boot time to seed the PRNG instead of using the thread ID only. This needs to be backported to 2.4. --- diff --git a/src/tools.c b/src/tools.c index b2814b5af2..e1ba2411ef 100644 --- a/src/tools.c +++ b/src/tools.c @@ -6333,7 +6333,7 @@ void *dlopen(const char *filename, int flags) static int init_tools_per_thread() { /* Let's make each thread start from a different position */ - statistical_prng_state += tid * MAX_THREADS; + statistical_prng_state += ha_random32(); if (!statistical_prng_state) statistical_prng_state++; return 1;