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

Fixed UpgradedHolder-related race condition in ActiveHostsMan #622

Merged
merged 4 commits into from
Jul 12, 2019
Merged

Fixed UpgradedHolder-related race condition in ActiveHostsMan #622

merged 4 commits into from
Jul 12, 2019

Conversation

dutor
Copy link
Contributor

@dutor dutor commented Jul 12, 2019

UpgradedHolder makes a RWSpinLock or ReadHolder progress to the UPGRADED phase. And UPGRADED is an intermediate phase to transit(upgrade) from READ to WRITE.

@dutor dutor added the ready-for-testing PR: ready for the CI test label Jul 12, 2019
@nebula-community-bot
Copy link
Member

Unit testing passed.

monadbobo
monadbobo previously approved these changes Jul 12, 2019
Copy link
Contributor

@monadbobo monadbobo left a comment

Choose a reason for hiding this comment

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

LGTM.

wadeliuyi
wadeliuyi previously approved these changes Jul 12, 2019
Copy link
Contributor

@wadeliuyi wadeliuyi left a comment

Choose a reason for hiding this comment

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

LGTM.

auto it = hostsMap_.find(hostAddr);
if (it == hostsMap_.end()) {
folly::RWSpinLock::UpgradedHolder uh(&lock_);
WriteHolder wh(UpgradedHolder(&lock_));
Copy link
Contributor

Choose a reason for hiding this comment

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

Like this.

boshengchen
boshengchen previously approved these changes Jul 12, 2019
@dutor dutor dismissed stale reviews from boshengchen, wadeliuyi, and monadbobo via 01b62b9 July 12, 2019 09:07
@dutor
Copy link
Contributor Author

dutor commented Jul 12, 2019

Here is a snippet to demo this issue.

    folly::RWSpinLock lock;
    auto reader = std::thread([&] () {
        folly::RWSpinLock::ReadHolder rh(lock);
        fprintf(stderr, "read locked\n");
        sleep(5);
        fprintf(stderr, "read unlocked\n");
    });

    auto writer = std::thread([&]() {
        sleep(1);
        folly::RWSpinLock::ReadHolder rh(lock);
        folly::RWSpinLock::UpgradedHolder uh(lock);
        fprintf(stderr, "upgraded\n");
        //rh.reset();
        //folly::RWSpinLock::WriteHolder wh(std::move(uh));
        //fprintf(stderr, "write locked\n");
        sleep(10);
    });

    reader.join();
    writer.join();

@dutor
Copy link
Contributor Author

dutor commented Jul 12, 2019

@monadbobo @wadeliuyi @boshengchen

Please note that the first commit of this PR is buggy. It has a deadlock problem, because the writer-to-be thread, aka. the current thread, is a reader itself. So it must release the read lock before acquire the write lock.

@nebula-community-bot
Copy link
Member

Unit testing failed.

dangleptr
dangleptr previously approved these changes Jul 12, 2019
@nebula-community-bot
Copy link
Member

Unit testing passed.

// Step to Upgraded phase to stop new readers
UpgradedHolder uh(&lock_);
// Unlock the read lock
rh.reset();
Copy link
Contributor

Choose a reason for hiding this comment

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

Is hostsMap_ the single-writer-many-reader?

Copy link
Contributor

Choose a reason for hiding this comment

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

It is multi-writer, update by heartbeat.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, it's a multi-writer multi-reader case

critical27
critical27 previously approved these changes Jul 12, 2019
Copy link
Contributor

@critical27 critical27 left a comment

Choose a reason for hiding this comment

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

Learned a lot!

@dutor
Copy link
Contributor Author

dutor commented Jul 12, 2019

For simplicity, we decide to use WriteHolder instead.

If anyone has questions about the UpgradedHolder, please contact @dangleptr or me for details.

Copy link
Contributor

@critical27 critical27 left a comment

Choose a reason for hiding this comment

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

LGTM.

Copy link
Contributor

@monadbobo monadbobo left a comment

Choose a reason for hiding this comment

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

LGTM.

@nebula-community-bot
Copy link
Member

Unit testing passed.

@dangleptr
Copy link
Contributor

Thanks for taking care of it. @dutor
It is my mistake to use UpgradeLock in this situation which is not Single-writer-multi-reader.

For more information about UpgradeLock concept, please refer https://www.boost.org/doc/libs/1_58_0/doc/html/thread/synchronization.html#thread.synchronization.mutex_concepts.upgrade_lockable

PS. The implementation in folly is different with boost.
In folly, upgradeLock and sharedLock are mutually exclusive

@dangleptr dangleptr merged commit 3f54d48 into vesoft-inc:master Jul 12, 2019
@dutor dutor deleted the fix-race-condition branch July 14, 2019 01:37
yixinglu pushed a commit to yixinglu/nebula that referenced this pull request Feb 16, 2020
…-inc#622)

* Fixed UpgradedHolder-releated race condition in ActiveHostsMan

* unlock own readlock before acquire write lock

* fix dependencies

* For simplicity, use WriteHolder instead
tong-hao pushed a commit to tong-hao/nebula that referenced this pull request Jun 1, 2021
…-inc#622)

* Fixed UpgradedHolder-releated race condition in ActiveHostsMan

* unlock own readlock before acquire write lock

* fix dependencies

* For simplicity, use WriteHolder instead
yixinglu pushed a commit to yixinglu/nebula that referenced this pull request Mar 21, 2022
Co-authored-by: Dashuang Li <ldshuang@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready-for-testing PR: ready for the CI test
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants