cockroachdb / cockroachdb/cockroach
util: replacement to `context.WithCancel` that introduces a stack trace in the cancellation error
- Dominant language
- Go
- Stars
- 32.5k
- Forks
- 4.1k
- PR merge metrics
- PR metrics pending
Description
Discussed/proposed by @ecwall
Since we use go 1.20 we can re-implement a better `WithCancel` as follows:
```go
func WithCancel(ctx context.Context) (context.Context, func()) {
ctx, cancelFn := context.WithCancelCause(ctx)
simpleCancelFn := func() {
// Ensure that the stack trace at the time of cancellation is included in the error.
enhancedError := errors.WithStack(context.Canceled)
cancelFn(enhancedError)
}
return ctx, simpleCancelFn
}
```
Then we would have a linter that enforces we use this version everywhere instead of the stdlib.
Similarly we probably want to build a new custom version of `context.WithTimeout` which does something similar.
Jira issue: CRDB-31455
Epic CRDB-60947
Contributor guide
Research direction
Start by locating the existing uses of context.WithCancel and context.WithTimeout and reviewing how cancellation errors are handled. Done would include agreed custom replacements, adoption across the relevant uses, and a linter enforcing the intended APIs.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- backend, tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100