fix(store): close watcher channels on shutdown

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-03 07:30:50 +00:00
parent a9ab1fd2ee
commit acad59664d
2 changed files with 24 additions and 0 deletions

View file

@ -70,6 +70,16 @@ func TestEvents_Unwatch_Good_Idempotent(t *testing.T) {
storeInstance.Unwatch("g", events)
}
func TestEvents_Close_Good_ClosesWatcherChannels(t *testing.T) {
storeInstance, _ := New(":memory:")
events := storeInstance.Watch("g")
require.NoError(t, storeInstance.Close())
_, open := <-events
assert.False(t, open, "channel should be closed after Close")
}
func TestEvents_Unwatch_Good_NilChannel(t *testing.T) {
storeInstance, _ := New(":memory:")
defer storeInstance.Close()

View file

@ -226,6 +226,20 @@ func (storeInstance *Store) Close() error {
storeInstance.cancelPurge()
}
storeInstance.purgeWaitGroup.Wait()
storeInstance.watchersLock.Lock()
for groupName, registeredEvents := range storeInstance.watchers {
for _, registeredEventChannel := range registeredEvents {
close(registeredEventChannel)
}
delete(storeInstance.watchers, groupName)
}
storeInstance.watchersLock.Unlock()
storeInstance.callbacksLock.Lock()
storeInstance.callbacks = nil
storeInstance.callbacksLock.Unlock()
if storeInstance.database == nil {
return nil
}