Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 90770499 authored by David Anderson's avatar David Anderson Committed by Automerger Merge Worker
Browse files

Merge "snapuserd: Fix race condition in MergeWorker::WaitForMergeBegin." into main am: c718295a

parents 17a2bf47 c718295a
Loading
Loading
Loading
Loading
+18 −21
Original line number Original line Diff line number Diff line
@@ -189,36 +189,33 @@ void SnapshotHandler::InitiateMerge() {
    cv.notify_all();
    cv.notify_all();
}
}


static inline bool IsMergeBeginError(MERGE_IO_TRANSITION io_state) {
    return io_state == MERGE_IO_TRANSITION::READ_AHEAD_FAILURE ||
           io_state == MERGE_IO_TRANSITION::IO_TERMINATED;
}

// Invoked by Merge thread - Waits on RA thread to resume merging. Will
// Invoked by Merge thread - Waits on RA thread to resume merging. Will
// be waken up RA thread.
// be waken up RA thread.
bool SnapshotHandler::WaitForMergeBegin() {
bool SnapshotHandler::WaitForMergeBegin() {
    {
    std::unique_lock<std::mutex> lock(lock_);
    std::unique_lock<std::mutex> lock(lock_);
        while (!MergeInitiated()) {
            cv.wait(lock);


            if (io_state_ == MERGE_IO_TRANSITION::READ_AHEAD_FAILURE ||
    cv.wait(lock, [this]() -> bool { return MergeInitiated() || IsMergeBeginError(io_state_); });
                io_state_ == MERGE_IO_TRANSITION::IO_TERMINATED) {

    if (IsMergeBeginError(io_state_)) {
        SNAP_LOG(ERROR) << "WaitForMergeBegin failed with state: " << io_state_;
        SNAP_LOG(ERROR) << "WaitForMergeBegin failed with state: " << io_state_;
        return false;
        return false;
    }
    }
        }


        while (!(io_state_ == MERGE_IO_TRANSITION::MERGE_BEGIN ||
    cv.wait(lock, [this]() -> bool {
                 io_state_ == MERGE_IO_TRANSITION::READ_AHEAD_FAILURE ||
        return io_state_ == MERGE_IO_TRANSITION::MERGE_BEGIN || IsMergeBeginError(io_state_);
                 io_state_ == MERGE_IO_TRANSITION::IO_TERMINATED)) {
    });
            cv.wait(lock);
        }


        if (io_state_ == MERGE_IO_TRANSITION::READ_AHEAD_FAILURE ||
    if (IsMergeBeginError(io_state_)) {
            io_state_ == MERGE_IO_TRANSITION::IO_TERMINATED) {
        SNAP_LOG(ERROR) << "WaitForMergeBegin failed with state: " << io_state_;
        SNAP_LOG(ERROR) << "WaitForMergeBegin failed with state: " << io_state_;
        return false;
        return false;
    }
    }

    return true;
    return true;
}
}
}


// Invoked by RA thread - Flushes the RA block to scratch space if necessary
// Invoked by RA thread - Flushes the RA block to scratch space if necessary
// and then notifies the merge thread to resume merging
// and then notifies the merge thread to resume merging