Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sentinel errors should not have stack traces #2042

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mitar
Copy link
Contributor

@mitar mitar commented Jan 16, 2024

Problem

While debugging a program which is using badger I noticed that stack traces returned from my code (which uses errors with stack traces) are useless. I found the reason: badger uses sentinel errors (great!) but it creates them with stack traces at the "global init time" (e.g., var ErrFoo = errors.New(...)) and not at "return from function time". Because of that I was seeing stack traces like:

github.com/dgraph-io/badger/v4.init
	/go/pkg/mod/github.com/dgraph-io/badger/v4@v4.2.0/compaction.go:41
runtime.doInit1
	/usr/local/go/src/runtime/proc.go:6740
runtime.doInit
	/usr/local/go/src/runtime/proc.go:6707
runtime.main
	/usr/local/go/src/runtime/proc.go:249
runtime.goexit
	/usr/local/go/src/runtime/asm_amd64.s:1650

Instead of a stack trace which would show me where the call which is returning sentinel error is made.

Solution

Ideally, one would create all sentinel errors without stack traces (standard errors package works great for that) and then when the error happens in a function, you return errors.WithStack(ErrFoobar) instead of just ErrFoobar. This means that sentinel error is then annotated with a stack trace at the place where the error happens. But if you do that, then you have to check for sentinel errors with errors.Is instead of just comparing errors with equality (==). That might be a breaking change for people who are not using errors.Is as they should. Because badger's own codebase uses equality instead of errors.Is I decided to not do this in this PR, but instead:

Just make sentinel errors without stack traces. This allows me to annotate with a stack trace inside my program (otherwise my program does not do so because if finds an existing stack trace and assumes it is a deeper one) and I get at least that part of the stack trace. Equality still works as usual.

I suggest this is merged this way and in v5 of badger errors.WithStack and required errors.Is are introduced.

Side note

Some time ago I made this errors package for Go which fixes various issues in github.com/pkg/errors and also has direct support for sentinel (in that package called "base") errors. It also has some features badger has in its y package for dealing with errors. You could consider adopting that package to have a comprehensive errors handling in badger.

Copy link

netlify bot commented Jan 16, 2024

Deploy Preview for badger-docs canceled.

Name Link
🔨 Latest commit 5071b42
🔍 Latest deploy log https://app.netlify.com/sites/badger-docs/deploys/65a68457a50b900008a9928c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant