@@ -10,6 +10,7 @@ import (
1010 "log/slog"
1111 "os"
1212 "strings"
13+ "sync"
1314 "time"
1415
1516 "github.com/fsnotify/fsnotify"
@@ -44,12 +45,14 @@ type NginxAppProtectInstanceWatcher struct {
4445 attackSignatureVersion string
4546 threatCampaignVersion string
4647 enforcerEngineVersion string
48+ instanceMutex sync.Mutex
4749}
4850
4951func NewNginxAppProtectInstanceWatcher (agentConfig * config.Config ) * NginxAppProtectInstanceWatcher {
5052 return & NginxAppProtectInstanceWatcher {
5153 agentConfig : agentConfig ,
5254 filesBeingWatched : make (map [string ]bool ),
55+ instanceMutex : sync.Mutex {},
5356 }
5457}
5558
@@ -96,6 +99,9 @@ func (w *NginxAppProtectInstanceWatcher) Watch(ctx context.Context) {
9699}
97100
98101func (w * NginxAppProtectInstanceWatcher ) NginxAppProtectInstance () * mpi.Instance {
102+ w .instanceMutex .Lock ()
103+ defer w .instanceMutex .Unlock ()
104+
99105 return w .nginxAppProtectInstance
100106}
101107
@@ -210,6 +216,8 @@ func (w *NginxAppProtectInstanceWatcher) isNewInstance() bool {
210216}
211217
212218func (w * NginxAppProtectInstanceWatcher ) createInstance (ctx context.Context ) {
219+ w .instanceMutex .Lock ()
220+ defer w .instanceMutex .Unlock ()
213221 w .nginxAppProtectInstance = & mpi.Instance {
214222 InstanceMeta : & mpi.InstanceMeta {
215223 InstanceId : id .Generate (versionFilePath ),
0 commit comments