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

refactor: Add thread safety annotation to BanMan::SweepBanned() #25149

Merged
merged 3 commits into from May 24, 2022

Conversation

hebasto
Copy link
Member

@hebasto hebasto commented May 16, 2022

This PR adds a proper thread safety annotation to BanMan::SweepBanned().

Also a simple refactoring applied.

@fanquake
Copy link
Member

ASAN job:

2022-05-16T21:20:13.025586Z (mocktime: 1970-01-01T00:12:57Z) [test] [net_types.cpp:63] [BanMapFromJson] Dropping entry with unknown version (2) from ban list
Assertion failed: lock m_cs_banned not held in banman.cpp:176; locks held:
make[3]: *** [Makefile:20916: test/banman_tests.cpp.test] Error 1

Copy link
Contributor

@theStack theStack 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

Looks like a m_cs_banned lock acquire needs to add before the the SweepBanned call in the BanMan constructor to get rid of the failed assertion.

@ajtowns
Copy link
Contributor

ajtowns commented May 17, 2022

Moving the code that's in the constructor into a separate function, so that the constructor is just { LoadBanlist(); DumpBanlist(); } causes the compiler to give errors about the missing locks in LoadBanlist().

@maflcko
Copy link
Member

maflcko commented May 17, 2022

A constructor shouldn't need to lock a member mutex. Maybe we should fade out AssertLockHeld and remove it here?

@hebasto hebasto force-pushed the 220516-bantsa branch 2 times, most recently from ac1deb7 to f317ae1 Compare May 17, 2022 10:08
@hebasto
Copy link
Member Author

hebasto commented May 17, 2022

Reworked: c4d0613 -> f317ae1 (pr25149.01 -> pr25149.02).

PR description has been updated as well.

@ajtowns
Copy link
Contributor

ajtowns commented May 17, 2022

Doing:

BanMan::BanMan(fs::path ban_file, CClientUIInterface* client_interface, int64_t default_ban_time)
    : m_client_interface(client_interface), m_ban_db(std::move(ban_file)), m_default_ban_time(default_ban_time)
{
    LoadBanlist();
    DumpBanlist();
}

void BanMan::LoadBanlist()
{
    LOCK(m_cs_banned);

    if (m_client_interface) m_client_interface->InitMessage(_("Loading banlist…").translated);

    int64_t n_start = GetTimeMillis();
    if (m_ban_db.Read(m_banned)) {
        SweepBanned(); // sweep out unused entries

        LogPrint(BCLog::NET, "Loaded %d banned node addresses/subnets  %dms\n", m_banned.size(),
                 GetTimeMillis() - n_start);
    } else {
        LogPrintf("Recreating the banlist database\n");
        m_banned = {};
        m_is_dirty = true;
    }
}

looks cleaner to me, fwiw. Just explicitly taking the lock in the constructor seems simpler than delegating to SweepBanMan() which takes the lock then calls SweepBanMan_() too...

@DrahtBot
Copy link
Contributor

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #24097 (Replace RecursiveMutex m_cs_banned with Mutex, and rename it by hebasto)

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.

@w0xlt
Copy link
Contributor

w0xlt commented May 18, 2022

Concept ACK

@hebasto
Copy link
Member Author

hebasto commented May 20, 2022

Applied @ajtowns's suggestion which was supported by @w0xlt with 👍 .

@ajtowns
Copy link
Contributor

ajtowns commented May 20, 2022

ACK ab75388

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.

ACK ab75388

Copy link
Contributor

@theStack theStack 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 ab75388

Happy to see that another RecursiveMutex will bite the dust soon.

@maflcko maflcko merged commit aa5cd3c into bitcoin:master May 24, 2022
@hebasto hebasto deleted the 220516-bantsa branch May 24, 2022 07:20
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request May 28, 2022
…n::SweepBanned()`

ab75388 refactor: Remove redundant scope in `BanMan::SweepBanned()` (Hennadii Stepanov)
52c0b3e refactor: Add thread safety annotation to `BanMan::SweepBanned()` (Hennadii Stepanov)
3919059 refactor: Move code from ctor into private `BanMan::LoadBanlist()` (Hennadii Stepanov)

Pull request description:

  This PR adds a proper thread safety annotation to `BanMan::SweepBanned()`.

  Also a simple refactoring applied.

ACKs for top commit:
  ajtowns:
    ACK ab75388
  w0xlt:
    ACK bitcoin@ab75388
  theStack:
    Code-review ACK ab75388

Tree-SHA512: 8699079c308454f3ffe72be2e77f0935214156bd509f9338b1104f8d128bfdd02ee06ee8c1c99b2eefdf317a00edd555d52990caaeb1ed4540eedc1e3c9d1faf
@bitcoin bitcoin locked and limited conversation to collaborators May 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants