Skip to content

Commit 7cb9367

Browse files
committed
rpc: keep .cookie if it was not generated
Otherwise, starting bitcoind twice may cause the `.cookie` file generated by the first instance to be deleted by the second instance shutdown (after failing to obtain a lock).
1 parent d9007f5 commit 7cb9367

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/rpc/request.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ static fs::path GetAuthCookieFile(bool temp=false)
8080
return AbsPathForConfigVal(gArgs, arg);
8181
}
8282

83+
static bool g_generated_cookie = false;
84+
8385
bool GenerateAuthCookie(std::string *cookie_out)
8486
{
8587
const size_t COOKIE_SIZE = 32;
@@ -105,6 +107,7 @@ bool GenerateAuthCookie(std::string *cookie_out)
105107
LogPrintf("Unable to rename cookie authentication file %s to %s\n", fs::PathToString(filepath_tmp), fs::PathToString(filepath));
106108
return false;
107109
}
110+
g_generated_cookie = true;
108111
LogPrintf("Generated RPC authentication cookie %s\n", fs::PathToString(filepath));
109112

110113
if (cookie_out)
@@ -131,7 +134,10 @@ bool GetAuthCookie(std::string *cookie_out)
131134
void DeleteAuthCookie()
132135
{
133136
try {
134-
fs::remove(GetAuthCookieFile());
137+
if (g_generated_cookie) {
138+
// Delete the cookie file if it was generated by this process
139+
fs::remove(GetAuthCookieFile());
140+
}
135141
} catch (const fs::filesystem_error& e) {
136142
LogPrintf("%s: Unable to remove random auth cookie file: %s\n", __func__, fsbridge::get_filesystem_error_message(e));
137143
}

test/functional/feature_filelock.py

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

33+
cookie_file = datadir / ".cookie"
34+
assert cookie_file.exists() # should not be deleted during the second bitcoind instance shutdown
35+
3336
if self.is_wallet_compiled():
3437
def check_wallet_filelock(descriptors):
3538
wallet_name = ''.join([random.choice(string.ascii_lowercase) for _ in range(6)])

0 commit comments

Comments
 (0)