Merge pull request #55175 from sttts/sttts-lock-registry-cleanup

Automatic merge from submit-queue (batch tested with PRs 53592, 52562, 55175, 55213). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

apiserver: protect registry cleanup against concurrent access

The mechanism uses global state during testing. Better protect it and fail early when a race is happening.

Related to https://github.com/kubernetes/kubernetes/issues/54095
This commit is contained in:
Kubernetes Submit Queue 2017-11-07 11:21:23 -08:00 committed by GitHub
commit 47d1973c18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -17,6 +17,8 @@ limitations under the License.
package registry
import (
"sync"
"github.com/golang/glog"
"k8s.io/apimachinery/pkg/runtime"
@ -82,13 +84,23 @@ func StorageWithCacher(capacity int) generic.StorageDecorator {
// only from the test harness, so Register/Cleanup will be
// no-op at runtime.
var cleanupLock sync.Mutex
var cleanup []func() = nil
func TrackStorageCleanup() {
cleanupLock.Lock()
defer cleanupLock.Unlock()
if cleanup != nil {
panic("Conflicting storage tracking")
}
cleanup = make([]func(), 0)
}
func RegisterStorageCleanup(fn func()) {
cleanupLock.Lock()
defer cleanupLock.Unlock()
if cleanup == nil {
return
}
@ -96,11 +108,12 @@ func RegisterStorageCleanup(fn func()) {
}
func CleanupStorage() {
if cleanup == nil {
return
}
for _, d := range cleanup {
cleanupLock.Lock()
old := cleanup
cleanup = nil
cleanupLock.Unlock()
for _, d := range old {
d()
}
cleanup = nil
}