Re: [PATCH 1/6] maintenance: add get_random_minute()
- From
Derrick Stolee <derrickstolee@github.com>
- Date
- Aug 8, 2023, 17:28 UTC
- Message-ID
- <8854e369-fabb-4044-a06c-eaf5b9fbde4a@github.com>
- In-Reply-To
- <ZNFgIyuhlNd8I9Y2@nand.local>
On 8/7/2023 5:20 PM, Taylor Blau wrote:
> On Mon, Aug 07, 2023 at 06:51:35PM +0000, Derrick Stolee via GitGitGadget wrote:
Show 12 quoted lines
>> + if (!random_initialized) {
>> + srand((unsigned int)getpid());
>> + random_initialized = 1;
>> + }
>
> I was wondering where else we call srand() within Git, and it looks like
> the only other spot is in `lock_file_timeout()`.
>
> I doubt it, but is there a chance that that code depends on only calling
> srand() once? I think the answer is "no", since we only use rand()
> within that function to generate a random-ish backoff period, so I think
> the repeatability of it doesn't matter all that much.The main point would be to avoid the cost of spinning up the number generator, but there is potential for a bug if we run both initializers: the lockfile would re-use the same filenames after the generator is reset.
> So I think this is kind of outside the scope of your series, but I > wonder if we should have a git_rand() that automatically initializes the > PRNG with the value of getpid()? Then multiple callers can grab random > values at will without reinitializing the PRNG.
I see you're moving ahead with removing the srand() from the lockfile code, so I'll focus on creating a `git_rand()` that centralizes the use of srand(), but won't touch the code in the lockfile so your patch applies independently.
Thanks, -Stolee