diff --git a/src/DataStreamSuite/PubSub/Server/InternalTagHandling/SystemSubscriptionMapPublisher.cs b/src/DataStreamSuite/PubSub/Server/InternalTagHandling/SystemSubscriptionMapPublisher.cs index d7c0c517b..018967273 100644 --- a/src/DataStreamSuite/PubSub/Server/InternalTagHandling/SystemSubscriptionMapPublisher.cs +++ b/src/DataStreamSuite/PubSub/Server/InternalTagHandling/SystemSubscriptionMapPublisher.cs @@ -31,6 +31,7 @@ namespace BCG { public static string tag_name = "System_AllSubscriptions"; readonly IInternalPublisher internalPublisher; readonly IWorker workerImpl; + readonly object syncRoot = new(); public SystemSubscriptionMapPublisher(IPubSubState state, IInternalPublisher internalPublisher) { bts.Log.Gen.info("SystemSubscriptionMapPublisher.constructor"); @@ -54,31 +55,35 @@ namespace BCG { return rv; }; bts.Log.Gen.info($"SystemSubscriptionMapPublisher._on_tag_state_changes tags: {tags_to_string()}"); - _dirty_tags.AddRange(tags); + lock (syncRoot) { + _dirty_tags.AddRange(tags); + } } private void _internal_publish() { - if (_dirty_tags.Count > 0) { - List tpsis = new List(); - foreach (string tag in _dirty_tags) { - Dictionary subscribers, active_pubslishers; - ICollection> dead_publishers; - _state.get_all_connections(tag, out subscribers, out active_pubslishers, out dead_publishers); - TagPubSubInfo ti = new TagPubSubInfo(tag, subscribers, active_pubslishers, - dead_publishers, _ip); - tpsis.Add(ti); + lock (syncRoot) { + if (_dirty_tags.Count > 0) { + List tpsis = new List(); + foreach (string tag in _dirty_tags) { + Dictionary subscribers, active_pubslishers; + ICollection> dead_publishers; + _state.get_all_connections(tag, out subscribers, out active_pubslishers, out dead_publishers); + TagPubSubInfo ti = new TagPubSubInfo(tag, subscribers, active_pubslishers, + dead_publishers, _ip); + tpsis.Add(ti); + } + _dirty_tags.Clear(); + var ser_bytes = TagPubSubInfo.serialize(tpsis); + PublishMsg pub_msg = new PublishMsg() { + publish_time_epoch_ms = 0, + tag = SystemSubscriptionMapPublisher.tag_name, + tag_type = SystemSubscriptionMapPublisher.tag_type, + connection = "Internal", + is_compressed = false, + data_to_publish = ser_bytes + }; + internalPublisher.Publish(pub_msg); } - _dirty_tags.Clear(); - var ser_bytes = TagPubSubInfo.serialize(tpsis); - PublishMsg pub_msg = new PublishMsg() { - publish_time_epoch_ms = 0, - tag = SystemSubscriptionMapPublisher.tag_name, - tag_type = SystemSubscriptionMapPublisher.tag_type, - connection = "Internal", - is_compressed = false, - data_to_publish = ser_bytes - }; - internalPublisher.Publish(pub_msg); } }