From a258f78736fd622dd9b33a239ab2c9c1b643bd1b Mon Sep 17 00:00:00 2001 From: Lily Ballard Date: Sat, 9 Nov 2024 19:18:54 -0800 Subject: [PATCH] libstore: make SQLite busy back-off logic portable Use C++ standard library not Unix functions for sleeping and randomness. Also change the PRNG seed from `clock()` to `std::random_device` because there's no good reason not to and this avoids two threads seeding their PRNG the same. Upstream-PR: https://github.com/NixOS/nix/pull/10399 Change-Id: I8424e7406366ea8e825d3e93dbb261ce60e3a37f --- lix/libstore/sqlite.cc | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lix/libstore/sqlite.cc b/lix/libstore/sqlite.cc index 217f470a2..bf88b7142 100644 --- a/lix/libstore/sqlite.cc +++ b/lix/libstore/sqlite.cc @@ -5,9 +5,10 @@ #include "lix/libutil/signals.hh" #include "lix/libutil/url.hh" -#include #include +#include +#include namespace nix { @@ -284,12 +285,10 @@ void handleSQLiteBusy(const SQLiteBusy & e, time_t & nextWarning) /* Sleep for a while since retrying the transaction right away is likely to fail again. */ checkInterrupt(); - static thread_local std::default_random_engine generator(clock()); + static thread_local std::default_random_engine generator(std::random_device{}()); std::uniform_int_distribution uniform_dist(0, 100); - struct timespec t; - t.tv_sec = 0; - t.tv_nsec = uniform_dist(generator) * 1000 * 1000; /* <= 0.1s */ - nanosleep(&t, 0); + /* <= 0.1s */ + std::this_thread::sleep_for(std::chrono::milliseconds { uniform_dist(generator) }); } }