Skip to content

Commit d6e6ff7

Browse files
committed
refactor(injector): mutualise stop and wait procedure.
1 parent a28d875 commit d6e6ff7

3 files changed

Lines changed: 28 additions & 42 deletions

File tree

‎injector/cpu_pressure.go‎

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@ package injector
77

88
import (
99
"context"
10-
"errors"
1110
"fmt"
1211
"math"
13-
"os"
14-
"time"
1512

1613
"github.com/DataDog/chaos-controller/api/v1beta1"
1714
"github.com/DataDog/chaos-controller/command"
@@ -114,22 +111,5 @@ func (i *cpuPressureInjector) UpdateConfig(config Config) {
114111
}
115112

116113
func (i *cpuPressureInjector) Clean() error {
117-
if i.backgroundCmd == nil {
118-
return nil
119-
}
120-
121-
defer i.cancel()
122-
123-
if err := i.backgroundCmd.Stop(); err != nil && !errors.Is(err, os.ErrProcessDone) {
124-
return fmt.Errorf("unable to stop background process: %w", err)
125-
}
126-
127-
// Wait for the child process to fully exit before a potential re-inject during pulse mode.
128-
select {
129-
case <-i.backgroundCmd.Done():
130-
case <-time.After(5 * time.Second):
131-
i.config.Log.Warnw("timed out waiting for background process to exit")
132-
}
133-
134-
return nil
114+
return stopAndWaitForBackgroundCmd(i.config.Log, i.backgroundCmd, i.cancel)
135115
}

‎injector/injector.go‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ package injector
77

88
import (
99
"context"
10+
"errors"
11+
"fmt"
12+
"os"
1013
"time"
1114

1215
chaosapi "github.com/DataDog/chaos-controller/api"
1316
"github.com/DataDog/chaos-controller/cgroup"
17+
"github.com/DataDog/chaos-controller/command"
1418
"github.com/DataDog/chaos-controller/container"
1519
"github.com/DataDog/chaos-controller/netns"
1620
"github.com/DataDog/chaos-controller/o11y/metrics"
@@ -65,3 +69,25 @@ func (c Config) TargetName() string {
6569

6670
return UnknownTargetName
6771
}
72+
73+
// stopAndWaitForBackgroundCmd stops a background command and waits for the process to fully exit
74+
// before returning. This prevents cgroup race conditions during pulse mode re-injection.
75+
func stopAndWaitForBackgroundCmd(log *zap.SugaredLogger, backgroundCmd command.BackgroundCmd, cancel context.CancelFunc) error {
76+
if backgroundCmd == nil {
77+
return nil
78+
}
79+
80+
defer cancel()
81+
82+
if err := backgroundCmd.Stop(); err != nil && !errors.Is(err, os.ErrProcessDone) {
83+
return fmt.Errorf("unable to stop background process: %w", err)
84+
}
85+
86+
select {
87+
case <-backgroundCmd.Done():
88+
case <-time.After(5 * time.Second):
89+
log.Warnw("timed out waiting for background process to exit")
90+
}
91+
92+
return nil
93+
}

‎injector/memory_pressure.go‎

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ package injector
77

88
import (
99
"context"
10-
"errors"
1110
"fmt"
12-
"os"
1311
"time"
1412

1513
"github.com/DataDog/chaos-controller/api/v1beta1"
@@ -97,23 +95,5 @@ func (i *memoryPressureInjector) UpdateConfig(config Config) {
9795
}
9896

9997
func (i *memoryPressureInjector) Clean() error {
100-
if i.backgroundCmd == nil {
101-
return nil
102-
}
103-
104-
defer i.cancel()
105-
106-
if err := i.backgroundCmd.Stop(); err != nil && !errors.Is(err, os.ErrProcessDone) {
107-
return fmt.Errorf("unable to stop background process: %w", err)
108-
}
109-
110-
// Wait for the child process to fully exit so cgroup memory accounting is updated
111-
// before a potential re-inject during pulse mode.
112-
select {
113-
case <-i.backgroundCmd.Done():
114-
case <-time.After(5 * time.Second):
115-
i.config.Log.Warnw("timed out waiting for background process to exit")
116-
}
117-
118-
return nil
98+
return stopAndWaitForBackgroundCmd(i.config.Log, i.backgroundCmd, i.cancel)
11999
}

0 commit comments

Comments
 (0)