Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: flyteadmin doesn't shutdown servers gracefully #6289

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 43 additions & 6 deletions flyteadmin/pkg/server/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
"fmt"
"net"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"

"github.com/gorilla/handlers"
Expand Down Expand Up @@ -422,11 +425,29 @@
ReadHeaderTimeout: time.Duration(cfg.ReadHeaderTimeoutSeconds) * time.Second,
}

err = server.ListenAndServe()
if err != nil {
return errors.Wrapf(err, "failed to Start HTTP Server")
go func() {
err = server.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
logger.Fatalf(ctx, "Failed to start HTTP Server: %v", err)
}

Check warning on line 432 in flyteadmin/pkg/server/service.go

View check run for this annotation

Codecov / codecov/patch

flyteadmin/pkg/server/service.go#L428-L432

Added lines #L428 - L432 were not covered by tests
}()

// Gracefully shutdown the servers
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
<-sigCh

// Create a context with timeout for the shutdown process
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

if err := server.Shutdown(shutdownCtx); err != nil {
logger.Errorf(ctx, "Failed to shutdown HTTP server: %v", err)

Check warning on line 445 in flyteadmin/pkg/server/service.go

View check run for this annotation

Codecov / codecov/patch

flyteadmin/pkg/server/service.go#L436-L445

Added lines #L436 - L445 were not covered by tests
}

grpcServer.GracefulStop()

logger.Infof(ctx, "Servers gracefully stopped")

Check warning on line 450 in flyteadmin/pkg/server/service.go

View check run for this annotation

Codecov / codecov/patch

flyteadmin/pkg/server/service.go#L448-L450

Added lines #L448 - L450 were not covered by tests
return nil
}

Expand Down Expand Up @@ -534,10 +555,26 @@
ReadHeaderTimeout: time.Duration(cfg.ReadHeaderTimeoutSeconds) * time.Second,
}

err = srv.Serve(tls.NewListener(conn, srv.TLSConfig))
go func() {
err = srv.Serve(tls.NewListener(conn, srv.TLSConfig))
if err != nil && err != http.ErrServerClosed {
logger.Errorf(ctx, "Failed to start HTTP/2 Server: %v", err)
}

Check warning on line 562 in flyteadmin/pkg/server/service.go

View check run for this annotation

Codecov / codecov/patch

flyteadmin/pkg/server/service.go#L558-L562

Added lines #L558 - L562 were not covered by tests
}()

if err != nil {
return errors.Wrapf(err, "failed to Start HTTP/2 Server")
// Gracefully shutdown the servers
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
<-sigCh

// Create a context with timeout for the shutdown process
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

if err := srv.Shutdown(shutdownCtx); err != nil {
logger.Errorf(ctx, "Failed to shutdown HTTP server: %v", err)

Check warning on line 575 in flyteadmin/pkg/server/service.go

View check run for this annotation

Codecov / codecov/patch

flyteadmin/pkg/server/service.go#L566-L575

Added lines #L566 - L575 were not covered by tests
}

logger.Infof(ctx, "Servers gracefully stopped")

Check warning on line 578 in flyteadmin/pkg/server/service.go

View check run for this annotation

Codecov / codecov/patch

flyteadmin/pkg/server/service.go#L578

Added line #L578 was not covered by tests
return nil
}
Loading