Server: Implement timeout waiting for server to shut down (#33333)

* tests: Undo cleanup in goroutine

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Server: Implement timeout waiting for it to shut down

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
Arve Knudsen
2021-04-30 10:55:59 +02:00
committed by GitHub
parent 492227fa0a
commit ec3d8b590a
4 changed files with 45 additions and 11 deletions
+10 -3
View File
@@ -1,6 +1,7 @@
package main package main
import ( import (
"context"
"errors" "errors"
"flag" "flag"
"fmt" "fmt"
@@ -156,7 +157,9 @@ func executeServer(configFile, homePath, pidFile, packaging string, traceDiagnos
return err return err
} }
go listenToSystemSignals(s) ctx := context.Background()
go listenToSystemSignals(ctx, s)
if err := s.Run(); err != nil { if err := s.Run(); err != nil {
code := s.ExitCode(err) code := s.ExitCode(err)
@@ -179,7 +182,7 @@ func validPackaging(packaging string) string {
return "unknown" return "unknown"
} }
func listenToSystemSignals(s *server.Server) { func listenToSystemSignals(ctx context.Context, s *server.Server) {
signalChan := make(chan os.Signal, 1) signalChan := make(chan os.Signal, 1)
sighupChan := make(chan os.Signal, 1) sighupChan := make(chan os.Signal, 1)
@@ -193,7 +196,11 @@ func listenToSystemSignals(s *server.Server) {
fmt.Fprintf(os.Stderr, "Failed to reload loggers: %s\n", err) fmt.Fprintf(os.Stderr, "Failed to reload loggers: %s\n", err)
} }
case sig := <-signalChan: case sig := <-signalChan:
s.Shutdown(fmt.Sprintf("System signal: %s", sig)) ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
if err := s.Shutdown(ctx, fmt.Sprintf("System signal: %s", sig)); err != nil {
fmt.Fprintf(os.Stderr, "Timed out waiting for server to shut down\n")
}
return return
} }
} }
+12 -6
View File
@@ -222,17 +222,23 @@ func (s *Server) Run() error {
// Shutdown initiates Grafana graceful shutdown. This shuts down all // Shutdown initiates Grafana graceful shutdown. This shuts down all
// running background services. Since Run blocks Shutdown supposed to // running background services. Since Run blocks Shutdown supposed to
// be run from a separate goroutine. // be run from a separate goroutine.
func (s *Server) Shutdown(reason string) { func (s *Server) Shutdown(ctx context.Context, reason string) error {
var err error
s.shutdownOnce.Do(func() { s.shutdownOnce.Do(func() {
s.log.Info("Shutdown started", "reason", reason) s.log.Info("Shutdown started", "reason", reason)
// Call cancel func to stop services. // Call cancel func to stop services.
s.shutdownFn() s.shutdownFn()
// Can introduce termination timeout here if needed over incoming Context, // Wait for server to shut down
// but this will require changing Shutdown method signature to accept context select {
// and return an error - caller can exit with code > 0 then. case <-s.shutdownFinished:
// I.e. sth like server.Shutdown(context.WithTimeout(...), reason). s.log.Debug("Finished waiting for server to shut down")
<-s.shutdownFinished case <-ctx.Done():
s.log.Warn("Timed out while waiting for server to shut down")
err = fmt.Errorf("timeout waiting for shutdown")
}
}) })
return err
} }
// ExitCode returns an exit code for a given error. // ExitCode returns an exit code for a given error.
+14 -1
View File
@@ -4,6 +4,7 @@ import (
"context" "context"
"errors" "errors"
"testing" "testing"
"time"
"github.com/grafana/grafana/pkg/registry" "github.com/grafana/grafana/pkg/registry"
@@ -83,6 +84,8 @@ func TestServer_Run_Error(t *testing.T) {
} }
func TestServer_Shutdown(t *testing.T) { func TestServer_Shutdown(t *testing.T) {
ctx := context.Background()
s := testServer() s := testServer()
services := []*registry.Descriptor{ services := []*registry.Descriptor{
{ {
@@ -100,14 +103,24 @@ func TestServer_Shutdown(t *testing.T) {
services: services, services: services,
} }
ch := make(chan error)
go func() { go func() {
defer close(ch)
// Wait until all services launched. // Wait until all services launched.
for _, svc := range services { for _, svc := range services {
<-svc.Instance.(*testService).started <-svc.Instance.(*testService).started
} }
s.Shutdown("test interrupt") ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
err := s.Shutdown(ctx, "test interrupt")
ch <- err
}() }()
err := s.Run() err := s.Run()
require.NoError(t, err) require.NoError(t, err)
require.Zero(t, s.ExitCode(err)) require.Zero(t, s.ExitCode(err))
err = <-ch
require.NoError(t, err)
} }
+9 -1
View File
@@ -1,6 +1,7 @@
package testinfra package testinfra
import ( import (
"context"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net" "net"
@@ -9,6 +10,7 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"testing" "testing"
"time"
"github.com/grafana/grafana/pkg/infra/fs" "github.com/grafana/grafana/pkg/infra/fs"
"github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/models"
@@ -25,6 +27,8 @@ import (
func StartGrafana(t *testing.T, grafDir, cfgPath string, sqlStore *sqlstore.SQLStore) string { func StartGrafana(t *testing.T, grafDir, cfgPath string, sqlStore *sqlstore.SQLStore) string {
t.Helper() t.Helper()
ctx := context.Background()
origSQLStore := registry.GetService(sqlstore.ServiceName) origSQLStore := registry.GetService(sqlstore.ServiceName)
t.Cleanup(func() { t.Cleanup(func() {
registry.Register(origSQLStore) registry.Register(origSQLStore)
@@ -58,7 +62,11 @@ func StartGrafana(t *testing.T, grafDir, cfgPath string, sqlStore *sqlstore.SQLS
} }
}() }()
t.Cleanup(func() { t.Cleanup(func() {
go server.Shutdown("test cleanup") ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
if err := server.Shutdown(ctx, "test cleanup"); err != nil {
t.Error("Timed out waiting on server to shut down")
}
}) })
// Wait for Grafana to be ready // Wait for Grafana to be ready