Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpc: keep .cookie file if it was not generated #28784

Merged
merged 1 commit into from
Dec 1, 2023

Conversation

romanz
Copy link
Contributor

@romanz romanz commented Nov 3, 2023

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).

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).
@DrahtBot
Copy link
Contributor

DrahtBot commented Nov 3, 2023

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Code Coverage

For detailed information about the code coverage, see the test coverage report.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK kristapsk, stickies-v, willcl-ark, achow101
Stale ACK luke-jr, maflcko

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #28946 (init: don't delete PID file if it was not generated by willcl-ark)
  • #28167 (init: Add option for rpccookie permissions (replace 26088) by willcl-ark)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@romanz
Copy link
Contributor Author

romanz commented Nov 3, 2023

The bug can be reproduced manually by starting bitcoind twice, resulting in a missing .cookie file:

$ bitcoind -signet &
...
2023-11-03T15:38:09Z Using random cookie authentication.
2023-11-03T15:38:09Z Generated RPC authentication cookie /home/user/.bitcoin/signet/.cookie
...

$ ls ~/.bitcoin/signet/.cookie 
/home/user/.bitcoin/signet/.cookie

$ bitcoind -signet
Error: Cannot obtain a lock on data directory /home/user/.bitcoin/signet. Bitcoin Core is probably already running.

$ ls /home/user/.bitcoin/signet/.cookie 
ls: cannot access '/home/user/.bitcoin/signet/.cookie': No such file or directory

@romanz romanz changed the title rpc: keep .cookie if it was not generated rpc: keep .cookie file if it was not generated Nov 3, 2023
Copy link
Member

@luke-jr luke-jr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK, though it might be better to do something like keep the file open and detect if it's the same file/content before we delete it.

Copy link
Contributor

@kristapsk kristapsk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK 7cb9367

@DrahtBot DrahtBot requested a review from luke-jr November 8, 2023 16:00
Copy link
Member

@luke-jr luke-jr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK after squash

luke-jr pushed a commit to bitcoinknots/bitcoin that referenced this pull request Nov 9, 2023
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).

Github-Pull: bitcoin#28784
Rebased-From: 7cb9367
luke-jr pushed a commit to bitcoinknots/bitcoin that referenced this pull request Nov 9, 2023
@romanz
Copy link
Contributor Author

romanz commented Nov 9, 2023

Thanks for the review!
Squashed and force-pushed d95dde9.

Copy link
Contributor

@kristapsk kristapsk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-ACK d95dde9

@DrahtBot DrahtBot requested a review from luke-jr November 9, 2023 05:49
@luke-jr
Copy link
Member

luke-jr commented Nov 9, 2023

re-ACK d95dde9

@DrahtBot DrahtBot removed the request for review from luke-jr November 9, 2023 22:57
luke-jr pushed a commit to bitcoinknots/bitcoin that referenced this pull request Nov 10, 2023
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).

Github-Pull: bitcoin#28784
Rebased-From: d95dde9
@romanz
Copy link
Contributor Author

romanz commented Nov 16, 2023

Just wanted to ask whether there are any other outstanding issues? :)

@maflcko maflcko modified the milestone: 27.0 Nov 17, 2023
@romanz
Copy link
Contributor Author

romanz commented Nov 24, 2023

Ping :)

Copy link
Member

@willcl-ark willcl-ark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK d95dde9

Kind of suprised this wasn't noticed earlier, but thanks for the patch!

I also noticed while testing this that the same issue is present with the pid file, but I will open a seperate pull for that in a moment.

@maflcko
Copy link
Member

maflcko commented Nov 27, 2023

lgtm ACK d95dde9

src/rpc/request.cpp Outdated Show resolved Hide resolved
@maflcko
Copy link
Member

maflcko commented Nov 30, 2023

Note: If you revert to 7cb9367 instead (which is the same), the previous reviews will be valid again

@romanz
Copy link
Contributor Author

romanz commented Nov 30, 2023

Note: If you revert to 7cb9367 instead (which is the same), the previous reviews will be valid again

Thanks - reverted to 7cb9367.

Copy link
Contributor

@kristapsk kristapsk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-ACK 7cb9367

Copy link
Contributor

@stickies-v stickies-v left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 7cb9367

@@ -131,7 +134,10 @@ bool GetAuthCookie(std::string *cookie_out)
void DeleteAuthCookie()
{
try {
fs::remove(GetAuthCookieFile());
if (g_generated_cookie) {
Copy link
Contributor

@stickies-v stickies-v Nov 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can be done without extra nesting

git diff on 7cb9367
diff --git a/src/rpc/request.cpp b/src/rpc/request.cpp
index b7acd62ee3..ad9f244f12 100644
--- a/src/rpc/request.cpp
+++ b/src/rpc/request.cpp
@@ -133,11 +133,10 @@ bool GetAuthCookie(std::string *cookie_out)
 
 void DeleteAuthCookie()
 {
+    // Don't delete the cookie file if it wasn't generated by this process
+    if (!g_generated_cookie) return;
     try {
-        if (g_generated_cookie) {
-            // Delete the cookie file if it was generated by this process
-            fs::remove(GetAuthCookieFile());
-        }
+        fs::remove(GetAuthCookieFile());
     } catch (const fs::filesystem_error& e) {
         LogPrintf("%s: Unable to remove random auth cookie file: %s\n", __func__, fsbridge::get_filesystem_error_message(e));
     }

@willcl-ark
Copy link
Member

re-ACK 7cb9367

@DrahtBot DrahtBot removed the request for review from willcl-ark December 1, 2023 11:29
@achow101
Copy link
Member

achow101 commented Dec 1, 2023

ACK 7cb9367

@achow101 achow101 merged commit 18bed14 into bitcoin:master Dec 1, 2023
20 checks passed
@romanz romanz deleted the fix-cookie-delete branch December 1, 2023 18:33
@romanz
Copy link
Contributor Author

romanz commented Dec 1, 2023

Thanks :)

achow101 added a commit that referenced this pull request Dec 4, 2023
8f6ab31 init: don't delete PID file if it was not generated (willcl-ark)

Pull request description:

  In a similar vein to #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 8f6ab31

Tree-SHA512: c9af703cbfa179d33ef9580a51e86c1b0acbd28daa18c8d2e5e5ff796ab4d3e2009a962a47e6046a0e5ece936f8a06ee8af5fdf8ff4ae1e52cbcdbec4b942271
luke-jr pushed a commit to luke-jr/bitcoin that referenced this pull request Jan 26, 2024
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).

Github-Pull: bitcoin#28784
Rebased-From: 7cb9367
luke-jr pushed a commit to luke-jr/bitcoin that referenced this pull request Jan 26, 2024
@achow101 achow101 added this to the 26.1 milestone Feb 22, 2024
darosior pushed a commit to darosior/bitcoin that referenced this pull request Feb 28, 2024
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).

Github-Pull: bitcoin#28784
Rebased-From: 7cb9367
glozow added a commit that referenced this pull request Feb 28, 2024
… generated")

1e95643 rpc: keep .cookie if it was not generated (Roman Zeyde)

Pull request description:

  v26 introduced a regression in that starting a `bitcoind` twice may have the second instance delete the cookie file of the first, making it impossible to communicate with it.

  Not a big deal but it's annoying, only an issue for 26.0, and the patch is trivial.

ACKs for top commit:
  glozow:
    lgtm ACK 1e95643

Tree-SHA512: 0e4b18aebaaf284944f1709b238c8c0acce5e8997409e0c278a5a30ac221ac1ff1d3ad31fbf2ac15b03bf7582891e07a7a2cf00f13cb596aa9512566b9320c23
@glozow
Copy link
Member

glozow commented Mar 6, 2024

this was backported for 26.x in #29503

Retropex pushed a commit to Retropex/bitcoin that referenced this pull request Mar 28, 2024
luke-jr pushed a commit to bitcoinknots/bitcoin that referenced this pull request Jun 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants