librdkafka
librdkafka copied to clipboard
rand: Ensure rd_jitter functions properly for new threads
In cases where a Windows producer spins up a new thread to produce a single message, the value emitted by rd_jitter is exactly the same for every new thread.
On Windows, calls to srand() modify thread-local state, with subsequent calls to rand() reading that same thread-local state.
The previous fix to call srand() once during rd_kafka_new (#2795) does not fully fix this situation, as a brand new thread can take the object without ever calling rd_kafka_new() or rd_kafka_global_srand().
This patch rectifies this problem by using a version of rand_r() from the musl c library for Win32 platforms. On POSIX platforms, rd_rand_r() is a #define that points to the system's existing rand_r().
This PR addresses the issue I raised in #3929.
@edenhill the commit history on this PR is a little messy. If you'd like me to clean it up as per the contributing guidelines, I can.
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.