From e38c340f93f463b690917ad21fc5170969ef565c Mon Sep 17 00:00:00 2001 From: Marco Munizaga Date: Thu, 3 Jul 2025 11:10:37 -0700 Subject: [PATCH] Fix race when calling Preprocess and msg ID generator(#627) Closes #624 --- midgen.go | 3 +++ topic.go | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/midgen.go b/midgen.go index 9d3acfc..291e297 100644 --- a/midgen.go +++ b/midgen.go @@ -9,6 +9,7 @@ import ( // msgIDGenerator handles computing IDs for msgs // It allows setting custom generators(MsgIdFunction) per topic type msgIDGenerator struct { + sync.Mutex Default MsgIdFunction topicGensLk sync.RWMutex @@ -31,6 +32,8 @@ func (m *msgIDGenerator) Set(topic string, gen MsgIdFunction) { // ID computes ID for the msg or short-circuits with the cached value. func (m *msgIDGenerator) ID(msg *Message) string { + m.Lock() + defer m.Unlock() if msg.ID != "" { return msg.ID } diff --git a/topic.go b/topic.go index c438ebc..3a65052 100644 --- a/topic.go +++ b/topic.go @@ -349,7 +349,9 @@ func (t *Topic) validate(ctx context.Context, data []byte, opts ...PubOpt) (*Mes } msg := &Message{m, "", t.p.host.ID(), pub.validatorData, pub.local} - t.p.rt.Preprocess(t.p.host.ID(), []*Message{msg}) + t.p.eval <- func() { + t.p.rt.Preprocess(t.p.host.ID(), []*Message{msg}) + } err := t.p.val.ValidateLocal(msg) if err != nil { return nil, err