Skip to content

Commit fa9af06

Browse files
author
MarcoFalke
committed
scripted-diff: Replace MilliSleep with UninterruptibleSleep
This is safe because MilliSleep is never executed in a boost::thread, the only type of thread that is interruptible. * The RPC server uses std::thread * The wallet is either executed in an RPC thread or the main thread * bitcoin-cli, benchmarks and tests are only one thread (the main thread) -BEGIN VERIFY SCRIPT- sed -i --regexp-extended -e 's/MilliSleep\((\S+)\);/UninterruptibleSleep(std::chrono::milliseconds{\1});/g' $(git grep -l MilliSleep) -END VERIFY SCRIPT-
1 parent fa4620b commit fa9af06

11 files changed

+13
-13
lines changed

src/bench/examples.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
static void Sleep100ms(benchmark::State& state)
1111
{
1212
while (state.KeepRunning()) {
13-
MilliSleep(100);
13+
UninterruptibleSleep(std::chrono::milliseconds{100});
1414
}
1515
}
1616

src/bitcoin-cli.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ static int CommandLineRPC(int argc, char *argv[])
524524
}
525525
catch (const CConnectionFailed&) {
526526
if (fWait)
527-
MilliSleep(1000);
527+
UninterruptibleSleep(std::chrono::milliseconds{1000});
528528
else
529529
throw;
530530
}

src/bitcoind.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static void WaitForShutdown(NodeContext& node)
2929
{
3030
while (!ShutdownRequested())
3131
{
32-
MilliSleep(200);
32+
UninterruptibleSleep(std::chrono::milliseconds{200});
3333
}
3434
Interrupt(node);
3535
}

src/httprpc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ static bool HTTPReq_JSONRPC(HTTPRequest* req, const std::string &)
174174
/* Deter brute-forcing
175175
If this results in a DoS the user really
176176
shouldn't have their RPC port exposed. */
177-
MilliSleep(250);
177+
UninterruptibleSleep(std::chrono::milliseconds{250});
178178

179179
req->WriteHeader("WWW-Authenticate", WWW_AUTH_HEADER_DATA);
180180
req->WriteReply(HTTP_UNAUTHORIZED);

src/rpc/server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ UniValue stop(const JSONRPCRequest& jsonRequest)
169169
// this reply will get back to the client.
170170
StartShutdown();
171171
if (jsonRequest.params[0].isNum()) {
172-
MilliSleep(jsonRequest.params[0].get_int());
172+
UninterruptibleSleep(std::chrono::milliseconds{jsonRequest.params[0].get_int()});
173173
}
174174
return PACKAGE_NAME " stopping";
175175
}

src/test/blockfilter_index_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ BOOST_FIXTURE_TEST_CASE(blockfilter_index_initial_sync, BuildChainTestingSetup)
138138
int64_t time_start = GetTimeMillis();
139139
while (!filter_index.BlockUntilSyncedToCurrentChain()) {
140140
BOOST_REQUIRE(time_start + timeout_ms > GetTimeMillis());
141-
MilliSleep(100);
141+
UninterruptibleSleep(std::chrono::milliseconds{100});
142142
}
143143

144144
// Check that filter index has all blocks that were in the chain before it started.

src/test/checkqueue_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueueControl_Locks)
393393
CCheckQueueControl<FakeCheck> control(queue.get());
394394
// While sleeping, no other thread should execute to this point
395395
auto observed = ++nThreads;
396-
MilliSleep(10);
396+
UninterruptibleSleep(std::chrono::milliseconds{10});
397397
fails += observed != nThreads;
398398
});
399399
}

src/test/txindex_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ BOOST_FIXTURE_TEST_CASE(txindex_initial_sync, TestChain100Setup)
3434
int64_t time_start = GetTimeMillis();
3535
while (!txindex.BlockUntilSyncedToCurrentChain()) {
3636
BOOST_REQUIRE(time_start + timeout_ms > GetTimeMillis());
37-
MilliSleep(100);
37+
UninterruptibleSleep(std::chrono::milliseconds{100});
3838
}
3939

4040
// Check that txindex excludes genesis block transactions.

src/test/util_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,7 +1322,7 @@ BOOST_AUTO_TEST_CASE(util_time_GetTime)
13221322
SetMockTime(111);
13231323
// Check that mock time does not change after a sleep
13241324
for (const auto& num_sleep : {0, 1}) {
1325-
MilliSleep(num_sleep);
1325+
UninterruptibleSleep(std::chrono::milliseconds{num_sleep});
13261326
BOOST_CHECK_EQUAL(111, GetTime()); // Deprecated time getter
13271327
BOOST_CHECK_EQUAL(111, GetTime<std::chrono::seconds>().count());
13281328
BOOST_CHECK_EQUAL(111000, GetTime<std::chrono::milliseconds>().count());
@@ -1333,7 +1333,7 @@ BOOST_AUTO_TEST_CASE(util_time_GetTime)
13331333
// Check that system time changes after a sleep
13341334
const auto ms_0 = GetTime<std::chrono::milliseconds>();
13351335
const auto us_0 = GetTime<std::chrono::microseconds>();
1336-
MilliSleep(1);
1336+
UninterruptibleSleep(std::chrono::milliseconds{1});
13371337
BOOST_CHECK(ms_0 < GetTime<std::chrono::milliseconds>());
13381338
BOOST_CHECK(us_0 < GetTime<std::chrono::microseconds>());
13391339
}

src/test/validation_block_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ BOOST_AUTO_TEST_CASE(processnewblock_signals_ordering)
205205
t.join();
206206
}
207207
while (GetMainSignals().CallbacksPending() > 0) {
208-
MilliSleep(100);
208+
UninterruptibleSleep(std::chrono::milliseconds{100});
209209
}
210210

211211
UnregisterValidationInterface(&sub);

0 commit comments

Comments
 (0)