45
45
const (
46
46
// ProcessAlreadyFinished contains error code when process is finished
47
47
ProcessAlreadyFinished = "os: process already finished"
48
+ // ProcessAlreadyKilled contains error code when process is already killed
49
+ ProcessAlreadyKilled = "no such process"
48
50
)
49
51
50
52
// Helper injects the stress chaos
@@ -125,6 +127,8 @@ func prepareStressChaos(experimentsDetails *experimentTypes.ExperimentDetails, c
125
127
126
128
// launch the stress-ng process on the target container in paused mode
127
129
cmd := exec .Command ("/bin/bash" , "-c" , stressCommand )
130
+ // enables the process group id
131
+ cmd .SysProcAttr = & syscall.SysProcAttr {Setpgid : true }
128
132
var buf bytes.Buffer
129
133
cmd .Stdout = & buf
130
134
err = cmd .Start ()
@@ -183,16 +187,18 @@ func prepareStressChaos(experimentsDetails *experimentTypes.ExperimentDetails, c
183
187
err , ok := err .(* exec.ExitError )
184
188
if ok {
185
189
status := err .Sys ().(syscall.WaitStatus )
186
- if status .Signaled () && status .Signal () == syscall .SIGTERM {
190
+ if status .Signaled () && status .Signal () == syscall .SIGKILL {
187
191
// wait for the completion of abort handler
188
192
time .Sleep (10 * time .Second )
189
- return errors .Errorf ("process stopped with SIGTERM signal" )
193
+ return errors .Errorf ("process stopped with SIGKILL signal" )
190
194
}
191
195
}
192
196
return errors .Errorf ("process exited before the actual cleanup, err: %v" , err )
193
197
}
194
198
log .Info ("[Info]: Chaos injection completed" )
195
- terminateProcess (cmd .Process .Pid )
199
+ if err := terminateProcess (cmd .Process .Pid ); err != nil {
200
+ return err
201
+ }
196
202
if err = result .AnnotateChaosResult (resultDetails .Name , chaosDetails .ChaosNamespace , "reverted" , "pod" , experimentsDetails .TargetPods ); err != nil {
197
203
return err
198
204
}
@@ -203,12 +209,11 @@ func prepareStressChaos(experimentsDetails *experimentTypes.ExperimentDetails, c
203
209
204
210
//terminateProcess will remove the stress process from the target container after chaos completion
205
211
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
212
217
}
213
218
log .Info ("[Info]: Stress process removed successfully" )
214
219
return nil
0 commit comments