diff --git a/src/rpc/request.cpp b/src/rpc/request.cpp index b7acd62ee3..c706ddf8d5 100644 --- a/src/rpc/request.cpp +++ b/src/rpc/request.cpp @@ -80,7 +80,7 @@ static fs::path GetAuthCookieFile(bool temp=false) return AbsPathForConfigVal(gArgs, arg); } -static bool g_generated_cookie = false; +static std::optional g_generated_cookie; bool GenerateAuthCookie(std::string *cookie_out) { @@ -107,7 +107,7 @@ bool GenerateAuthCookie(std::string *cookie_out) LogPrintf("Unable to rename cookie authentication file %s to %s\n", fs::PathToString(filepath_tmp), fs::PathToString(filepath)); return false; } - g_generated_cookie = true; + g_generated_cookie = cookie; LogPrintf("Generated RPC authentication cookie %s\n", fs::PathToString(filepath)); if (cookie_out) @@ -134,8 +134,9 @@ bool GetAuthCookie(std::string *cookie_out) void DeleteAuthCookie() { try { - if (g_generated_cookie) { - // Delete the cookie file if it was generated by this process + std::string existing_cookie; + if (GetAuthCookie(&existing_cookie) && g_generated_cookie == existing_cookie) { + // Delete the cookie file if it exists and was generated by this process fs::remove(GetAuthCookieFile()); } } catch (const fs::filesystem_error& e) {