Skip to content

Commit 54de20b

Browse files
achow101Claude Code
authored andcommitted
Merge bitcoin#28946: init: don't delete PID file if it was not generated
8f6ab31 init: don't delete PID file if it was not generated (willcl-ark) Pull request description: In a similar vein to bitcoin#28784, if a second `bitcoind` is started using the same datadir it will fail to start up, but during shutdown remove the PID file from the first `bitcoind` instance. ACKs for top commit: achow101: ACK 8f6ab31 andrewtoth: ACK 8f6ab31 romanz: ACK bitcoin@8f6ab31 Tree-SHA512: c9af703cbfa179d33ef9580a51e86c1b0acbd28daa18c8d2e5e5ff796ab4d3e2009a962a47e6046a0e5ece936f8a06ee8af5fdf8ff4ae1e52cbcdbec4b942271
1 parent 3b3169d commit 54de20b

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

src/init.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ static const char* DEFAULT_ASMAP_FILENAME="ip_asn.map";
178178
* The PID file facilities.
179179
*/
180180
static const char* BITCOIN_PID_FILENAME = "dashd.pid";
181+
/**
182+
* True if this process has created a PID file.
183+
* Used to determine whether we should remove the PID file on shutdown.
184+
*/
185+
static bool g_generated_pid{false};
181186

182187
static fs::path GetPidFile(const ArgsManager& args)
183188
{
@@ -193,12 +198,24 @@ static fs::path GetPidFile(const ArgsManager& args)
193198
#else
194199
tfm::format(file, "%d\n", getpid());
195200
#endif
201+
g_generated_pid = true;
196202
return true;
197203
} else {
198204
return InitError(strprintf(_("Unable to create the PID file '%s': %s"), fs::PathToString(GetPidFile(args)), SysErrorString(errno)));
199205
}
200206
}
201207

208+
static void RemovePidFile(const ArgsManager& args)
209+
{
210+
if (!g_generated_pid) return;
211+
const auto pid_path{GetPidFile(args)};
212+
if (std::error_code error; !fs::remove(pid_path, error)) {
213+
std::string msg{error ? error.message() : "File does not exist"};
214+
LogPrintf("Unable to remove PID file (%s): %s\n", fs::PathToString(pid_path), msg);
215+
}
216+
}
217+
218+
202219
//////////////////////////////////////////////////////////////////////////////
203220
//
204221
// Shutdown
@@ -419,13 +436,7 @@ void Shutdown(NodeContext& node)
419436
node.chainman.reset();
420437
node.scheduler.reset();
421438

422-
try {
423-
if (!fs::remove(GetPidFile(*node.args))) {
424-
LogPrintf("%s: Unable to remove PID file: File does not exist\n", __func__);
425-
}
426-
} catch (const fs::filesystem_error& e) {
427-
LogPrintf("%s: Unable to remove PID file: %s\n", __func__, fsbridge::get_filesystem_error_message(e));
428-
}
439+
RemovePidFile(*node.args);
429440

430441
node.args = nullptr;
431442
LogPrintf("%s: done\n", __func__);

test/functional/feature_filelock.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python3
1+
#\!/usr/bin/env python3
22
# Copyright (c) 2018-2020 The Bitcoin Core developers
33
# Distributed under the MIT software license, see the accompanying
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
@@ -28,6 +28,12 @@ def run_test(self):
2828
expected_msg = f"Error: Cannot obtain a lock on data directory {datadir}. {self.config['environment']['PACKAGE_NAME']} is probably already running."
2929
self.nodes[1].assert_start_raises_init_error(extra_args=[f'-datadir={self.nodes[0].datadir}', '-noserver'], expected_msg=expected_msg)
3030

31+
self.log.info("Check that cookie and PID file are not deleted when attempting to start a second dashd using the same datadir")
32+
cookie_file = os.path.join(datadir, ".cookie")
33+
assert os.path.exists(cookie_file) # should not be deleted during the second dashd instance shutdown
34+
pid_file = os.path.join(datadir, "dashd.pid")
35+
assert os.path.exists(pid_file)
36+
3137
if self.is_wallet_compiled():
3238
def check_wallet_filelock(descriptors):
3339
wallet_name = ''.join([random.choice(string.ascii_lowercase) for _ in range(6)])
@@ -47,3 +53,4 @@ def check_wallet_filelock(descriptors):
4753

4854
if __name__ == '__main__':
4955
FilelockTest().main()
56+
EOF < /dev/null

0 commit comments

Comments
 (0)