Skip to content

Commit 9916564

Browse files
authored
[release/5.x] Cherry pick: Prevent racey access to indexer state (#6886) (#6913)
1 parent 094ae51 commit 9916564

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

include/ccf/indexing/indexer_interface.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "ccf/indexing/strategy.h"
66
#include "ccf/node_subsystem_interface.h"
7+
#include "ccf/pal/locking.h"
78

89
#include <map>
910
#include <memory>
@@ -19,6 +20,7 @@ namespace ccf::indexing
1920
class IndexingStrategies : public ccf::AbstractNodeSubSystem
2021
{
2122
protected:
23+
ccf::pal::Mutex lock;
2224
std::set<StrategyPtr> strategies;
2325

2426
public:
@@ -36,11 +38,13 @@ namespace ccf::indexing
3638
throw std::logic_error("Tried to install null strategy");
3739
}
3840

41+
std::lock_guard<ccf::pal::Mutex> guard(lock);
3942
return strategies.insert(strategy).second;
4043
}
4144

4245
void uninstall_strategy(const StrategyPtr& strategy)
4346
{
47+
std::lock_guard<ccf::pal::Mutex> guard(lock);
4448
if (strategy == nullptr || strategies.find(strategy) == strategies.end())
4549
{
4650
throw std::logic_error("Strategy doesn't exist");
@@ -49,8 +53,9 @@ namespace ccf::indexing
4953
strategies.erase(strategy);
5054
}
5155

52-
nlohmann::json describe() const
56+
nlohmann::json describe()
5357
{
58+
std::lock_guard<ccf::pal::Mutex> guard(lock);
5459
auto j = nlohmann::json::array();
5560

5661
for (const auto& strategy : strategies)

include/ccf/indexing/strategy.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#pragma once
44

55
#include "ccf/kv/read_only_store.h"
6+
#include "ccf/pal/locking.h"
67
#include "ccf/tx_id.h"
78

89
#include <optional>
@@ -68,6 +69,7 @@ namespace ccf::indexing
6869
class LazyStrategy : public Base
6970
{
7071
protected:
72+
ccf::pal::Mutex lock;
7173
ccf::SeqNo max_requested_seqno = 0;
7274

7375
public:
@@ -78,6 +80,7 @@ namespace ccf::indexing
7880
const auto base = Base::next_requested();
7981
if (base.has_value())
8082
{
83+
std::lock_guard<ccf::pal::Mutex> guard(lock);
8184
if (*base <= max_requested_seqno)
8285
{
8386
return base;
@@ -89,6 +92,7 @@ namespace ccf::indexing
8992

9093
void extend_index_to(ccf::TxID to_txid)
9194
{
95+
std::lock_guard<ccf::pal::Mutex> guard(lock);
9296
if (to_txid.seqno > max_requested_seqno)
9397
{
9498
max_requested_seqno = to_txid.seqno;

src/indexing/indexer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ namespace ccf::indexing
6868
update_commit(newly_committed);
6969

7070
std::optional<ccf::SeqNo> min_requested = std::nullopt;
71+
72+
std::lock_guard<ccf::pal::Mutex> guard(lock);
73+
7174
for (auto& strategy : strategies)
7275
{
7376
strategy->tick();

0 commit comments

Comments
 (0)