Skip to content

Commit 7b03459

Browse files
authored
Merge pull request #91 from mattn/fix-race
Fix race
2 parents bfa320f + b18ead5 commit 7b03459

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

goreman.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ type procInfo struct {
6262
waitErr error
6363
}
6464

65+
var mu sync.Mutex
66+
6567
// process informations named with proc.
6668
var procs []*procInfo
6769

@@ -128,6 +130,9 @@ func readProcfile(cfg *config) error {
128130
if err != nil {
129131
return err
130132
}
133+
mu.Lock()
134+
defer mu.Unlock()
135+
131136
procs = []*procInfo{}
132137
index := 0
133138
for _, line := range strings.Split(string(content), "\n") {
@@ -195,6 +200,10 @@ func check(cfg *config) error {
195200
if err != nil {
196201
return err
197202
}
203+
204+
mu.Lock()
205+
defer mu.Unlock()
206+
198207
keys := make([]string, len(procs))
199208
i := 0
200209
for _, proc := range procs {
@@ -207,6 +216,9 @@ func check(cfg *config) error {
207216
}
208217

209218
func findProc(name string) *procInfo {
219+
mu.Lock()
220+
defer mu.Unlock()
221+
210222
for _, proc := range procs {
211223
if proc.name == name {
212224
return proc
@@ -221,6 +233,7 @@ func start(ctx context.Context, sig <-chan os.Signal, cfg *config) error {
221233
if err != nil {
222234
return err
223235
}
236+
224237
ctx, cancel := context.WithCancel(ctx)
225238
// Cancel the RPC server when procs have returned/errored, cancel the
226239
// context anyway in case of early return.
@@ -238,7 +251,9 @@ func start(ctx context.Context, sig <-chan os.Signal, cfg *config) error {
238251
maxProcNameLength = len(v)
239252
}
240253
}
254+
mu.Lock()
241255
procs = tmp
256+
mu.Unlock()
242257
}
243258
godotenv.Load()
244259
rpcChan := make(chan *rpcMessage, 10)

goreman_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@ web4: sleep 10
149149
goremanStopped <- struct{}{}
150150
}()
151151
for {
152-
if procs == nil {
152+
mu.Lock()
153+
isEmpty := procs == nil
154+
mu.Unlock()
155+
if isEmpty {
153156
time.Sleep(5 * time.Millisecond)
154157
continue
155158
}

0 commit comments

Comments
 (0)