fix: stop store-bound workers and reconcile settled repair fixtures

This commit is contained in:
2026-10-20 16:19:21 +01:00
parent fdf0369ede
commit 4e35da7473
5 changed files with 61 additions and 2 deletions
+13 -1
View File
@@ -24,6 +24,8 @@ type Store struct {
mu sync.Mutex
runnerLock *os.File
runnerClaimed bool
done chan struct{}
closeOnce sync.Once
}
// openDB opens one SQLite connection pool for a DSN. Every open path shares it
@@ -51,7 +53,7 @@ CREATE TABLE IF NOT EXISTS submissions (id INTEGER PRIMARY KEY, name TEXT NOT NU
db.Close()
return nil, err
}
s := &Store{db: db, dir: filepath.Dir(path), path: path}
s := &Store{db: db, dir: filepath.Dir(path), path: path, done: make(chan struct{})}
if err := s.initRuns(); err != nil {
db.Close()
return nil, err
@@ -76,6 +78,11 @@ CREATE TABLE IF NOT EXISTS submissions (id INTEGER PRIMARY KEY, name TEXT NOT NU
}
func (s *Store) Close() error {
s.closeOnce.Do(func() {
if s.done != nil {
close(s.done)
}
})
err := s.db.Close()
s.mu.Lock()
defer s.mu.Unlock()
@@ -86,6 +93,11 @@ func (s *Store) Close() error {
return err
}
// Done lets runtime workers stop when an embedded caller closes its store even
// if the caller supplied a background context. It is not a shutdown substitute
// for active execution: callers must still cancel and drain runs before Close.
func (s *Store) Done() <-chan struct{} { return s.done }
// OpenReadOnly opens an existing database without running schema DDL or
// changing journal mode, so a live database can be queried read-only while its
// server keeps writing. It fails when the file does not exist. It is used by
+2
View File
@@ -354,6 +354,8 @@ func (r *runner) resumeRunNotifications() {
select {
case <-r.ctx.Done():
return
case <-r.store.Done():
return
case <-ticker.C:
r.deliverRunNotifications(time.Now())
}
+2
View File
@@ -19,6 +19,8 @@ func (r *runner) scheduleLoop() {
select {
case <-r.ctx.Done():
return
case <-r.store.Done():
return
case now := <-ticker.C:
if err := r.scheduleTick(now, false); err != nil {
log.Printf("scheduler: %v", err)
+12 -1
View File
@@ -100,11 +100,22 @@ func TestRuntimeRepairRejectsUnrecordedAndActiveVisits(t *testing.T) {
h, _, id := sessionExportRunner(t, "v2", func(stage.SessionExportRequest) (stage.SessionExportResult, error) {
return stage.SessionExportResult{}, errors.New("unavailable")
})
for _, sequence := range []int{0, 1, 2} {
for _, sequence := range []int{0, 2} {
if _, err := h.runner.repairSessionExport(context.Background(), id, sequence); err == nil {
t.Fatalf("invalid visit %d accepted", sequence)
}
}
// The shared fixture now confirms executor cleanup, as the runtime should.
// Explicitly model an in-flight executor for the active-visit repair case.
h.runner.mu.Lock()
h.runner.active[id] = activeRun{cancel: func() {}}
h.runner.mu.Unlock()
if _, err := h.runner.repairSessionExport(context.Background(), id, 1); err == nil {
t.Fatal("active visit accepted for export repair")
}
h.runner.mu.Lock()
delete(h.runner.active, id)
h.runner.mu.Unlock()
}
func TestCLIRepairSessionExportUsesRuntimeProtocol(t *testing.T) {
+32
View File
@@ -0,0 +1,32 @@
package server
import (
"context"
"path/filepath"
"testing"
"time"
"git.a-wan.net/alan/pipelines/internal/pipeline"
)
func TestNotificationWorkerStopsWhenEmbeddedStoreCloses(t *testing.T) {
store, err := pipeline.Open(filepath.Join(t.TempDir(), "workers.db"))
if err != nil {
t.Fatal(err)
}
r, err := newRunner(context.Background(), store)
if err != nil {
store.Close()
t.Fatal(err)
}
if err := store.Close(); err != nil {
t.Fatal(err)
}
done := make(chan struct{})
go func() { r.schedulerWG.Wait(); close(done) }()
select {
case <-done:
case <-time.After(time.Second):
t.Fatal("notification worker outlived its store")
}
}