Commit a246d6f1 authored by Dr. Stefan Schimanski's avatar Dr. Stefan Schimanski

apiserver: protect registry cleanup against concurrent access

parent 21062657
...@@ -17,6 +17,8 @@ limitations under the License. ...@@ -17,6 +17,8 @@ limitations under the License.
package registry package registry
import ( import (
"sync"
"github.com/golang/glog" "github.com/golang/glog"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
...@@ -82,13 +84,23 @@ func StorageWithCacher(capacity int) generic.StorageDecorator { ...@@ -82,13 +84,23 @@ func StorageWithCacher(capacity int) generic.StorageDecorator {
// only from the test harness, so Register/Cleanup will be // only from the test harness, so Register/Cleanup will be
// no-op at runtime. // no-op at runtime.
var cleanupLock sync.Mutex
var cleanup []func() = nil var cleanup []func() = nil
func TrackStorageCleanup() { func TrackStorageCleanup() {
cleanupLock.Lock()
defer cleanupLock.Unlock()
if cleanup != nil {
panic("Conflicting storage tracking")
}
cleanup = make([]func(), 0) cleanup = make([]func(), 0)
} }
func RegisterStorageCleanup(fn func()) { func RegisterStorageCleanup(fn func()) {
cleanupLock.Lock()
defer cleanupLock.Unlock()
if cleanup == nil { if cleanup == nil {
return return
} }
...@@ -96,11 +108,12 @@ func RegisterStorageCleanup(fn func()) { ...@@ -96,11 +108,12 @@ func RegisterStorageCleanup(fn func()) {
} }
func CleanupStorage() { func CleanupStorage() {
if cleanup == nil { cleanupLock.Lock()
return old := cleanup
} cleanup = nil
for _, d := range cleanup { cleanupLock.Unlock()
for _, d := range old {
d() d()
} }
cleanup = nil
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment