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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

lowc1012
Copy link
Contributor

@lowc1012 lowc1012 commented Feb 28, 2025

Tracking issue

#6274

Why are the changes needed?

To handle SIGTERM properly, ensure cleanup of resources and provide smooth user experience

What changes were proposed in this pull request?

Implement graceful shutdown for flyteadmin servers

How was this patch tested?

Labels

Please add one or more of the following labels to categorize your PR:

  • added: For new features.
  • changed: For changes in existing functionality.
  • deprecated: For soon-to-be-removed features.
  • removed: For features being removed.
  • fixed: For any bug fixed.
  • security: In case of vulnerabilities

This is important to improve the readability of release notes.

Setup process

Screenshots

Check all the applicable boxes

  • I updated the documentation accordingly.
  • All new and existing tests passed.
  • All commits are signed-off.

Related PRs

Docs link

Summary by Bito

This PR implements graceful shutdown mechanisms for flyteadmin servers by introducing configurable timeout settings (replacing hardcoded 10-second values), adding signal handling and timeout-based contexts. It enhances error handling in gRPC server startup, updates configuration files with command-line flag support, and ensures proper termination of both HTTP and gRPC servers to prevent resource leaks and improve service reliability.

Unit tests added: False

Estimated effort to review (1-5, lower is better): 1

@flyte-bot
Copy link
Collaborator

Code Review Agent Run Status

  • Limitations and other issues: ❌ Failure - The AI Code Review Agent skipped reviewing this change because it is configured to exclude certain pull requests based on the source/target branch or the pull request status. You can change the settings here, or contact the agent instance creator at [email protected].

Copy link

codecov bot commented Feb 28, 2025

Codecov Report

Attention: Patch coverage is 2.32558% with 42 lines in your changes missing coverage. Please review.

Project coverage is 58.47%. Comparing base (588ceec) to head (6d76383).
Report is 34 commits behind head on master.

Files with missing lines Patch % Lines
flyteadmin/pkg/server/service.go 0.00% 42 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6289      +/-   ##
==========================================
+ Coverage   50.37%   58.47%   +8.09%     
==========================================
  Files        1170      937     -233     
  Lines       92851    71141   -21710     
==========================================
- Hits        46774    41599    -5175     
+ Misses      41931    26389   -15542     
+ Partials     4146     3153     -993     
Flag Coverage Δ
unittests-datacatalog 59.03% <ø> (+7.44%) ⬆️
unittests-flyteadmin 56.21% <2.32%> (+4.33%) ⬆️
unittests-flytecopilot 30.99% <ø> (ø)
unittests-flytectl 64.70% <ø> (+2.37%) ⬆️
unittests-flyteidl 76.12% <ø> (?)
unittests-flyteplugins 61.00% <ø> (+6.86%) ⬆️
unittests-flytepropeller 54.82% <ø> (+11.95%) ⬆️
unittests-flytestdlib 64.04% <ø> (+8.70%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lowc1012 lowc1012 changed the title Flyte issue 6274 fix: flyteadmin doesn't shutdown servers gracefully Feb 28, 2025
@eapolinario
Copy link
Contributor

@lowc1012 , is this ready for review?

@lowc1012 lowc1012 marked this pull request as ready for review March 11, 2025 13:13
@flyte-bot
Copy link
Collaborator

flyte-bot commented Mar 11, 2025

Code Review Agent Run #55c786

Actionable Suggestions - 3
  • flyteadmin/pkg/server/service.go - 3
Filtered by Review Rules

Bito filtered these suggestions based on rules created automatically for your feedback. Manage rules.

  • flyteadmin/pkg/server/service.go - 2
Review Details
  • Files reviewed - 1 · Commit Range: 490e289..a264c15
    • flyteadmin/pkg/server/service.go
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful

AI Code Review powered by Bito Logo

@flyte-bot
Copy link
Collaborator

flyte-bot commented Mar 11, 2025

Changelist by Bito

This pull request implements the following key changes.

Key Change Files Impacted
Bug Fix - Bug Fix: Update configuration for graceful shutdown

config.go - Refactored ServerConfig, aligning field definitions and adding gracefulShutdownTimeoutSeconds.

serverconfig_flags.go - Introduced a new command flag for graceful shutdown timeout support.

serverconfig_flags_test.go - Enhanced tests to validate the new graceful shutdown flag behavior.

Bug Fix - Bug Fix: Implement graceful shutdown in server operations

service.go - Refactored server logic by adding signal handling, timeout contexts, and concurrent routines to ensure both HTTP and gRPC servers shutdown gracefully.

Comment on lines +558 to +563
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)
}
}()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential race condition in server startup

The HTTP/2 server is now started in a goroutine, but the function immediately proceeds to wait for shutdown signals. This could lead to a race condition where the shutdown sequence begins before the server is fully initialized. Consider adding a small delay or a readiness check before proceeding to the shutdown logic.

Code suggestion
Check the AI-generated fix before applying
 @@ -558,6 +558,13 @@
  	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)
  		}
  	}()
 +
 +	// Give the server a moment to start before proceeding to shutdown logic
 +	time.Sleep(100 * time.Millisecond)
 +
 +	// Log that the server has started
 +	logger.Infof(ctx, "HTTP/2 Server started successfully on %s", cfg.GetHostAddress())
 

Code Review Run #55c786


Should Bito avoid suggestions like this for future reviews? (Manage Rules)

  • Yes, avoid them

Signed-off-by: Ryan Lo <[email protected]>
@flyte-bot
Copy link
Collaborator

flyte-bot commented Mar 11, 2025

Code Review Agent Run #e9c2cd

Actionable Suggestions - 0
Review Details
  • Files reviewed - 1 · Commit Range: a264c15..d1714f6
    • flyteadmin/pkg/server/service.go
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

Refer to the documentation for additional commands.

Configuration

This repository uses code_review_bito You can customize the agent settings here or contact your Bito workspace admin at [email protected].

Documentation & Help

AI Code Review powered by Bito Logo

@lowc1012
Copy link
Contributor Author

@eapolinario please help review it, thank you

@Sovietaced
Copy link
Contributor

@eapolinario please help review it, thank you

I was the original one that filed this issue for myself to take a look at later so I can review it a bit.

}()

// Gracefully shutdown the servers
sigCh := make(chan os.Signal, 1)
Copy link
Contributor

@Sovietaced Sovietaced Mar 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to have two signal listeners? Should just be able to use one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you mean just SIGINT or SIGTERM?

Copy link
Contributor

@Sovietaced Sovietaced Mar 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean that in this diff you have two signal channels (one for grpc and one for http). You only need one to know whether or not the app is shutting down.

grpcServer.GracefulStop()
grpcCancel()
}()
<-grpcShutdownCtx.Done()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Waiting for graceful shutdown doesn't necessarily mean that it completed successful graceful shutdown. We should follow up with a more forceful stop per the documentation/examples: https://github.com/grpc/grpc-go/blob/master/examples/features/gracefulstop/server/main.go#L93-L99

@Sovietaced
Copy link
Contributor

Part of my thinking when handling graceful shutdown was not just the servers but also draining any buffers, specifically for cloud events so that we minimize notifications that are dropped because they are still buffered in memory.

@Sovietaced
Copy link
Contributor

Sovietaced commented Mar 14, 2025

Part of my thinking when handling graceful shutdown was not just the servers but also draining any buffers, specifically for cloud events so that we minimize notifications that are dropped because they are still buffered in memory.

I think the buffers might have been in my company's custom fork but it would still be good to look for anything else that can be drained or shutdown via context after the servers are shutdown.

For example, there are a number of things that get kicked off in goroutines here liked the scheduled execution executor: https://github.com/lowc1012/flyte/blob/master/flyteadmin/pkg/rpc/adminservice/base.go

Signed-off-by: Ryan Lo <[email protected]>
@flyte-bot
Copy link
Collaborator

flyte-bot commented Mar 14, 2025

Code Review Agent Run #138eb1

Actionable Suggestions - 0
Review Details
  • Files reviewed - 1 · Commit Range: d1714f6..cdb0383
    • flyteadmin/pkg/server/service.go
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

Refer to the documentation for additional commands.

Configuration

This repository uses code_review_bito You can customize the agent settings here or contact your Bito workspace admin at [email protected].

Documentation & Help

AI Code Review powered by Bito Logo

time.Sleep(1 * time.Second)

// force to shut down servers after 10 seconds
timer := time.AfterFunc(10 * time.Second, func() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May want to make this timeout configurable

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like configure the timeout in flyteadmin_config.yaml?

server:
  forceShutdownTimeoutSec: 10
  

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. Something along those lines.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

@flyte-bot
Copy link
Collaborator

flyte-bot commented Mar 14, 2025

Code Review Agent Run #4c1dda

Actionable Suggestions - 0
Filtered by Review Rules

Bito filtered these suggestions based on rules created automatically for your feedback. Manage rules.

  • flyteadmin/pkg/server/service.go - 2
    • Consider validating graceful shutdown timeout value · Line 578-578
    • Consider validating the shutdown timeout value · Line 444-444
Review Details
  • Files reviewed - 4 · Commit Range: cdb0383..6d76383
    • flyteadmin/pkg/config/config.go
    • flyteadmin/pkg/config/serverconfig_flags.go
    • flyteadmin/pkg/config/serverconfig_flags_test.go
    • flyteadmin/pkg/server/service.go
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

Refer to the documentation for additional commands.

Configuration

This repository uses code_review_bito You can customize the agent settings here or contact your Bito workspace admin at [email protected].

Documentation & Help

AI Code Review powered by Bito Logo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants