Skip to content

fix(sentry): Never use the global Hub outside main() - #724

Merged
mjq merged 1 commit into
mainfrom
mjq/sdk-patterns
Oct 13, 2026
Merged

mjq merged 1 commit into
mainfrom
mjq/sdk-patterns

Conversation

@mjq

@mjq mjq commented Oct 13, 2026

Copy link
Copy Markdown
Member

The Sentry SDK keeps a global Hub available at sentry.CurrentHub(), and many functions in the github.com/getsentry/sentry-go package implicitly use it.

But, every goroutine should be using its own cloned hub and call the appropriate methods directly on that hub, in order to isolate Scope changes. (See the Go SDK's concurrency docs for details).

Properly pass goroutine-local Hub clones via ctx or a function argument. Use forbidigo (already included in golangci-lint) to forbid all uses of CurrentHub and methods that implicitly target it so we don't introduce new concurrency issues in the future.

@mjq mjq changed the title ref(sdk): Never use the global Hub outside main() fix(sdk): Never use the global Hub outside main() Oct 13, 2026
@mjq
mjq marked this pull request as ready for review October 13, 2026 14:42
@mjq
mjq requested a review from a team as a code owner October 13, 2026 14:42
@mjq mjq changed the title fix(sdk): Never use the global Hub outside main() fix(sdk): Never use the global Hub outside main() Oct 13, 2026
@mjq mjq changed the title fix(sdk): Never use the global Hub outside main() fix(sentry): Never use the global Hub outside main() Oct 13, 2026
Comment thread cmd/vroom/main.go Outdated
clearDownFile()

env, err := newEnvironment()
rootHub := sentry.CurrentHub() //nolint:forbidigo // This *must* be the only exception to this lint.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nit: since this is the only rule to the exception in main(), might be useful to leave a comment explaining why this is the exception to the rule

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done - thanks!

@mjq
mjq force-pushed the mjq/sdk-patterns branch from 2085884 to 81e3d2f Compare October 13, 2026 17:46
@mjq
mjq enabled auto-merge (squash) October 13, 2026 17:47
@mjq
mjq merged commit f4928fd into main Oct 13, 2026
24 checks passed
@mjq
mjq deleted the mjq/sdk-patterns branch October 13, 2026 17:48
Comment thread .golangci.yml
settings:
forbidigo:
analyze-types: true
forbid:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

neat

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.

3 participants