Skip to content

Commit a0c52f8

Browse files
achamayouCopilotCopilotCopilot
authored
Make Store::flags atomic to fix TSan data race (#7795)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: achamayou <4016369+achamayou@users.noreply.github.com>
1 parent 7aa1f20 commit a0c52f8

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

src/kv/store.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,10 @@ namespace ccf::kv
108108
// If true, use historical ledger secrets to deserialise entries
109109
const bool is_historical = false;
110110

111-
// Ledger entry header flags
112-
uint8_t flags = 0;
111+
// Store-level flags (AbstractStore::StoreFlag) influencing behaviour such
112+
// as snapshot and ledger chunk decisions. Atomic because _unsafe accessors
113+
// may be called from different threads without a common lock.
114+
std::atomic<uint8_t> flags = 0;
113115

114116
bool commit_deserialised(
115117
OrderedChanges& changes,
@@ -1342,17 +1344,19 @@ namespace ccf::kv
13421344

13431345
void set_flag_unsafe(StoreFlag f) override
13441346
{
1345-
this->flags |= static_cast<uint8_t>(f);
1347+
this->flags.fetch_or(static_cast<uint8_t>(f), std::memory_order_relaxed);
13461348
}
13471349

13481350
void unset_flag_unsafe(StoreFlag f) override
13491351
{
1350-
this->flags &= ~static_cast<uint8_t>(f);
1352+
this->flags.fetch_and(
1353+
~static_cast<uint8_t>(f), std::memory_order_relaxed);
13511354
}
13521355

13531356
[[nodiscard]] bool flag_enabled_unsafe(StoreFlag f) const override
13541357
{
1355-
return (flags & static_cast<uint8_t>(f)) != 0;
1358+
return (flags.load(std::memory_order_relaxed) &
1359+
static_cast<uint8_t>(f)) != 0;
13561360
}
13571361
};
13581362

0 commit comments

Comments
 (0)