-
Notifications
You must be signed in to change notification settings - Fork 585
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 5 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,30 @@ | ||
//go:build !windows | ||
// +build !windows | ||
|
||
package transport | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/shirou/gopsutil/v3/process" | ||
) | ||
|
||
func killByPid(pid int) error { | ||
proc, err := process.NewProcess(int32(pid)) | ||
if err != nil { | ||
return err | ||
} | ||
// first send SIGTERM | ||
err = proc.Terminate() | ||
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. How about using 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. On linux/unix, we can simple use process.Signal() to send signal. |
||
if err != nil { | ||
fmt.Printf("Failed to send SIGTERM to pid %d: %v\n", pid, err) | ||
} | ||
|
||
_, err = proc.Status() | ||
if err == nil { | ||
// kill ok | ||
return nil | ||
} | ||
// send SIGKILL | ||
return proc.Kill() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//go:build windows | ||
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 recommend to move it to a util package 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. Move to the top level package? 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. maybe |
||
// +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. |
||
) | ||
|
||
func killByPid(pid int) error { | ||
proc, err := process.NewProcess(int32(pid)) | ||
if err != nil { | ||
return err | ||
} | ||
// 获取所有子进程(递归) | ||
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. Please translate to English for consistency 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. translated. |
||
children, err := proc.Children() | ||
if err == nil { | ||
for _, child := range children { | ||
err = killByPid(int(child.Pid)) // 递归杀子进程 | ||
if err != nil { | ||
fmt.Printf("Failed to kill pid %d: %v\n", child.Pid, err) | ||
} | ||
} | ||
} | ||
|
||
// 杀掉当前进程 | ||
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 | ||
} |
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'd rather not introduce a new dependency if we can help it
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.
Yeah, but handling subprocess termination on Windows is tricky and can get messy. May be it's a trade off.
Uh oh!
There was an error while loading. Please reload this page.
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.
for OS windows, how about utilizing command
taskkill
to kill a process and its subprocess:pid := fmt.Sprint(proc.Pid)
cmd := exec.Command("taskkill", "/T", "/PID", pid)
cmd = exec.Command("taskkill", "/F", "/T", "/PID", pid)
If it works, we may not need to introduce this dependency
https://learn.microsoft.com/zh-cn/windows-server/administration/windows-commands/taskkill
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.
Good idea @cryo-zd