-
Notifications
You must be signed in to change notification settings - Fork 617
Feature/add kill to stdio client #183
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
base: main
Are you sure you want to change the base?
Changes from all commits
39e39a0
2c49442
1a5dc5a
90cade4
174e815
ee3e773
b5ef487
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
//go:build !windows | ||
// +build !windows | ||
|
||
package transport | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"strings" | ||
"syscall" | ||
"time" | ||
) | ||
|
||
// killprocess kills the process on non-windows platforms. | ||
func killProcess(proc *os.Process) error { | ||
err := proc.Signal(syscall.SIGTERM) | ||
if err != nil { | ||
fmt.Printf("Failed to send SIGTERM to pid %d: %v\n", proc.Pid, err) | ||
} | ||
// wait for a short time to allow the process to terminate gracefully | ||
time.Sleep(200 * time.Millisecond) | ||
// check if the process is still running | ||
if err := proc.Signal(syscall.Signal(0)); err != nil { | ||
if errors.Is(err, os.ErrProcessDone) { | ||
return nil | ||
} | ||
// check if the process is gone | ||
// on some platforms, this may return "no such process" if the process is already gone | ||
if strings.Contains(err.Error(), "no such process") { | ||
return nil | ||
} | ||
} | ||
|
||
// if the process is still running, kill it | ||
return proc.Kill() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
//go:build windows | ||
// +build windows | ||
|
||
package transport | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/shirou/gopsutil/v3/process" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still don't like this dep. Can you justify why it is actually needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think @cryo-zd gave a good suggestion in #183 (comment) on how we might be able to avoid it. |
||
) | ||
|
||
// killByPid kills the process by pid on windows. | ||
// It kills all subprocesses recursively. | ||
func killByPid(pid int) error { | ||
proc, err := process.NewProcess(int32(pid)) | ||
if err != nil { | ||
return err | ||
} | ||
// get all subprocess recursively | ||
children, err := proc.Children() | ||
if err == nil { | ||
for _, child := range children { | ||
err = killByPid(int(child.Pid)) // kill all subprocesses | ||
if err != nil { | ||
fmt.Printf("Failed to kill pid %d: %v\n", child.Pid, err) | ||
} | ||
} | ||
} | ||
|
||
// kill current process | ||
p, err := os.FindProcess(int(pid)) | ||
if err == nil { | ||
// windows does not support SIGTERM, so we just use Kill() | ||
err = p.Kill() | ||
if err != nil { | ||
fmt.Printf("Failed to kill pid %d: %v\n", pid, err) | ||
} | ||
} | ||
return err | ||
} | ||
|
||
// KillProcess kills the process on windows. | ||
func killProcess(p *os.Process) error { | ||
if p == nil { | ||
return nil | ||
} | ||
return killByPid(p.Pid) | ||
} |
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.
I recommend to move it to a util package
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.
Move to the top level package?
I think it's a pretty simple code snippet.
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.
maybe
client/transport/util
?