[ntcore] Queue current value on subscriber creation (#4938)

This fixes a potential race condition in code that only uses readQueue.
This commit is contained in:
Peter Johnson
2023-01-13 20:07:24 -08:00
committed by GitHub
parent f7f19207e0
commit 039edcc23f
2 changed files with 59 additions and 3 deletions

View File

@@ -96,6 +96,7 @@ struct TopicData {
NT_Entry entry{0}; // cached entry for GetEntry()
bool onNetwork{false}; // true if there are any remote publishers
bool lastValueFromNetwork{false};
wpi::SmallVector<DataLoggerEntry, 1> datalogs;
NT_Type datalogType{NT_UNASSIGNED};
@@ -484,6 +485,7 @@ void LSImpl::CheckReset(TopicData* topic) {
}
topic->lastValue = {};
topic->lastValueNetwork = {};
topic->lastValueFromNetwork = false;
topic->type = NT_UNASSIGNED;
topic->typeStr.clear();
topic->flags = 0;
@@ -503,6 +505,7 @@ bool LSImpl::SetValue(TopicData* topic, const Value& value,
// TODO: notify option even if older value
topic->type = value.type();
topic->lastValue = value;
topic->lastValueFromNetwork = false;
NotifyValue(topic, eventFlags, isDuplicate, publisher);
}
if (!isDuplicate && topic->datalogType == value.type()) {
@@ -858,6 +861,17 @@ SubscriberData* LSImpl::AddLocalSubscriber(TopicData* topic,
DEBUG4("-> NetworkSubscribe({})", topic->name);
m_network->Subscribe(subscriber->handle, {{topic->name}}, config);
}
// queue current value
if (subscriber->active) {
if (!topic->lastValueFromNetwork && !config.disableLocal) {
subscriber->pollStorage.emplace_back(topic->lastValue);
subscriber->handle.Set();
} else if (topic->lastValueFromNetwork && !config.disableRemote) {
subscriber->pollStorage.emplace_back(topic->lastValueNetwork);
subscriber->handle.Set();
}
}
return subscriber;
}
@@ -1376,6 +1390,7 @@ void LocalStorage::NetworkSetValue(NT_Topic topicHandle, const Value& value) {
if (m_impl->SetValue(topic, value, NT_EVENT_VALUE_REMOTE,
value == topic->lastValue, nullptr)) {
topic->lastValueNetwork = value;
topic->lastValueFromNetwork = true;
}
}
}