Skip to content

Commit e32ae89

Browse files
Fix data race in childProcess (#82)
There was a data race between GetResourceConsumption and watchResourceConsumption, one was setting resourceSpec, the other was reading. Now setting of resourceSpec is done before the resource motoring is started and both functions only read it so there is no more data race. Issue: #81
1 parent cebc629 commit e32ae89

File tree

2 files changed

+31
-21
lines changed

2 files changed

+31
-21
lines changed

testbed/testbed/child_process.go

+19-8
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ type resourceSpec struct {
4848
resourceCheckPeriod time.Duration
4949
}
5050

51+
// isSpecified returns true if any part of resourceSpec is specified,
52+
// i.e. has non-zero value.
53+
func (rs *resourceSpec) isSpecified() bool {
54+
return rs != nil && (rs.expectedMaxCPU != 0 || rs.expectedMaxRAM != 0)
55+
}
56+
5157
// childProcess is a child process that can be monitored and the output
5258
// of which will be written to a log file.
5359
type childProcess struct {
@@ -101,10 +107,11 @@ type childProcess struct {
101107
}
102108

103109
type startParams struct {
104-
name string
105-
logFilePath string
106-
cmd string
107-
cmdArgs []string
110+
name string
111+
logFilePath string
112+
cmd string
113+
cmdArgs []string
114+
resourceSpec *resourceSpec
108115
}
109116

110117
type ResourceConsumption struct {
@@ -126,6 +133,7 @@ func (cp *childProcess) start(params startParams) error {
126133

127134
cp.name = params.name
128135
cp.doneSignal = make(chan struct{})
136+
cp.resourceSpec = params.resourceSpec
129137

130138
log.Printf("Starting %s (%s)", cp.name, params.cmd)
131139

@@ -236,8 +244,11 @@ func (cp *childProcess) stop() {
236244
})
237245
}
238246

239-
func (cp *childProcess) watchResourceConsumption(spec *resourceSpec) error {
240-
cp.resourceSpec = spec
247+
func (cp *childProcess) watchResourceConsumption() error {
248+
if !cp.resourceSpec.isSpecified() {
249+
// Resource monitoring is not enabled.
250+
return nil
251+
}
241252

242253
var err error
243254
cp.processMon, err = process.NewProcess(int32(cp.cmd.Process.Pid))
@@ -257,7 +268,7 @@ func (cp *childProcess) watchResourceConsumption(spec *resourceSpec) error {
257268
}
258269

259270
// Measure every resourceCheckPeriod.
260-
ticker := time.NewTicker(spec.resourceCheckPeriod)
271+
ticker := time.NewTicker(cp.resourceSpec.resourceCheckPeriod)
261272
defer ticker.Stop()
262273

263274
for {
@@ -351,7 +362,7 @@ func (cp *childProcess) isAllowedResourceUsage() bool {
351362

352363
// GetResourceConsumption returns resource consumption as a string
353364
func (cp *childProcess) GetResourceConsumption() string {
354-
if cp.resourceSpec == nil {
365+
if !cp.resourceSpec.isSpecified() {
355366
// Monitoring is not enabled.
356367
return ""
357368
}

testbed/testbed/test_case.go

+12-13
Original file line numberDiff line numberDiff line change
@@ -127,26 +127,25 @@ func (tc *TestCase) StartAgent() {
127127
logFileName := tc.composeTestResultFileName("agent.log")
128128

129129
err := tc.agentProc.start(startParams{
130-
name: "Agent",
131-
logFilePath: logFileName,
132-
cmd: testBedConfig.Agent,
133-
cmdArgs: []string{"--config", tc.agentConfigFile},
130+
name: "Agent",
131+
logFilePath: logFileName,
132+
cmd: testBedConfig.Agent,
133+
cmdArgs: []string{"--config", tc.agentConfigFile},
134+
resourceSpec: &tc.resourceSpec,
134135
})
135136

136137
if err != nil {
137138
tc.indicateError(err)
138139
return
139140
}
140141

141-
// Start watching if expected resources are defined.
142-
if tc.resourceSpec.expectedMaxCPU != 0 || tc.resourceSpec.expectedMaxRAM != 0 {
143-
go func() {
144-
err := tc.agentProc.watchResourceConsumption(&tc.resourceSpec)
145-
if err != nil {
146-
tc.indicateError(err)
147-
}
148-
}()
149-
}
142+
// Start watching resource consumption.
143+
go func() {
144+
err := tc.agentProc.watchResourceConsumption()
145+
if err != nil {
146+
tc.indicateError(err)
147+
}
148+
}()
150149

151150
// Wait a bit for agent to start. This is a hack. We need to have a way to
152151
// wait for agent to start properly.

0 commit comments

Comments
 (0)