Skip to content

Commit 0e6ab71

Browse files
committed
Fix possible race condition
This fixes a possible issue where slices modified in the Config struct were accessed in one go routine while being changed in another. I never saw this occure and it would be be very rare but it was possible for this to cause a race before. I think this is the only location in the program this was possible but no promises.
1 parent d39d787 commit 0e6ab71

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

main.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package main
22

33
import (
4+
"bytes"
45
"context"
6+
"encoding/gob"
57
"encoding/json"
68
"errors"
79
"flag"
@@ -508,10 +510,22 @@ func repoWorker(wg *sync.WaitGroup, rc <-chan repo) {
508510
}
509511
}
510512

513+
// Read config makes a copy of the current config so that if the structure
514+
// is modified by a user signal we don't have race conditions in the goroutines
515+
// accessing the returned variable.
511516
func readConfig() Config {
517+
var n bytes.Buffer
518+
enc := gob.NewEncoder(&n)
519+
dec := gob.NewDecoder(&n)
520+
521+
dupe := Config{}
522+
512523
configLock.RLock()
513524
defer configLock.RUnlock()
514-
return globalConf
525+
526+
enc.Encode(globalConf)
527+
dec.Decode(&dupe)
528+
return dupe
515529
}
516530

517531
// Feed the channel all the information that it needs

0 commit comments

Comments
 (0)