From 03e6089ce8cb0a0cae67a1c802689319635917f1 Mon Sep 17 00:00:00 2001 From: Clayton Coleman Date: Tue, 17 Jan 2023 15:01:02 -0500 Subject: [PATCH] wait: Use a context implementation for ContextForChannel ContextForChannel uses a goroutine to transform a channel close to a context cancel. However, this exposes a synchronization issue if we want to unify the underlying implementation between contextless and with context - a ConditionFunc that closes the channel today expects the behavior that no subsequent conditions will be invoked (we have a test in wait_test.go TestUntilReturnsImmediately that verifies this expectation). We can't unify the implementation without ensuring this property holds. To do that this commit changes from the goroutine propagation to implementing context.Context and using stopCh as the Done(). We then implement Err() by returning context.Canceled and stub the other methods. Since our context cannot be explicitly cancelled by users, we cease to return the cancelFn and callers that need that behavior must wrap the context as normal. This should be invisible to clients - they would already observe the same behavior from the context, and the existing error behavior of Poll* is preserved (which ignores ctx.Err()). As a side effect, one less goroutine is created making it more efficient. Kubernetes-commit: 95051a63b323081daf8a3fe55a252eb79f0053aa --- pkg/server/options/etcd.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pkg/server/options/etcd.go b/pkg/server/options/etcd.go index 5a1407d1c..0741e2ba6 100644 --- a/pkg/server/options/etcd.go +++ b/pkg/server/options/etcd.go @@ -17,6 +17,7 @@ limitations under the License. package options import ( + "context" "fmt" "net/http" "strconv" @@ -228,8 +229,8 @@ func (s *EtcdOptions) Complete( } if len(s.EncryptionProviderConfigFilepath) != 0 { - ctxTransformers, closeTransformers := wait.ContextForChannel(stopCh) - ctxServer, _ := wait.ContextForChannel(stopCh) // explicitly ignore cancel here because we do not own the server's lifecycle + ctxServer := wait.ContextForChannel(stopCh) + ctxTransformers, closeTransformers := context.WithCancel(ctxServer) encryptionConfiguration, err := encryptionconfig.LoadEncryptionConfig(ctxTransformers, s.EncryptionProviderConfigFilepath, s.EncryptionProviderConfigAutomaticReload) if err != nil {