fix(server): stop the background compaction before the database is closed
MindSift CI & Docker Release / Build, Test in Container & Push (push) Successful in 2m35s

The compaction starts right away in NewServer and could still write to the SQLite files while a
test's temp dir was removed ("directory not empty" on the CI runner, TestWebhookTokenInURLOnlyAPIKey).
Server.Close stops it and waits for a running pass; tests call it before closing the store, and the
server calls it on shutdown before the deferred store.Close. v0.37.1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
sandrodachandClaude Opus 5.5 committed 2026-11-01 13:16:58 +01:00
1 parent 3fbe015c06
commit c56bd4d943
10 files changed
+43 -6

No files matched your search

+15
View File
@@ -7,6 +7,7 @@ import (
"log/slog"
"net/http"
"strings"
"sync"
"time"
"github.com/go-chi/chi/v5"
@@ -41,6 +42,11 @@ type Server struct {
notifyPushHook func(userID string, msg pushMessage) error
pushHTTPClient *http.Client // tests: reach a local push service
push pushKeys
// Background work that touches the database right from the start (compaction); Close stops it
stop chan struct{}
stopOnce sync.Once
bg sync.WaitGroup
}
func NewServer(cfg *config.Config, store *storage.Store, wsHub *ws.Hub, staticFS fs.FS) *Server {
@@ -51,6 +57,7 @@ func NewServer(cfg *config.Config, store *storage.Store, wsHub *ws.Hub, staticFS
staticFS: staticFS,
leases: collab.NewLeaseManager(nil),
notifier: collab.NewNotifier(),
stop: make(chan struct{}),
}
srv.loginGuard = newLoginGuard(!cfg.DisableLoginProtection)
srv.voice = newVoiceService(cfg.Speech.MaxParallel)
@@ -63,6 +70,7 @@ func NewServer(cfg *config.Config, store *storage.Store, wsHub *ws.Hub, staticFS
store.OnTaskCreated = func(th *domain.Thought) {
srv.wsHub.BroadcastToUsers(srv.taskAudience(th), "TASK_CREATED", th)
}
srv.bg.Add(1)
go srv.startCompaction()
if cfg.DemoMode && cfg.DemoResetMinutes > 0 {
go srv.startDemoResetTicker()
@@ -93,6 +101,13 @@ func (s *Server) taskAudience(th *domain.Thought) []string {
return append(s.store.GetTaskListUserIDs(th.TaskListID), th.UserID)
}
// Close stops the background compaction and waits until a running pass has finished, so the database
// can be closed safely afterwards (tests close it right away; a pass still writing would race with that).
func (s *Server) Close() {
s.stopOnce.Do(func() { close(s.stop) })
s.bg.Wait()
}
func (s *Server) Router() http.Handler {
r := chi.NewRouter()