From 1577b5fa6732ef55d51ace6fc930f27c78e095b6 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Thu, 4 Apr 2024 12:42:41 -0400 Subject: [PATCH] Make SQLite busy back-off logic portable Use C++ standard library not Unix functions for sleeping and randomness. Suggested by @edolstra in https://github.com/NixOS/nix/pull/8901#discussion_r1550416615 --- src/libstore/sqlite.cc | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/libstore/sqlite.cc b/src/libstore/sqlite.cc index 06abfb90b..3175c1978 100644 --- a/src/libstore/sqlite.cc +++ b/src/libstore/sqlite.cc @@ -7,6 +7,7 @@ #include #include +#include namespace nix { @@ -256,10 +257,8 @@ void handleSQLiteBusy(const SQLiteBusy & e, time_t & nextWarning) /* Sleep for a while since retrying the transaction right away is likely to fail again. */ checkInterrupt(); - struct timespec t; - t.tv_sec = 0; - t.tv_nsec = (random() % 100) * 1000 * 1000; /* <= 0.1s */ - nanosleep(&t, 0); + /* <= 0.1s */ + std::this_thread::sleep_for(std::chrono::milliseconds { rand() % 100 }); } }