From 8602a1e6aeca65911e4d4c821d3575147ba32a7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Barbosa?= Date: Thu, 31 Jan 2019 00:04:51 +0000 Subject: [PATCH 1/3] wallet: Close dbenv error file db.log The error file db.log is opened by BerkeleyEnvironment instance and should be closed after dbenv is closed. --- src/wallet/db.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/wallet/db.cpp b/src/wallet/db.cpp index ae405532680..24911846ed7 100644 --- a/src/wallet/db.cpp +++ b/src/wallet/db.cpp @@ -126,11 +126,16 @@ 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); } void BerkeleyEnvironment::Reset() From 2f8b8f479bb43729ca2ff40929e8463347b0b7b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Barbosa?= Date: Thu, 31 Jan 2019 00:05:18 +0000 Subject: [PATCH 2/3] wallet: Close wallet env lock file Close .walletlock file when a BerkeleyEnvironment is deleted. --- src/util/system.cpp | 6 ++++++ src/util/system.h | 1 + src/wallet/db.cpp | 2 ++ 3 files changed, 9 insertions(+) diff --git a/src/util/system.cpp b/src/util/system.cpp index 06317a3a909..d8b70ee52ca 100644 --- a/src/util/system.cpp +++ b/src/util/system.cpp @@ -116,6 +116,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 lock(cs_dir_locks); + dir_locks.erase((directory / lockfile_name).string()); +} + void ReleaseDirectoryLocks() { std::lock_guard ulock(cs_dir_locks); diff --git a/src/util/system.h b/src/util/system.h index 5932e557932..17723d427da 100644 --- a/src/util/system.h +++ b/src/util/system.h @@ -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 diff --git a/src/wallet/db.cpp b/src/wallet/db.cpp index 24911846ed7..463f8e1f41a 100644 --- a/src/wallet/db.cpp +++ b/src/wallet/db.cpp @@ -136,6 +136,8 @@ void BerkeleyEnvironment::Close() DbEnv((u_int32_t)0).remove(strPath.c_str(), 0); if (error_file) fclose(error_file); + + UnlockDirectory(strPath, ".walletlock"); } void BerkeleyEnvironment::Reset() From d3bf3b930d34da7d121ae35b4fb75865ed73208c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Barbosa?= Date: Mon, 4 Feb 2019 18:50:21 +0000 Subject: [PATCH 3/3] qa: Test .walletlock file is closed --- test/functional/wallet_multiwallet.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/functional/wallet_multiwallet.py b/test/functional/wallet_multiwallet.py index 8ab569a3c30..df778f57df7 100755 --- a/test/functional/wallet_multiwallet.py +++ b/test/functional/wallet_multiwallet.py @@ -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()