Skip to content

Commit edf629d

Browse files
achow101vijaydasmp
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 85cac58 commit edf629d

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

src/init.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,11 @@ static const char* DEFAULT_ASMAP_FILENAME="ip_asn.map";
184184
* The PID file facilities.
185185
*/
186186
static const char* BITCOIN_PID_FILENAME = "dashd.pid";
187+
/**
188+
* True if this process has created a PID file.
189+
* Used to determine whether we should remove the PID file on shutdown.
190+
*/
191+
static bool g_generated_pid{false};
187192

188193
static fs::path GetPidFile(const ArgsManager& args)
189194
{
@@ -199,12 +204,24 @@ static fs::path GetPidFile(const ArgsManager& args)
199204
#else
200205
tfm::format(file, "%d\n", getpid());
201206
#endif
207+
g_generated_pid = true;
202208
return true;
203209
} else {
204210
return InitError(strprintf(_("Unable to create the PID file '%s': %s"), fs::PathToString(GetPidFile(args)), SysErrorString(errno)));
205211
}
206212
}
207213

214+
static void RemovePidFile(const ArgsManager& args)
215+
{
216+
if (!g_generated_pid) return;
217+
const auto pid_path{GetPidFile(args)};
218+
if (std::error_code error; !fs::remove(pid_path, error)) {
219+
std::string msg{error ? error.message() : "File does not exist"};
220+
LogPrintf("Unable to remove PID file (%s): %s\n", fs::PathToString(pid_path), msg);
221+
}
222+
}
223+
224+
208225
//////////////////////////////////////////////////////////////////////////////
209226
//
210227
// Shutdown
@@ -456,13 +473,7 @@ void Shutdown(NodeContext& node)
456473
node.chainman.reset();
457474
node.scheduler.reset();
458475

459-
try {
460-
if (!fs::remove(GetPidFile(*node.args))) {
461-
LogPrintf("%s: Unable to remove PID file: File does not exist\n", __func__);
462-
}
463-
} catch (const fs::filesystem_error& e) {
464-
LogPrintf("%s: Unable to remove PID file: %s\n", __func__, fsbridge::get_filesystem_error_message(e));
465-
}
476+
RemovePidFile(*node.args);
466477

467478
LogPrintf("%s: done\n", __func__);
468479
}

test/functional/feature_filelock.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@ def run_test(self):
3333
expected_msg = f"Error: Cannot obtain a lock on data directory {datadir}. {self.config['environment']['PACKAGE_NAME']} is probably already running."
3434
self.nodes[1].assert_start_raises_init_error(extra_args=[f'-datadir={self.nodes[0].datadir}', '-noserver'], expected_msg=expected_msg)
3535

36+
self.log.info("Check that cookie and PID file are not deleted when attempting to start a second bitcoind using the same datadir")
3637
cookie_file = Path(datadir) / ".cookie"
3738
assert cookie_file.exists() # should not be deleted during the second bitcoind instance shutdown
39+
pid_file = datadir / "dashd.pid"
40+
assert pid_file.exists()
3841

3942
if self.is_wallet_compiled():
4043
def check_wallet_filelock(descriptors):

0 commit comments

Comments
 (0)