fix(coderd/notifications): do not fetch after a graceful stop (#32627)

## TL;DR

Fix a race condition where the notification sender could fetch another batch after graceful shutdown was requested, delaying shutdown and causing intermittent `TestBackpressure` failures.

Refs https://linear.app/codercom/issue/CORE-718/flake-testbackpressure

## Changes

- Check the graceful context before fetching a new batch.
- Allow in-flight batches to complete and persist their results before shutdown.
- Leave `quartz` and other `TickerFunc` callers unchanged.
- Add `TestNotifier_NoFetchAfterGracefulStop` to verify that a late ticker callback does not trigger another fetch.

## Testing

- New regression test fails without the fix and passes with it.
- Notifier tests pass with `-race -count=5`.
- `golangci-lint` reports zero issues.
- Reproduced the original CI failure under high concurrency.
- With an artificially widened race window: 10/10 failures before the fix, 0/20 after the fix.
- Database-backed tests were validated on an earlier commit but have not been rerun on this branch.
This commit is contained in:
Bobby Ho authored and GitHub committed 2026-11-24 12:46:12 -07:00
1 parent 27eecbf092
commit f32341ea4e
2 files changed
+65

No files matched your search

+6
View File
@@ -101,6 +101,12 @@ func (n *notifier) run(success chan<- dispatchResult, failure chan<- dispatchRes
// run the ticker with the graceful context, so we stop fetching after stop() is called
tick := n.clock.TickerFunc(n.gracefulCtx, n.cfg.FetchInterval.Value(), func() error {
// The ticker can deliver one more tick after the graceful context is
// canceled, so do not start a new fetch during shutdown.
if n.gracefulCtx.Err() != nil {
return nil
}
// Check if notifier is not paused.
ok, err := n.ensureRunning(n.outerCtx)
if err != nil {
@@ -0,0 +1,59 @@
package notifications
import (
"context"
"testing"
"time"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
"github.com/coder/coder/v2/coderd/database/dbmock"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/testutil"
"github.com/coder/quartz"
)
func TestNotifier_NoFetchAfterGracefulStop(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitShort)
// The mock store fails the test on any call, which is how a fetch
// would show up.
store := dbmock.NewMockStore(gomock.NewController(t))
clock := &lateTickClock{Clock: quartz.NewMock(t), fetch: make(chan func() error, 1)}
n := newNotifier(ctx, codersdk.NotificationsConfig{}, uuid.New(), testutil.Logger(t), store, nil, nil, nil, clock)
runErr := make(chan error, 1)
go func() {
runErr <- n.run(nil, nil)
}()
fetch := testutil.TryReceive(ctx, t, clock.fetch)
n.stop()
require.NoError(t, fetch())
require.NoError(t, testutil.TryReceive(ctx, t, runErr))
}
// lateTickClock hands the ticker callback to the test, so the test can
// deliver a tick after the ticker's context is canceled.
type lateTickClock struct {
quartz.Clock
fetch chan func() error
}
func (c *lateTickClock) TickerFunc(ctx context.Context, _ time.Duration, f func() error, _ ...string) quartz.Waiter {
c.fetch <- f
return contextWaiter{ctx: ctx}
}
type contextWaiter struct {
ctx context.Context
}
func (w contextWaiter) Wait(...string) error {
<-w.ctx.Done()
return w.ctx.Err()
}