Skip to content

Commit 116e9c2

Browse files
committed
silence manager already closed error logs
1 parent 9617776 commit 116e9c2

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

coderd/notifications/manager.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ type Manager struct {
5353
success, failure chan dispatchResult
5454

5555
mu sync.Mutex // Protects following.
56+
closed bool
5657
notifier *notifier
5758

5859
runOnce sync.Once
@@ -134,6 +135,8 @@ func (m *Manager) WithHandlers(reg map[database.NotificationMethod]Handler) {
134135
m.handlers = reg
135136
}
136137

138+
var ErrManagerAlreadyClosed = xerrors.New("manager already closed")
139+
137140
// Run initiates the control loop in the background, which spawns a given number of notifier goroutines.
138141
// Manager requires system-level permissions to interact with the store.
139142
// Run is only intended to be run once.
@@ -145,7 +148,11 @@ func (m *Manager) Run(ctx context.Context) {
145148
go func() {
146149
err := m.loop(ctx)
147150
if err != nil {
148-
m.log.Error(ctx, "notification manager stopped with error", slog.Error(err))
151+
if xerrors.Is(err, ErrManagerAlreadyClosed) {
152+
m.log.Warn(ctx, "notification manager stopped with error", slog.Error(err))
153+
} else {
154+
m.log.Error(ctx, "notification manager stopped with error", slog.Error(err))
155+
}
149156
}
150157
}()
151158
})
@@ -160,6 +167,11 @@ func (m *Manager) loop(ctx context.Context) error {
160167
}()
161168

162169
m.mu.Lock()
170+
if m.closed {
171+
m.mu.Unlock()
172+
return ErrManagerAlreadyClosed
173+
}
174+
163175
var eg errgroup.Group
164176

165177
m.notifier = newNotifier(ctx, m.cfg, uuid.New(), m.log, m.store, m.handlers, m.helpers, m.metrics, m.clock)
@@ -348,11 +360,10 @@ func (m *Manager) Stop(ctx context.Context) error {
348360
m.mu.Lock()
349361
defer m.mu.Unlock()
350362

351-
select {
352-
case <-m.stop:
363+
if m.closed {
353364
return nil
354-
default:
355365
}
366+
m.closed = true
356367

357368
m.log.Debug(context.Background(), "graceful stop requested")
358369

0 commit comments

Comments
 (0)