Skip to content

Commit cb7d9ac

Browse files
ispeakc0dechinmaym07
authored andcommitted
fix(stress): kill the stress process for abort (#569)
* fix(stress): kill the stress process for abort Signed-off-by: Shubham Chaudhary <[email protected]> Signed-off-by: chinmaym07 <[email protected]>
1 parent d07355a commit cb7d9ac

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

chaoslib/litmus/stress-chaos/helper/stress-helper.go

+14-9
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ var (
4545
const (
4646
// ProcessAlreadyFinished contains error code when process is finished
4747
ProcessAlreadyFinished = "os: process already finished"
48+
// ProcessAlreadyKilled contains error code when process is already killed
49+
ProcessAlreadyKilled = "no such process"
4850
)
4951

5052
// Helper injects the stress chaos
@@ -125,6 +127,8 @@ func prepareStressChaos(experimentsDetails *experimentTypes.ExperimentDetails, c
125127

126128
// launch the stress-ng process on the target container in paused mode
127129
cmd := exec.Command("/bin/bash", "-c", stressCommand)
130+
// enables the process group id
131+
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
128132
var buf bytes.Buffer
129133
cmd.Stdout = &buf
130134
err = cmd.Start()
@@ -183,16 +187,18 @@ func prepareStressChaos(experimentsDetails *experimentTypes.ExperimentDetails, c
183187
err, ok := err.(*exec.ExitError)
184188
if ok {
185189
status := err.Sys().(syscall.WaitStatus)
186-
if status.Signaled() && status.Signal() == syscall.SIGTERM {
190+
if status.Signaled() && status.Signal() == syscall.SIGKILL {
187191
// wait for the completion of abort handler
188192
time.Sleep(10 * time.Second)
189-
return errors.Errorf("process stopped with SIGTERM signal")
193+
return errors.Errorf("process stopped with SIGKILL signal")
190194
}
191195
}
192196
return errors.Errorf("process exited before the actual cleanup, err: %v", err)
193197
}
194198
log.Info("[Info]: Chaos injection completed")
195-
terminateProcess(cmd.Process.Pid)
199+
if err := terminateProcess(cmd.Process.Pid); err != nil {
200+
return err
201+
}
196202
if err = result.AnnotateChaosResult(resultDetails.Name, chaosDetails.ChaosNamespace, "reverted", "pod", experimentsDetails.TargetPods); err != nil {
197203
return err
198204
}
@@ -203,12 +209,11 @@ func prepareStressChaos(experimentsDetails *experimentTypes.ExperimentDetails, c
203209

204210
//terminateProcess will remove the stress process from the target container after chaos completion
205211
func terminateProcess(pid int) error {
206-
process, err := os.FindProcess(pid)
207-
if err != nil {
208-
return errors.Errorf("unreachable path, err: %v", err)
209-
}
210-
if err = process.Signal(syscall.SIGTERM); err != nil && err.Error() != ProcessAlreadyFinished {
211-
return errors.Errorf("error while killing process, err: %v", err)
212+
if err := syscall.Kill(-pid, syscall.SIGKILL); err != nil {
213+
if strings.Contains(err.Error(), ProcessAlreadyKilled) || strings.Contains(err.Error(), ProcessAlreadyFinished) {
214+
return nil
215+
}
216+
return err
212217
}
213218
log.Info("[Info]: Stress process removed successfully")
214219
return nil

0 commit comments

Comments
 (0)