Make reading a header's opcodes safe for concurrent readers

indexOf wrote the node's remembered index even when it was right, so
Header.Get raced when a document was read from several goroutines.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-01 19:15:55 +00:00
co-authored by Claude Sonnet 5.5
parent 158661eefe
commit e08f3a5d0b
2 changed files with 29 additions and 1 deletions
+25
View File
@@ -6,6 +6,7 @@ import (
"io"
"reflect"
"strings"
"sync"
"testing"
"time"
)
@@ -360,3 +361,27 @@ func TestEqualsInValue(t *testing.T) {
}
}
}
// TestConcurrentReads checks that a document that hasn't been edited can be
// read by several goroutines at once. It is run with the race detector.
func TestConcurrentReads(t *testing.T) {
doc := mustParse(t, "#define $A 1\n<group> volume=-3\n<region> sample=a.wav lokey=c4\n<region> sample=b.wav\n", nil)
var wg sync.WaitGroup
for i := 0; i < 8; i++ {
wg.Add(1)
go func() {
defer wg.Done()
doc.Validate()
inst := doc.Flatten().Instrument()
_ = doc.String()
for _, h := range doc.Headers() {
h.Get("sample")
h.Opcodes()
}
if _, err := inst.Patches(0, inst.Regions[0]); err != nil {
t.Error(err)
}
}()
}
wg.Wait()
}