Fix race when calling Preprocess and msg ID generator(#627)

Closes #624
This commit is contained in:
Marco Munizaga 2025-07-03 11:10:37 -07:00 committed by GitHub
parent ae65ce484e
commit e38c340f93
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 6 additions and 1 deletions

View File

@ -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
}

View File

@ -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