-
Notifications
You must be signed in to change notification settings - Fork 602
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
refactor: Enhanced Error Handling and Logging for Process Exit Optimization #1553
base: develop
Are you sure you want to change the base?
Conversation
WalkthroughThe changes focus on the Changes
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Outside diff range and nitpick comments (1)
common/cmd/cmd_app.go (1)
69-81
: Consider adding timeout and observability improvements.While the error handling improvements are good, consider these architectural enhancements:
- Add a timeout for the graceful shutdown period
- Add metrics for tracking process termination success/failure rates
- Consider implementing a more robust shutdown sequence with multiple signals (SIGTERM → wait → SIGKILL)
Example implementation pattern:
func (c *Cmd) gracefulShutdown(timeout time.Duration) error { done := make(chan error, 1) go func() { if err := c.app.Process.Signal(syscall.SIGTERM); err != nil { done <- err return } done <- c.app.Process.Wait() }() select { case err := <-done: return err case <-time.After(timeout): return c.app.Process.Kill() } }Would you like me to create a detailed proposal for implementing these improvements?
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (1)
- common/cmd/cmd_app.go (1 hunks)
🔇 Additional comments (1)
common/cmd/cmd_app.go (1)
63-65
: Good cleanup: Removing commented-out code.Removing inactive code improves maintainability and reduces confusion.
// Attempt graceful shutdown, then fallback to kill if necessary. | ||
if err = c.app.Process.Signal(syscall.SIGTERM); err != nil { | ||
log.Error("failed to send SIGTERM, attempting to kill process", "err", err) | ||
if killErr := c.app.Process.Kill(); killErr != nil { | ||
log.Error("failed to kill process", "kill error", killErr) | ||
} | ||
} else { | ||
// Use Wait to ensure the process terminates gracefully. | ||
if waitErr := c.app.Process.Wait(); waitErr != nil { | ||
log.Error("failed to wait for process termination", "wait error", waitErr) | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Fix missing import and improve error handling structure.
Several issues need to be addressed:
- The
log
package is used but not imported - The error handling structure could be more idiomatic
- There are inconsistent indentation issues
Apply these changes:
import (
"fmt"
"os"
"strings"
"sync/atomic"
"syscall"
"testing"
"time"
+ "log"
"github.com/stretchr/testify/assert"
)
// In WaitExit method:
- // Attempt graceful shutdown, then fallback to kill if necessary.
- if err = c.app.Process.Signal(syscall.SIGTERM); err != nil {
- log.Error("failed to send SIGTERM, attempting to kill process", "err", err)
- if killErr := c.app.Process.Kill(); killErr != nil {
- log.Error("failed to kill process", "kill error", killErr)
- }
- } else {
- // Use Wait to ensure the process terminates gracefully.
- if waitErr := c.app.Process.Wait(); waitErr != nil {
- log.Error("failed to wait for process termination", "wait error", waitErr)
- }
- }
+ // Attempt graceful shutdown, then fallback to kill if necessary.
+ if err = c.app.Process.Signal(syscall.SIGTERM); err != nil {
+ log.Error("failed to send SIGTERM, attempting to kill process", "err", err)
+ if killErr := c.app.Process.Kill(); killErr != nil {
+ log.Error("failed to kill process", "err", err, "kill_err", killErr)
+ return
+ }
+ return
+ }
+
+ // Wait for process to terminate gracefully
+ if err := c.app.Process.Wait(); err != nil {
+ log.Error("failed to wait for process termination", "err", err)
+ return
+ }
Additionally, consider using structured logging with fields consistently across all error messages for better debugging.
Consider using a more robust error handling package like github.com/pkg/errors
to provide stack traces and better error context.
Committable suggestion was skipped due to low confidence.
Purpose or design rationale of this PR
Describe your change. Make sure to answer these three questions: What does this PR do? Why does it do it? How does it do it?
Logic optimization and error handling are more detailed,
PR title
Your PR title must follow conventional commits (as we are doing squash merge for each PR), so it must start with one of the following types:
Deployment tag versioning
Has
tag
incommon/version.go
been updated or have you addedbump-version
label to this PR?Breaking change label
Does this PR have the
breaking-change
label?Summary by CodeRabbit
Bug Fixes
Chores