Merge #15297: wallet: Releases dangling files on BerkeleyEnvironment::Close

d3bf3b930 qa: Test .walletlock file is closed (João Barbosa)
2f8b8f479 wallet: Close wallet env lock file (João Barbosa)
8602a1e6a 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 https://github.com/bitcoin/bitcoin/issues/15291#issuecomment-459131886.

Tree-SHA512: 05d8b027feea914e0ba873e75d117857473d1fd7b400e41bd473d638171fa39d5be048990bf685dc0807f7d92418579b763056dc2a6dcf6b96777d5688ddee04
This commit is contained in:
MeshCollider 2019-02-06 10:09:24 +13:00
commit 30e799a5f7
No known key found for this signature in database
GPG key ID: D300116E1C875A3D
4 changed files with 22 additions and 0 deletions

View file

@ -111,6 +111,12 @@ bool LockDirectory(const fs::path& directory, const std::string lockfile_name, b
return true;
}
void UnlockDirectory(const fs::path& directory, const std::string& lockfile_name)
{
std::lock_guard<std::mutex> lock(cs_dir_locks);
dir_locks.erase((directory / lockfile_name).string());
}
void ReleaseDirectoryLocks()
{
std::lock_guard<std::mutex> ulock(cs_dir_locks);

View file

@ -70,6 +70,7 @@ int RaiseFileDescriptorLimit(int nMinFD);
void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length);
bool RenameOver(fs::path src, fs::path dest);
bool LockDirectory(const fs::path& directory, const std::string lockfile_name, bool probe_only=false);
void UnlockDirectory(const fs::path& directory, const std::string& lockfile_name);
bool DirIsWritable(const fs::path& directory);
/** Release all directory locks. This is used for unit testing only, at runtime

View file

@ -126,11 +126,18 @@ void BerkeleyEnvironment::Close()
}
}
FILE* error_file = nullptr;
dbenv->get_errfile(&error_file);
int ret = dbenv->close(0);
if (ret != 0)
LogPrintf("BerkeleyEnvironment::Close: Error %d closing database environment: %s\n", ret, DbEnv::strerror(ret));
if (!fMockDb)
DbEnv((u_int32_t)0).remove(strPath.c_str(), 0);
if (error_file) fclose(error_file);
UnlockDirectory(strPath, ".walletlock");
}
void BerkeleyEnvironment::Reset()

View file

@ -315,6 +315,14 @@ class MultiWalletTest(BitcoinTestFramework):
self.nodes[0].loadwallet(wallet_name)
assert_equal(rpc.getaddressinfo(addr)['ismine'], True)
# Test .walletlock file is closed
self.start_node(1)
wallet = os.path.join(self.options.tmpdir, 'my_wallet')
self.nodes[0].createwallet(wallet)
assert_raises_rpc_error(-4, "Error initializing wallet database environment", self.nodes[1].loadwallet, wallet)
self.nodes[0].unloadwallet(wallet)
self.nodes[1].loadwallet(wallet)
if __name__ == '__main__':
MultiWalletTest().main()