statedb: Fix race between Observable and DB stopping #30816
Merged
+7
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Since "Observable" forks a goroutine that is not tied to the lifecycle of the application what may occur is that the "observe" goroutine calls DeleteTracker.Close after DB.Stop, leading to:
While it would be ideal that goroutines created by statedb would be tied to its lifecycle and thus Stop() could wait for e.g. all observable goroutines to be finished, it's not enough as DeleteTracker's may be created outside and stopped after DB. Thus this commit changes the logic to make it safe to call DeleteTracker.Close() even after the DB has stopped.
The fix was validated by adding a "defer time.Sleep(100*time.Millisecond)" to observable.go before the "dt.Close()" to force it to run after DB.Stop, with it failing with "send on closed channel" before fix and passing after.
As a future follow-up it would make sense to use a Hive job group tied to DB's lifecycle to make sure all goroutines are cleaned up (this follow-up will be done against the cilium/statedb repo as it's being moved there). The fix in this commit is already part of cilium/statedb repo and does not need to be ported.
Fixes: #30806
Fixes: 23b0492 ("statedb2: StateDB v2.0 with per-table locks and deletion tracking")