Skip to content

Commit 30e799a

Browse files
committed
Merge #15297: wallet: Releases dangling files on BerkeleyEnvironment::Close
d3bf3b9 qa: Test .walletlock file is closed (João Barbosa) 2f8b8f4 wallet: Close wallet env lock file (João Barbosa) 8602a1e wallet: Close dbenv error file db.log (João Barbosa) Pull request description: This PR closes `db.log` and removes `.walletlock` files when `BerkeleyEnvironment` is closed. Fixes bitcoin/bitcoin#15291 (comment). Tree-SHA512: 05d8b027feea914e0ba873e75d117857473d1fd7b400e41bd473d638171fa39d5be048990bf685dc0807f7d92418579b763056dc2a6dcf6b96777d5688ddee04
2 parents 3a573fd + d3bf3b9 commit 30e799a

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

src/util/system.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ bool LockDirectory(const fs::path& directory, const std::string lockfile_name, b
111111
return true;
112112
}
113113

114+
void UnlockDirectory(const fs::path& directory, const std::string& lockfile_name)
115+
{
116+
std::lock_guard<std::mutex> lock(cs_dir_locks);
117+
dir_locks.erase((directory / lockfile_name).string());
118+
}
119+
114120
void ReleaseDirectoryLocks()
115121
{
116122
std::lock_guard<std::mutex> ulock(cs_dir_locks);

src/util/system.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ int RaiseFileDescriptorLimit(int nMinFD);
7070
void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length);
7171
bool RenameOver(fs::path src, fs::path dest);
7272
bool LockDirectory(const fs::path& directory, const std::string lockfile_name, bool probe_only=false);
73+
void UnlockDirectory(const fs::path& directory, const std::string& lockfile_name);
7374
bool DirIsWritable(const fs::path& directory);
7475

7576
/** Release all directory locks. This is used for unit testing only, at runtime

src/wallet/db.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,18 @@ void BerkeleyEnvironment::Close()
126126
}
127127
}
128128

129+
FILE* error_file = nullptr;
130+
dbenv->get_errfile(&error_file);
131+
129132
int ret = dbenv->close(0);
130133
if (ret != 0)
131134
LogPrintf("BerkeleyEnvironment::Close: Error %d closing database environment: %s\n", ret, DbEnv::strerror(ret));
132135
if (!fMockDb)
133136
DbEnv((u_int32_t)0).remove(strPath.c_str(), 0);
137+
138+
if (error_file) fclose(error_file);
139+
140+
UnlockDirectory(strPath, ".walletlock");
134141
}
135142

136143
void BerkeleyEnvironment::Reset()

test/functional/wallet_multiwallet.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,14 @@ def wallet_file(name):
315315
self.nodes[0].loadwallet(wallet_name)
316316
assert_equal(rpc.getaddressinfo(addr)['ismine'], True)
317317

318+
# Test .walletlock file is closed
319+
self.start_node(1)
320+
wallet = os.path.join(self.options.tmpdir, 'my_wallet')
321+
self.nodes[0].createwallet(wallet)
322+
assert_raises_rpc_error(-4, "Error initializing wallet database environment", self.nodes[1].loadwallet, wallet)
323+
self.nodes[0].unloadwallet(wallet)
324+
self.nodes[1].loadwallet(wallet)
325+
318326

319327
if __name__ == '__main__':
320328
MultiWalletTest().main()

0 commit comments

Comments
 (0)