-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Verify the ExecSession pid before killing it. #25906
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
Merged
Merged
Changes from all commits
Commits
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
//go:build !windows | ||
|
||
// Package for handling processes and PIDs. | ||
package pidhandle | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/shirou/gopsutil/v4/process" | ||
"github.com/sirupsen/logrus" | ||
"golang.org/x/sys/unix" | ||
) | ||
|
||
// PIDHandle defines an interface for working with operating system processes | ||
// in a reliable way. OS-specific implementations include additional logic | ||
// to try to ensure that operations (e.g., sending signals) are performed | ||
// on the exact same process that was originally referenced when the PIDHandle | ||
// was created via NewPIDHandle or NewPIDHandleFromString. | ||
// | ||
// This prevents accidental interaction with a different process in scenarios | ||
// where the original process has exited and its PID has been reused by | ||
// the system for an unrelated process. | ||
type PIDHandle interface { | ||
// Returns the PID associated with this PIDHandle. | ||
PID() int | ||
// Releases the PIDHandle resources. | ||
Close() error | ||
// Sends the signal to process. | ||
Kill(signal unix.Signal) error | ||
// Returns true in case the process is still alive. | ||
IsAlive() (bool, error) | ||
// Returns a serialized representation of the PIDHandle. | ||
// This string can be passed to NewPIDHandleFromString to recreate | ||
// a PIDHandle that reliably refers to the same process as the original. | ||
String() (string, error) | ||
} | ||
|
||
// The pidData value used when no process with this PID exists when creating | ||
// the PIDHandle. | ||
const noSuchProcessID = "no-proc" | ||
jankaluza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// The pidData prefix used when only the process start time (creation time) | ||
// is supported when creating the PIDHandle to uniquely identify the process. | ||
const startTimePrefix = "start-time:" | ||
|
||
type pidHandle struct { | ||
pid int | ||
pidData string | ||
} | ||
|
||
// Returns the PID. | ||
func (h *pidHandle) PID() int { | ||
return h.pid | ||
} | ||
|
||
// Close releases the PIDHandle resource. | ||
func (h *pidHandle) Close() error { | ||
// No resources for the default PIDHandle implementation. | ||
return nil | ||
} | ||
|
||
// Sends the signal to process. | ||
func (h *pidHandle) Kill(signal unix.Signal) error { | ||
if h.pidData == noSuchProcessID { | ||
// The process did not exist when we created the PIDHandle, so return | ||
// ESRCH error. | ||
return unix.ESRCH | ||
} | ||
|
||
// Get the start-time of the process and check if it is the same as | ||
// the one we store in pidData. If it is not, we know that the PID | ||
// has been recycled and return ESRCH error. | ||
startTime, found := strings.CutPrefix(h.pidData, startTimePrefix) | ||
if found { | ||
p, err := process.NewProcess(int32(h.pid)) | ||
if err != nil { | ||
if err == process.ErrorProcessNotRunning { | ||
return unix.ESRCH | ||
} | ||
return err | ||
} | ||
|
||
ctime, err := p.CreateTime() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if strconv.FormatInt(ctime, 10) != startTime { | ||
return unix.ESRCH | ||
} | ||
} | ||
|
||
return unix.Kill(h.pid, signal) | ||
} | ||
|
||
// Returns true in case the process is still alive. | ||
func (h *pidHandle) IsAlive() (bool, error) { | ||
err := h.Kill(0) | ||
if err != nil { | ||
if err == unix.ESRCH { | ||
return false, nil | ||
} | ||
return false, err | ||
} | ||
return true, nil | ||
} | ||
|
||
// Returns a serialized representation of the PIDHandle. | ||
// This string can be passed to NewPIDHandleFromString to recreate | ||
// a PIDHandle that reliably refers to the same process as the original. | ||
func (h *pidHandle) String() (string, error) { | ||
if len(h.pidData) != 0 { | ||
return h.pidData, nil | ||
} | ||
|
||
// Get the start-time of the process and return it as string. | ||
p, err := process.NewProcess(int32(h.pid)) | ||
if err != nil { | ||
if err == process.ErrorProcessNotRunning { | ||
return noSuchProcessID, nil | ||
} | ||
return "", err | ||
} | ||
|
||
ctime, err := p.CreateTime() | ||
if err != nil { | ||
// The process existed, but we cannot get its start-time. There is | ||
// either an issue with getting it, or the process terminated in the | ||
// mean-time. We have no way to find out what actually happened, so | ||
// in this case, we just fallback to an empty string. This will mean | ||
// that Kill or IsAlive might kill wrong process in rare situation | ||
// when CreateTime() failed for different reason than the process | ||
// terminated... | ||
logrus.Debugf("Getting CreateTime for process (%d) failed: %v", h.pid, err) | ||
return "", nil | ||
} | ||
|
||
return startTimePrefix + strconv.FormatInt(ctime, 10), nil | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.