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

Merged
merged 1 commit into from
Oct 14, 2024

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

Copy link

This PR has been stale for 60 days and will be closed automatically in 7 days. Comment to keep it open.

@github-actions github-actions bot added the Stale label Jul 13, 2024
@mitar
Copy link
Contributor Author

mitar commented Jul 13, 2024

Still relevant.

@github-actions github-actions bot removed the Stale label Jul 14, 2024
Copy link

This PR has been stale for 60 days and will be closed automatically in 7 days. Comment to keep it open.

@github-actions github-actions bot added the Stale label Sep 12, 2024
@mitar
Copy link
Contributor Author

mitar commented Sep 12, 2024

Still relevant.

Copy link

netlify bot commented Sep 12, 2024

Deploy Preview for badger-docs canceled.

Name Link
🔨 Latest commit b79c894
🔍 Latest deploy log https://app.netlify.com/sites/badger-docs/deploys/670d016136880d000874bec3

@mangalaman93 mangalaman93 merged commit de3932e into dgraph-io:main Oct 14, 2024
10 of 11 checks passed
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.

2 participants