Skip to content

Fix race in index delegate callback #110

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

Merged
merged 2 commits into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:4.2
// swift-tools-version:5.0

import PackageDescription

Expand Down Expand Up @@ -70,7 +70,11 @@ let package = Package(
.target(
name: "IndexStoreDB_Database",
dependencies: ["IndexStoreDB_Core"],
path: "lib/Database"),
path: "lib/Database",
cSettings: [
.define("MDB_USE_POSIX_MUTEX", to: "1"),
.define("MDB_USE_ROBUST", to: "0"),
]),

// Core index types.
.target(
Expand Down
47 changes: 19 additions & 28 deletions lib/Index/IndexSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ class AsyncIndexDelegate : public IndexSystemDelegate {
AsyncIndexDelegate(std::shared_ptr<IndexSystemDelegate> Other)
: Others(1, std::move(Other)) {}

~AsyncIndexDelegate() {
_wait(); // Ensure the queue is drained, since we capture `this`.
}

void addDelegate(std::shared_ptr<IndexSystemDelegate> Other) {
Queue.dispatchSync([&] {
if (PendingActions)
Expand All @@ -60,36 +64,25 @@ class AsyncIndexDelegate : public IndexSystemDelegate {

private:
virtual void processingAddedPending(unsigned NumActions) override {
PendingActions += NumActions;

if (Others.empty())
return;
auto LocalOthers = this->Others;
Queue.dispatch([LocalOthers, NumActions]{
for (auto &other : LocalOthers)
Queue.dispatch([this, NumActions]{
PendingActions += NumActions;
for (auto &other : Others)
other->processingAddedPending(NumActions);
});
}

virtual void processingCompleted(unsigned NumActions) override {
assert(NumActions <= PendingActions);
PendingActions -= NumActions;

if (Others.empty())
return;
auto LocalOthers = this->Others;
Queue.dispatch([LocalOthers, NumActions]{
for (auto &other : LocalOthers)
Queue.dispatch([this, NumActions]{
assert(NumActions <= PendingActions);
PendingActions -= NumActions;
for (auto &other : Others)
other->processingCompleted(NumActions);
});
}

virtual void processedStoreUnit(StoreUnitInfo unitInfo) override {
if (Others.empty())
return;
auto LocalOthers = this->Others;
Queue.dispatch([LocalOthers, unitInfo]{
for (auto &other : LocalOthers)
Queue.dispatch([this, unitInfo]{
for (auto &other : Others)
other->processedStoreUnit(unitInfo);
});
}
Expand All @@ -98,24 +91,22 @@ class AsyncIndexDelegate : public IndexSystemDelegate {
llvm::sys::TimePoint<> outOfDateModTime,
OutOfDateTriggerHintRef hint,
bool synchronous) override {
if (Others.empty())
return;

if (synchronous) {
for (auto &other : Others)
other->unitIsOutOfDate(std::move(unitInfo), outOfDateModTime, hint, true);
Queue.dispatchSync([&]{
for (auto &other : Others)
other->unitIsOutOfDate(std::move(unitInfo), outOfDateModTime, hint, true);
});
return;
}

auto LocalOthers = this->Others;
Queue.dispatch([=]{
for (auto &other : LocalOthers)
for (auto &other : Others)
other->unitIsOutOfDate(std::move(unitInfo), outOfDateModTime, hint, false);
});
}

public:
/// For Testing. Wait for any outstanding async work to finish.
/// Public for Testing. Wait for any outstanding async work to finish.
void _wait() {
Queue.dispatchSync([]{});
}
Expand Down