-
Notifications
You must be signed in to change notification settings - Fork 348
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
Open
Crayzero
wants to merge
7
commits into
mark3labs:main
Choose a base branch
from
Crayzero:feature/add_kill_to_stdio_client
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+170
−2
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
39e39a0
feat(transport/stdio.go): :sparkles: add send sigterm and sigkill to …
2c49442
feat(transport/stdio.go): :sparkles: add send sigterm and sigkill to …
Crayzero 1a5dc5a
fix(close_on_other.go): :bug: fix kill process on linux/unix
Crayzero 90cade4
test(stdio_test.go): :white_check_mark: add test cases for close and …
Crayzero 174e815
Merge branch 'feature/add_kill_to_stdio_client' of https://github.com…
ee3e773
chore(close_on_windows.go): :bulb: update comment to english
b5ef487
refactor(close_on_other.go): :recycle: change signature of killProces…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? |
||
) | ||
|
||
// 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) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
?