From 6e76ad0dcbf150f046e114614106c0b0d73354f1 Mon Sep 17 00:00:00 2001 From: Sergey Romanov Date: Mon, 9 Nov 2026 10:23:50 +0500 Subject: [PATCH 1/2] oto: clarify the error when NewContext is called twice NewContext can be called only once per process, but the error did not say so. Users hit this and stalled waiting on the ready channel, not noticing the error (#245). Point to the fix: reuse the existing context. Mention the restriction next to the README example too. --- README.md | 3 ++- context.go | 2 +- oto_test.go | 15 +++++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7af2cda..c28a189 100644 --- a/README.md +++ b/README.md @@ -123,7 +123,8 @@ func main() { // Format of the source. go-mp3's format is signed 16bit integers. op.Format = oto.FormatSignedInt16LE - // Remember that you should **not** create more than one context + // Remember that you should **not** create more than one context. + // Calling oto.NewContext more than once returns an error: reuse the context for all your players. otoCtx, readyChan, err := oto.NewContext(op) if err != nil { panic("oto.NewContext failed: " + err.Error()) diff --git a/context.go b/context.go index 8d337bd..5a49b25 100644 --- a/context.go +++ b/context.go @@ -107,7 +107,7 @@ func NewContext(options *NewContextOptions) (*Context, chan struct{}, error) { defer contextCreationMutex.Unlock() if contextCreated { - return nil, nil, fmt.Errorf("oto: context is already created") + return nil, nil, fmt.Errorf("oto: context is already created (NewContext can be called only once per process; reuse the existing context instead)") } contextCreated = true diff --git a/oto_test.go b/oto_test.go index fac52f4..4c11456 100644 --- a/oto_test.go +++ b/oto_test.go @@ -17,6 +17,7 @@ package oto_test import ( "bytes" "os" + "strings" "testing" "time" @@ -60,3 +61,17 @@ func TestSetBufferSize(t *testing.T) { } } } + +func TestNewContextAfterContextCreated(t *testing.T) { + // TestMain already created the context, so this call must fail. + op := &oto.NewContextOptions{} + op.SampleRate = 48000 + op.ChannelCount = 2 + _, _, err := oto.NewContext(op) + if err == nil { + t.Fatal("NewContext after the context was created must return an error") + } + if !strings.Contains(err.Error(), "context is already created") { + t.Errorf("unexpected error: %v", err) + } +} From fd68d2698a5769a4e1453d694ce682877034d82b Mon Sep 17 00:00:00 2001 From: saromanov Date: Mon, 9 Nov 2026 22:45:27 +0500 Subject: [PATCH 2/2] oto: don't compare the error message in the repeated NewContext test --- oto_test.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/oto_test.go b/oto_test.go index 4c11456..f1aba09 100644 --- a/oto_test.go +++ b/oto_test.go @@ -17,7 +17,6 @@ package oto_test import ( "bytes" "os" - "strings" "testing" "time" @@ -71,7 +70,4 @@ func TestNewContextAfterContextCreated(t *testing.T) { if err == nil { t.Fatal("NewContext after the context was created must return an error") } - if !strings.Contains(err.Error(), "context is already created") { - t.Errorf("unexpected error: %v", err) - } }