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

Replace RecursiveMutex m_cs_banned with Mutex, and rename it #24097

Merged
merged 5 commits into from Nov 2, 2023

Conversation

hebasto
Copy link
Member

@hebasto hebasto commented Jan 18, 2022

This PR is an alternative to #24092. Last two commit have been cherry-picked from the latter.

@DrahtBot
Copy link
Contributor

DrahtBot commented Jan 18, 2022

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 theStack, vasild, maflcko, achow101
Concept ACK shaavan
Stale ACK w0xlt, promag

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

Conflicts

No conflicts as of last run.

@hebasto hebasto marked this pull request as ready for review January 18, 2022 18:05
Copy link
Contributor

@w0xlt w0xlt left a comment

Choose a reason for hiding this comment

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

tACK 39e101f

In addition to the refactoring, this PR:

. Adds lock acquisition in BanMan::DumpBanlist before calling SweepBanned().

. Replaces BanMan::SetBannedSetDirty() with WITH_LOCK(m_cs_banned, m_is_dirty = false); (write operation) or simply m_is_dirty (non-blocking read operation).

. SweepBanned() no longer locks the mutex, just checks that it is held. This function is now called with WITH_LOCK(m_banned_mutex, SweepBanned());.

. BanMan::DumpBanlist called BanMan::SweepBanned() twice (directly and via BanMan::GetBanned). Now it gets rid of BanMan::GetBanned and handles banmap_t banmap within the lock scope.

Copy link
Contributor

@shaavan shaavan left a comment

Choose a reason for hiding this comment

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

Code Review ACK 39e101f

I prefer that the SweepBanned function, instead of locking m_cs_banned by itself, now asserts it being locked by its caller function. This prevents any need for double locking, and hence m_cs_banned could be safely converted from RecursiveMutex to Mutex.

The renaming of m_cs_banned to m_banned_mutex is an improvement, and I agree with it.

The conversion of m_banned_mutex from RecursiveMutex to Mutex is very cleanly implemented and is clutter-free.

Copy link
Member

@promag promag left a comment

Choose a reason for hiding this comment

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

Code review ACK 39e101f.

There is a behavior change where m_client_interface->BannedListChanged() is now called with the lock held. The only place that connects to that signal is in ClientModel where updateBanlist is called asynchronously, so I'd say this change is deadlock-free.

@hebasto
Copy link
Member Author

hebasto commented Jan 24, 2022

@promag

There is a behavior change where m_client_interface->BannedListChanged() is now called with the lock held.

The same behavior is on the master branch:

bitcoin/src/banman.cpp

Lines 160 to 166 in d0bf9bb

void BanMan::GetBanned(banmap_t& banmap)
{
LOCK(m_cs_banned);
// Sweep the banlist so expired bans are not returned
SweepBanned();
banmap = m_banned; //create a thread safe copy
}

isn't it?

@promag
Copy link
Member

promag commented Jan 24, 2022

@hebasto right! Only checked that from DumpBanlist the lock isn't held, but from GetBanned it is. So this change makes it consistent 🎉

src/banman.cpp Outdated Show resolved Hide resolved
@hebasto
Copy link
Member Author

hebasto commented Jan 26, 2022

@MarcoFalke

Strictly speaking, fixing the logical race is a bugfix, so it might be better to do it first to allow easier cherry-picking to older commits. Would you mind going with the fix first?

I split bugfixes into #24168.


Fellow reviewers, please review #24168 first.

Converting this to a draft for now.

@hebasto hebasto marked this pull request as draft January 26, 2022 15:35
maflcko pushed a commit to bitcoin-core/gui that referenced this pull request Jan 31, 2022
…pBanlist()`

99a6b69 Fix race condition for SetBannedSetDirty() calls (Hennadii Stepanov)
83c7646 Avoid calling BanMan::SweepBanned() twice in a row (Hennadii Stepanov)
33bda6a Fix data race condition in BanMan::DumpBanlist() (Hennadii Stepanov)
5e20e9e Prevent possible concurrent CBanDB::Write() calls (Hennadii Stepanov)

Pull request description:

  This PR split from bitcoin/bitcoin#24097 with some additions. This makes the following switch from `RecursiveMutex` to `Mutex` a pure refactoring.

  See details in commit messages.

ACKs for top commit:
  w0xlt:
    reACK 99a6b69
  shaavan:
    ACK 99a6b69

Tree-SHA512: da4e7268c7bd3424491f446145f18af4ccfc804023d0a7fe70e1462baab550a5e44f9159f8b9f9c7820d2c6cb6447b63883616199e4d9d439ab9ab1b67c7201b
@hebasto hebasto marked this pull request as ready for review January 31, 2022 11:30
@hebasto
Copy link
Member Author

hebasto commented Jan 31, 2022

Rebased 39e101f -> 0ad0288 (pr24097.02 -> pr24097.03) on top of the merged #24168.

Copy link
Contributor

@shaavan shaavan left a comment

Choose a reason for hiding this comment

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

Concept ACK

I had a nit suggestion and a doubt that I would like to discuss.

src/banman.cpp Show resolved Hide resolved
src/banman.cpp Show resolved Hide resolved
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Feb 1, 2022
99a6b69 Fix race condition for SetBannedSetDirty() calls (Hennadii Stepanov)
83c7646 Avoid calling BanMan::SweepBanned() twice in a row (Hennadii Stepanov)
33bda6a Fix data race condition in BanMan::DumpBanlist() (Hennadii Stepanov)
5e20e9e Prevent possible concurrent CBanDB::Write() calls (Hennadii Stepanov)

Pull request description:

  This PR split from bitcoin#24097 with some additions. This makes the following switch from `RecursiveMutex` to `Mutex` a pure refactoring.

  See details in commit messages.

ACKs for top commit:
  w0xlt:
    reACK 99a6b69
  shaavan:
    ACK 99a6b69

Tree-SHA512: da4e7268c7bd3424491f446145f18af4ccfc804023d0a7fe70e1462baab550a5e44f9159f8b9f9c7820d2c6cb6447b63883616199e4d9d439ab9ab1b67c7201b
@hebasto hebasto marked this pull request as draft May 16, 2022 20:20
@hebasto
Copy link
Member Author

hebasto commented May 16, 2022

Converted to draft for now. Please review #25149 first.

@DrahtBot DrahtBot requested review from shaavan and w0xlt and removed request for shaavan and w0xlt October 11, 2023 11:30
@hebasto
Copy link
Member Author

hebasto commented Oct 24, 2023

@maflcko Mind having a look at this PR please?

@DrahtBot DrahtBot requested review from w0xlt and shaavan and removed request for shaavan and w0xlt October 24, 2023 11:15
@maflcko
Copy link
Member

maflcko commented Oct 25, 2023

ACK 37d150d 🎾

Show signature

Signature:

untrusted comment: signature from minisign secret key on empty file; verify via: minisign -Vm "${path_to_any_empty_file}" -P RWTRmVTMeKV5noAMqVlsMugDDCyyTSbA3Re5AkUrhvLVln0tSaFWglOw -x "${path_to_this_whole_four_line_signature_blob}"
RUTRmVTMeKV5npGrKx1nqXCw5zeVHdtdYURB/KlyA/LMFgpNCs+SkW9a8N95d+U4AP1RJMi+krxU1A3Yux4bpwZNLvVBKy0wLgM=
trusted comment: ACK 37d150d8c5ffcb2bddcd99951a739e97571194c7 🎾
6hI0SRliStMofz4F+1OOUsdVFjJFjIicGUMlRpqAROJJGilc1hwLblhiZg1bxrodqlBVD+O8wLbCbdwCzCQPCA==

@DrahtBot DrahtBot requested review from w0xlt and shaavan and removed request for maflcko, w0xlt and shaavan October 25, 2023 08:53
@hebasto
Copy link
Member Author

hebasto commented Nov 2, 2023

@fanquake @achow101

Is there anything else to do here?

@DrahtBot DrahtBot requested review from w0xlt and shaavan and removed request for w0xlt and shaavan November 2, 2023 14:46
@achow101
Copy link
Member

achow101 commented Nov 2, 2023

ACK 37d150d

@DrahtBot DrahtBot requested review from w0xlt and shaavan and removed request for w0xlt and shaavan November 2, 2023 17:50
@achow101 achow101 merged commit 0857f29 into bitcoin:master Nov 2, 2023
16 checks passed
@hebasto hebasto deleted the 220118-banman branch November 2, 2023 18:26
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jan 12, 2024
99a6b69 Fix race condition for SetBannedSetDirty() calls (Hennadii Stepanov)
83c7646 Avoid calling BanMan::SweepBanned() twice in a row (Hennadii Stepanov)
33bda6a Fix data race condition in BanMan::DumpBanlist() (Hennadii Stepanov)
5e20e9e Prevent possible concurrent CBanDB::Write() calls (Hennadii Stepanov)

Pull request description:

  This PR split from bitcoin#24097 with some additions. This makes the following switch from `RecursiveMutex` to `Mutex` a pure refactoring.

  See details in commit messages.

ACKs for top commit:
  w0xlt:
    reACK 99a6b69
  shaavan:
    ACK 99a6b69

Tree-SHA512: da4e7268c7bd3424491f446145f18af4ccfc804023d0a7fe70e1462baab550a5e44f9159f8b9f9c7820d2c6cb6447b63883616199e4d9d439ab9ab1b67c7201b
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jan 12, 2024
99a6b69 Fix race condition for SetBannedSetDirty() calls (Hennadii Stepanov)
83c7646 Avoid calling BanMan::SweepBanned() twice in a row (Hennadii Stepanov)
33bda6a Fix data race condition in BanMan::DumpBanlist() (Hennadii Stepanov)
5e20e9e Prevent possible concurrent CBanDB::Write() calls (Hennadii Stepanov)

Pull request description:

  This PR split from bitcoin#24097 with some additions. This makes the following switch from `RecursiveMutex` to `Mutex` a pure refactoring.

  See details in commit messages.

ACKs for top commit:
  w0xlt:
    reACK 99a6b69
  shaavan:
    ACK 99a6b69

Tree-SHA512: da4e7268c7bd3424491f446145f18af4ccfc804023d0a7fe70e1462baab550a5e44f9159f8b9f9c7820d2c6cb6447b63883616199e4d9d439ab9ab1b67c7201b
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jan 14, 2024
99a6b69 Fix race condition for SetBannedSetDirty() calls (Hennadii Stepanov)
83c7646 Avoid calling BanMan::SweepBanned() twice in a row (Hennadii Stepanov)
33bda6a Fix data race condition in BanMan::DumpBanlist() (Hennadii Stepanov)
5e20e9e Prevent possible concurrent CBanDB::Write() calls (Hennadii Stepanov)

Pull request description:

  This PR split from bitcoin#24097 with some additions. This makes the following switch from `RecursiveMutex` to `Mutex` a pure refactoring.

  See details in commit messages.

ACKs for top commit:
  w0xlt:
    reACK 99a6b69
  shaavan:
    ACK 99a6b69

Tree-SHA512: da4e7268c7bd3424491f446145f18af4ccfc804023d0a7fe70e1462baab550a5e44f9159f8b9f9c7820d2c6cb6447b63883616199e4d9d439ab9ab1b67c7201b
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

10 participants