Skip to content

Commit b2863c0

Browse files
committed
Merge #14426: utils: Fix broken Windows filelock
369244f utils: Fix broken Windows filelock (Chun Kuan Lee) Pull request description: Fix broken filelock on Windows, also add a test for this. It's a regression introduced by #13862. Tree-SHA512: 15665b1930cf39ec71f3ab07def8e2897659f6fd4d2de749d63a5a8ec920e4a04282f12bc262f242b1b3d14d2dd9fa191ddbcf16a46fb927b5b2b14d9f6b5d01
2 parents b14db5a + 369244f commit b2863c0

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

src/fs.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#ifndef WIN32
44
#include <fcntl.h>
55
#else
6+
#define NOMINMAX
67
#include <codecvt>
78
#include <windows.h>
89
#endif
@@ -89,7 +90,7 @@ bool FileLock::TryLock()
8990
return false;
9091
}
9192
_OVERLAPPED overlapped = {0};
92-
if (!LockFileEx(hFile, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0, 0, 0, &overlapped)) {
93+
if (!LockFileEx(hFile, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0, std::numeric_limits<DWORD>::max(), std::numeric_limits<DWORD>::max(), &overlapped)) {
9394
reason = GetErrorReason();
9495
return false;
9596
}

test/functional/feature_filelock.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2018 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
"""Check that it's not possible to start a second bitcoind instance using the same datadir or wallet."""
6+
import os
7+
8+
from test_framework.test_framework import BitcoinTestFramework
9+
from test_framework.test_node import ErrorMatch
10+
11+
class FilelockTest(BitcoinTestFramework):
12+
def set_test_params(self):
13+
self.setup_clean_chain = True
14+
self.num_nodes = 2
15+
16+
def setup_network(self):
17+
self.add_nodes(self.num_nodes, extra_args=None)
18+
self.nodes[0].start([])
19+
self.nodes[0].wait_for_rpc_connection()
20+
21+
def run_test(self):
22+
datadir = os.path.join(self.nodes[0].datadir, 'regtest')
23+
self.log.info("Using datadir {}".format(datadir))
24+
25+
self.log.info("Check that we can't start a second bitcoind instance using the same datadir")
26+
expected_msg = "Error: Cannot obtain a lock on data directory {}. Bitcoin Core is probably already running.".format(datadir)
27+
self.nodes[1].assert_start_raises_init_error(extra_args=['-datadir={}'.format(self.nodes[0].datadir), '-noserver'], expected_msg=expected_msg)
28+
29+
if self.is_wallet_compiled():
30+
wallet_dir = os.path.join(datadir, 'wallets')
31+
self.log.info("Check that we can't start a second bitcoind instance using the same wallet")
32+
expected_msg = "Error: Error initializing wallet database environment"
33+
self.nodes[1].assert_start_raises_init_error(extra_args=['-walletdir={}'.format(wallet_dir), '-noserver'], expected_msg=expected_msg, match=ErrorMatch.PARTIAL_REGEX)
34+
35+
if __name__ == '__main__':
36+
FilelockTest().main()

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@
174174
'rpc_getblockstats.py',
175175
'p2p_fingerprint.py',
176176
'feature_uacomment.py',
177+
'feature_filelock.py',
177178
'p2p_unrequested_blocks.py',
178179
'feature_includeconf.py',
179180
'rpc_scantxoutset.py',

0 commit comments

Comments
 (0)