From 096bdd0902640fd2550fa2bb4b20cacb9b6d16ca Mon Sep 17 00:00:00 2001 From: Epid Date: Tue, 24 Mar 2026 01:22:03 +0300 Subject: [PATCH] fix(websocket): avoid data race with timeutil.MockUnset in tests Replace timeutil.TimeStampNow() calls in the websocket notifier with a nowTS() helper that reads time.Now().Unix() directly. TimeStampNow reads a package-level mock variable that TestIncomingEmail writes concurrently, causing a race detected by the race detector in test-pgsql CI. --- services/websocket/notifier.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/services/websocket/notifier.go b/services/websocket/notifier.go index 85c98bfb7b..af00d75d03 100644 --- a/services/websocket/notifier.go +++ b/services/websocket/notifier.go @@ -18,6 +18,12 @@ import ( "code.gitea.io/gitea/services/pubsub" ) +// nowTS returns the current time as a TimeStamp using the real wall clock, +// avoiding data races with timeutil.MockUnset during tests. +func nowTS() timeutil.TimeStamp { + return timeutil.TimeStamp(time.Now().Unix()) +} + type notificationCountEvent struct { Type string `json:"type"` Count int64 `json:"count"` @@ -42,7 +48,7 @@ func run(ctx context.Context) { return } - then := timeutil.TimeStampNow().Add(-2) + then := nowTS().Add(-2) timer := time.NewTicker(setting.UI.Notification.EventSourceUpdateTime) defer timer.Stop() @@ -51,7 +57,7 @@ func run(ctx context.Context) { case <-ctx.Done(): return case <-timer.C: - now := timeutil.TimeStampNow().Add(-2) + now := nowTS().Add(-2) uidCounts, err := activities_model.GetUIDsAndNotificationCounts(ctx, then, now) if err != nil {