Skip to content

Commit c64a28f

Browse files
committed
BUG: prevent concurrent modification of map for map files
1 parent ae8a874 commit c64a28f

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

pkg/haproxy/maps/main.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"path"
2222
"sort"
2323
"strings"
24+
"sync"
2425

2526
"github.com/google/renameio"
2627
"github.com/haproxytech/kubernetes-ingress/pkg/fs"
@@ -113,20 +114,28 @@ func (m mapFiles) CleanMaps() {
113114
}
114115

115116
func (m mapFiles) RefreshMaps(client api.HAProxyClient) {
117+
mapFilesToDelete := make([]Name, 0, len(m))
118+
var wgWriter sync.WaitGroup
119+
var mapMutex sync.Mutex
120+
116121
for name, mapFile := range m {
117122
content, hash := mapFile.getContent()
118123
if mapFile.hash == hash {
119124
continue
120125
}
126+
wgWriter.Add(1)
121127
// parallelize writing of files
122128
fs.Writer.Write(func() {
129+
defer wgWriter.Done()
123130
var err error
124131
filename := GetPath(name)
125132
if len(content) == 0 && !mapFile.persistent {
126133
fs.AddDelayedFunc(string(filename), func() {
127134
logger.Error(os.Remove(string(filename)))
128135
})
129-
delete(m, name)
136+
mapMutex.Lock()
137+
mapFilesToDelete = append(mapFilesToDelete, name)
138+
mapMutex.Unlock()
130139
return
131140
} else {
132141
if _, err = os.Stat(string(filename)); err != nil {
@@ -165,6 +174,11 @@ func (m mapFiles) RefreshMaps(client api.HAProxyClient) {
165174
}
166175
})
167176
}
177+
178+
wgWriter.Wait()
179+
for _, mapFileToDelete := range mapFilesToDelete {
180+
delete(m, mapFileToDelete)
181+
}
168182
}
169183

170184
func GetPath(name Name) Path {

0 commit comments

Comments
 (0)