fix(hub): stop launching system updaters after manager shutdown (#1950) (#1951)

The staggered Initialize goroutine kept calling AddSystem after the hub
had been torn down, which spawned StartUpdater goroutines that later
dereferenced a nil DB inside PocketBase. Track a manager-level context
that RemoveAllSystems cancels, abort the staggered loop on shutdown,
and refuse AddSystem once the manager is stopped.
This commit is contained in:
Yvan Wang
2026-08-18 01:59:42 +08:00
committed by GitHub
parent c1c1cd1bcb
commit 63959694de
3 changed files with 42 additions and 1 deletions

View File

@@ -3,9 +3,11 @@
package systems
import (
"context"
"testing"
"github.com/henrygd/beszel/internal/entities/system"
"github.com/pocketbase/pocketbase/tools/store"
)
func TestCombinedData_MigrateDeprecatedFields(t *testing.T) {
@@ -157,3 +159,20 @@ func TestCombinedData_MigrateDeprecatedFields(t *testing.T) {
}
})
}
// TestAddSystemRefusedAfterShutdown verifies that AddSystem returns an error
// once the manager has been shut down, so the staggered Initialize starter
// cannot spawn updater goroutines against a torn-down hub. See #1950.
func TestAddSystemRefusedAfterShutdown(t *testing.T) {
sm := &SystemManager{systems: store.New(map[string]*System{})}
sm.ctx, sm.cancel = context.WithCancel(context.Background())
sm.cancel()
err := sm.AddSystem(&System{Id: "sys", Host: "127.0.0.1"})
if err == nil {
t.Fatalf("AddSystem returned nil error after shutdown")
}
if sm.systems.Has("sys") {
t.Fatalf("system was added to store after shutdown")
}
}