From 4362d613f243a02558f03e90b8fcb58b4c6efb06 Mon Sep 17 00:00:00 2001 From: Lukasz Szaszkiewicz Date: Wed, 10 Jun 2020 14:05:24 +0200 Subject: [PATCH] genericapiserver library must wait for server.Shutdown --- .../k8s.io/apiserver/pkg/server/genericapiserver.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/staging/src/k8s.io/apiserver/pkg/server/genericapiserver.go b/staging/src/k8s.io/apiserver/pkg/server/genericapiserver.go index ed7d50b7a2c..e0bbc31671c 100644 --- a/staging/src/k8s.io/apiserver/pkg/server/genericapiserver.go +++ b/staging/src/k8s.io/apiserver/pkg/server/genericapiserver.go @@ -330,7 +330,7 @@ func (s preparedGenericAPIServer) Run(stopCh <-chan struct{}) error { }() // close socket after delayed stopCh - err := s.NonBlockingRun(delayedStopCh) + stoppedCh, err := s.NonBlockingRun(delayedStopCh) if err != nil { return err } @@ -345,6 +345,8 @@ func (s preparedGenericAPIServer) Run(stopCh <-chan struct{}) error { // wait for the delayed stopCh before closing the handler chain (it rejects everything after Wait has been called). <-delayedStopCh + // wait for stoppedCh that is closed when the graceful termination (server.Shutdown) is finished. + <-stoppedCh // Wait for all requests to finish, which are bounded by the RequestTimeout variable. s.HandlerChainWaitGroup.Wait() @@ -354,7 +356,8 @@ func (s preparedGenericAPIServer) Run(stopCh <-chan struct{}) error { // NonBlockingRun spawns the secure http server. An error is // returned if the secure port cannot be listened on. -func (s preparedGenericAPIServer) NonBlockingRun(stopCh <-chan struct{}) error { +// The returned channel is closed when the (asynchronous) termination is finished. +func (s preparedGenericAPIServer) NonBlockingRun(stopCh <-chan struct{}) (<-chan struct{}, error) { // Use an stop channel to allow graceful shutdown without dropping audit events // after http server shutdown. auditStopCh := make(chan struct{}) @@ -363,7 +366,7 @@ func (s preparedGenericAPIServer) NonBlockingRun(stopCh <-chan struct{}) error { // before http server start serving. Otherwise the Backend.ProcessEvents call might block. if s.AuditBackend != nil { if err := s.AuditBackend.Run(auditStopCh); err != nil { - return fmt.Errorf("failed to run the audit backend: %v", err) + return nil, fmt.Errorf("failed to run the audit backend: %v", err) } } @@ -376,7 +379,7 @@ func (s preparedGenericAPIServer) NonBlockingRun(stopCh <-chan struct{}) error { if err != nil { close(internalStopCh) close(auditStopCh) - return err + return nil, err } } @@ -399,7 +402,7 @@ func (s preparedGenericAPIServer) NonBlockingRun(stopCh <-chan struct{}) error { klog.Errorf("Unable to send systemd daemon successful start message: %v\n", err) } - return nil + return stoppedCh, nil } // installAPIResources is a private method for installing the REST storage backing each api groupversionresource