diff --git a/internal/incident/incidents.go b/internal/incident/incidents.go index 56d0b1663..e96eb9b1b 100644 --- a/internal/incident/incidents.go +++ b/internal/incident/incidents.go @@ -17,7 +17,7 @@ import ( var ( currentIncidents = make(map[*object.Object]*Incident) - currentIncidentsMu sync.Mutex + currentIncidentsMu sync.RWMutex ) // LoadOpenIncidents loads all active (not yet closed) incidents from the database and restores all their states. @@ -59,11 +59,11 @@ func GetCurrent( ctx context.Context, db *icingadb.DB, obj *object.Object, logger *logging.Logger, runtimeConfig *config.RuntimeConfig, create bool, ) (*Incident, bool, error) { - currentIncidentsMu.Lock() - defer currentIncidentsMu.Unlock() - created := false + + currentIncidentsMu.RLock() currentIncident := currentIncidents[obj] + currentIncidentsMu.RUnlock() if currentIncident == nil { ir := &IncidentRow{} @@ -94,7 +94,9 @@ func GetCurrent( } if currentIncident != nil { + currentIncidentsMu.Lock() currentIncidents[obj] = currentIncident + currentIncidentsMu.Unlock() } } @@ -114,17 +116,16 @@ func RemoveCurrent(obj *object.Object) { currentIncidentsMu.Lock() defer currentIncidentsMu.Unlock() - currentIncident := currentIncidents[obj] - - if currentIncident != nil { - delete(currentIncidents, obj) - } + delete(currentIncidents, obj) } // GetCurrentIncidents returns a map of all incidents for debugging purposes. +// +// NOTE: This map contains the same pointers as stored in currentIncidents, but are no longer protected with its mutex. +// For accessing each Incident, use their internal sync.Mutex functions. func GetCurrentIncidents() map[int64]*Incident { - currentIncidentsMu.Lock() - defer currentIncidentsMu.Unlock() + currentIncidentsMu.RLock() + defer currentIncidentsMu.RUnlock() m := make(map[int64]*Incident) for _, incident := range currentIncidents { diff --git a/internal/object/object.go b/internal/object/object.go index 4b1ec42d5..160e8718a 100644 --- a/internal/object/object.go +++ b/internal/object/object.go @@ -53,14 +53,13 @@ func FromEvent(ctx context.Context, db *icingadb.DB, ev *event.Event) (*Object, id := ID(ev.SourceId, ev.Tags) cacheMu.Lock() - defer cacheMu.Unlock() - object, ok := cache[id.String()] if !ok { object = NewObject(db, ev) object.ID = id cache[id.String()] = object } + cacheMu.Unlock() tx, err := db.BeginTxx(ctx, nil) if err != nil { @@ -105,10 +104,12 @@ func FromEvent(ctx context.Context, db *icingadb.DB, ev *event.Event) (*Object, return nil, fmt.Errorf("can't commit object database transaction: %w", err) } + cacheMu.Lock() object.ExtraTags = ev.ExtraTags object.Tags = ev.Tags object.Name = ev.Name object.URL = ev.URL + cacheMu.Unlock() return object, nil }