Skip to content

oto: clarify the error when NewContext is called twice - #334

Closed
saromanov wants to merge 2 commits into
ebitengine:mainfrom
saromanov:friendly-context-error
Closed

saromanov wants to merge 2 commits into
ebitengine:mainfrom
saromanov:friendly-context-error

Conversation

@saromanov

Copy link
Copy Markdown

Closes #245.

NewContext can be called only once per process, but the error message did not say so. In #245 a user hit exactly this: the second call returned the error, the code checked err but the confusion led to a stall at <-readyChan, and the issue was filed as a mystery.

This PR makes the error actionable:

oto: context is already created (NewContext can be called only once per process; reuse the existing context instead)

Also mentions the restriction right next to the README example, where the single-context rule is easy to miss.

  • The new TestNewContextAfterContextCreated runs after TestMain has created the context, so the second-call path is exercised deterministically on every platform.
  • go test passes on Linux (ALSA), go vet and gofmt clean.

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.
Comment thread oto_test.go Outdated
if err == nil {
t.Fatal("NewContext after the context was created must return an error")
}
if !strings.Contains(err.Error(), "context is already created") {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comparing an error message is not good. Omit this

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ок, removed that

@hajimehoshi

Copy link
Copy Markdown
Member

Thank you for the PR. I'd prefer not to change the error message here. The one-context restriction should be made clear in the documentation instead (e.g. the NewContext godoc), so I'm closing this one.

Comment drafted by Claude (Claude Code) on behalf of @hajimehoshi.

@hajimehoshi hajimehoshi closed this Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

README example: Program stalls at second '->readyChan'

2 participants