From ca918c187dc52653bcbd88a72e77088def8ac753 Mon Sep 17 00:00:00 2001 From: Nia Weiss Date: Wed, 17 Feb 2021 15:39:22 -0500 Subject: [PATCH] fix a race condition when deferring foreign-index initialization --- holder.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/holder.go b/holder.go index 8400f1811..c24d178f1 100644 --- a/holder.go +++ b/holder.go @@ -121,7 +121,8 @@ type Holder struct { // Queue of fields (having a foreign index) which have // opened before their foreign index has opened. - foreignIndexFields []*Field + foreignIndexFields []*Field + foreignIndexFieldsMu sync.Mutex // opening is set to true while Holder is opening. // It's used to determine if foreign index application @@ -738,6 +739,8 @@ func (h *Holder) Activate() { func (h *Holder) checkForeignIndex(f *Field) error { if h.opening { if fi := h.Index(f.options.ForeignIndex); fi == nil { + h.foreignIndexFieldsMu.Lock() + defer h.foreignIndexFieldsMu.Unlock() h.foreignIndexFields = append(h.foreignIndexFields, f) return nil }