Skip to content

Commit d1cb363

Browse files
committed
fix race
1 parent a219346 commit d1cb363

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

internal/watcher/instance/nginx-app-protect-instance-watcher.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ func (w *NginxAppProtectInstanceWatcher) addWatcher(ctx context.Context, version
143143
}
144144

145145
func (w *NginxAppProtectInstanceWatcher) readVersionFile(ctx context.Context, versionFile string) {
146+
w.instanceMutex.Lock()
147+
defer w.instanceMutex.Unlock()
148+
146149
switch versionFile {
147150
case versionFilePath:
148151
w.version = w.readFile(ctx, versionFilePath)
@@ -167,6 +170,9 @@ func (w *NginxAppProtectInstanceWatcher) handleEvent(ctx context.Context, event
167170
}
168171

169172
func (w *NginxAppProtectInstanceWatcher) handleFileUpdateEvent(ctx context.Context, event fsnotify.Event) {
173+
w.instanceMutex.Lock()
174+
defer w.instanceMutex.Unlock()
175+
170176
switch event.Name {
171177
case versionFilePath:
172178
w.version = w.readFile(ctx, event.Name)
@@ -182,6 +188,9 @@ func (w *NginxAppProtectInstanceWatcher) handleFileUpdateEvent(ctx context.Conte
182188
}
183189

184190
func (w *NginxAppProtectInstanceWatcher) handleFileDeleteEvent(event fsnotify.Event) {
191+
w.instanceMutex.Lock()
192+
defer w.instanceMutex.Unlock()
193+
185194
switch event.Name {
186195
case versionFilePath:
187196
w.version = ""
@@ -212,6 +221,9 @@ func (w *NginxAppProtectInstanceWatcher) checkForUpdates(ctx context.Context) {
212221
}
213222

214223
func (w *NginxAppProtectInstanceWatcher) isNewInstance() bool {
224+
w.instanceMutex.Lock()
225+
defer w.instanceMutex.Unlock()
226+
215227
return w.nginxAppProtectInstance == nil && w.version != ""
216228
}
217229

@@ -245,13 +257,17 @@ func (w *NginxAppProtectInstanceWatcher) createInstance(ctx context.Context) {
245257
}
246258

247259
func (w *NginxAppProtectInstanceWatcher) deleteInstance(ctx context.Context) {
260+
w.instanceMutex.Lock()
261+
defer w.instanceMutex.Unlock()
262+
248263
slog.InfoContext(ctx, "NGINX App Protect instance not longer exists")
249264
w.nginxAppProtectInstance = nil
250265
}
251266

252267
func (w *NginxAppProtectInstanceWatcher) updateInstance(ctx context.Context) {
253268
w.instanceMutex.Lock()
254269
defer w.instanceMutex.Unlock()
270+
255271
w.nginxAppProtectInstance.GetInstanceMeta().Version = w.version
256272
runtimeInfo := w.nginxAppProtectInstance.GetInstanceRuntime().GetNginxAppProtectRuntimeInfo()
257273
if runtimeInfo == nil {

0 commit comments

Comments
 (0)