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

Improved process termination logic in process_unix.go #19

Draft
wants to merge 1 commit 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
43 changes: 34 additions & 9 deletions utils/process_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

package utils

// Dummy test thing

import (
"errors"
"fmt"
"os"
"syscall"
Expand All @@ -22,23 +25,45 @@ func IsProcessRunning(pid int) bool {
return err == nil
}

// StopProcess stops a process by its PID on Unix-based systems.
func StopProcess(pid int) error {
process, err := os.FindProcess(pid)
if err != nil {
// Check if the process exists
if _, err := os.FindProcess(pid); err != nil {
return fmt.Errorf("could not find process: %w", err)
}

// Send SIGTERM (soft termination)
err = process.Signal(syscall.SIGTERM)
// Send SIGTERM to the process group
err := syscall.Kill(-pid, syscall.SIGTERM)
if err != nil {
return fmt.Errorf("could not terminate process: %w", err)
return fmt.Errorf("could not send SIGTERM to process: %w", err)
}
fmt.Printf("SIGTERM sent to process group %d\n", pid)

// termination might take some time and it will effect the next steps during update, sleep 5 seconds just in case
time.Sleep(5 * time.Second)
// Wait for graceful termination
timeout := time.After(10 * time.Second)
ticker := time.NewTicker(500 * time.Millisecond)
defer ticker.Stop()

return nil
for {
select {
case <-timeout:
// Escalate to SIGKILL if process does not terminate
err = syscall.Kill(-pid, syscall.SIGKILL)
if err != nil {
return fmt.Errorf("could not kill process: %w", err)
}
fmt.Printf("SIGKILL sent to process group %d\n", pid)
time.Sleep(1 * time.Second)
if IsProcessRunning(pid) {
return errors.New("process did not terminate even after SIGKILL")
}
return fmt.Errorf("process did not terminate gracefully; sent SIGKILL")
case <-ticker.C:
if !IsProcessRunning(pid) {
fmt.Printf("Process group %d terminated successfully\n", pid)
return nil
}
}
}
}

func SetFileDescriptorLimit(limit uint64) error {
Expand Down