Skip to content

fix(coderd/batchstats): fix init race and close flush #9248

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

Merged
merged 5 commits into from
Aug 23, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
better fix
  • Loading branch information
mafredri committed Aug 22, 2023
commit abab11c7fe40a5034e7f702552e0164cab9eeeba
10 changes: 8 additions & 2 deletions coderd/batchstats/batcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (b *Batcher) Add(
// Run runs the batcher.
func (b *Batcher) run(ctx context.Context) {
// nolint:gocritic // This is only ever used for one thing - inserting agent stats.
authCtx := dbauthz.AsSystemRestricted(context.Background())
authCtx := dbauthz.AsSystemRestricted(ctx)
for {
select {
case <-b.tickCh:
Expand All @@ -185,7 +185,13 @@ func (b *Batcher) run(ctx context.Context) {
b.flush(authCtx, true, "reaching capacity")
case <-ctx.Done():
b.log.Debug(ctx, "context done, flushing before exit")
b.flush(authCtx, true, "exit")

// We must create a new context here as the parent context is done.
ctxTimeout, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel() //nolint:revive // We're returning, defer is fine.

// nolint:gocritic // This is only ever used for one thing - inserting agent stats.
b.flush(dbauthz.AsSystemRestricted(ctxTimeout), true, "exit")
return
}
}
Expand Down