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

refactor: Enhanced Error Handling and Logging for Process Exit Optimization #1553

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
20 changes: 18 additions & 2 deletions common/cmd/cmd_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,25 @@ func (c *Cmd) WaitExit() {
// should use `_ = c.app.Process.Wait()` here, but we have some bugs in coordinator's graceful exit,
// so we use `Kill` as a temp workaround. And since `WaitExit` is only used in integration tests, so
// it won't really affect our functionalities.
if err = c.app.Process.Signal(syscall.SIGTERM); err != nil {
_ = c.app.Process.Kill()
//if err = c.app.Process.Signal(syscall.SIGTERM); err != nil {
// _ = c.app.Process.Kill()
//}
// Send interrupt signal.


// 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)
}
}

Comment on lines +69 to +81
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

⚠️ Potential issue

Fix missing import and improve error handling structure.

Several issues need to be addressed:

  1. The log package is used but not imported
  2. The error handling structure could be more idiomatic
  3. 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.

}

// Interrupt send interrupt signal.
Expand Down