From 2e9e904a5d58e0d288e9abc1cbc602a8674bc1a2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jo=C3=A3o=20Barbosa?= <joao.paulo.barbosa@gmail.com>
Date: Thu, 31 Jan 2019 00:05:18 +0000
Subject: [PATCH] wallet: Close wallet env lock file

Close .walletlock file when a BerkeleyEnvironment is deleted.

Github-Pull: #15297
Rebased-From: 2f8b8f4
---
 src/util.cpp      | 6 ++++++
 src/util.h        | 1 +
 src/wallet/db.cpp | 2 ++
 3 files changed, 9 insertions(+)

diff --git a/src/util.cpp b/src/util.cpp
index a391c5e85..b41e61ddf 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -174,6 +174,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);
diff --git a/src/util.h b/src/util.h
index 23b2a787e..957654124 100644
--- a/src/util.h
+++ b/src/util.h
@@ -77,6 +77,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 7e7fd24a8..89b68495a 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()