Skip to content

Commit e58785b

Browse files
authored
[*] prevent concurrent map read and write in SourceCon (#971)
1 parent 4fc4d10 commit e58785b

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

internal/reaper/database.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,8 @@ func (r *Reaper) GetInstanceUpMeasurement(ctx context.Context, md *sources.Sourc
461461
}
462462

463463
func (r *Reaper) CheckForPGObjectChangesAndStore(ctx context.Context, md *sources.SourceConn) {
464+
md.Lock()
465+
defer md.Unlock()
464466
var err error
465467
l := log.GetLogger(ctx).WithField("source", md.Name).WithField("metric", specialMetricChangeEvents)
466468
ctx = log.WithLogger(ctx, l)

internal/reaper/reaper.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,9 @@ func (r *Reaper) CreateSourceHelpers(ctx context.Context, srcL log.Logger, monit
216216
if r.Sources.TryCreateListedExtsIfMissing > "" {
217217
srcL.Info("trying to create extensions if missing")
218218
extsToCreate := strings.Split(r.Sources.TryCreateListedExtsIfMissing, ",")
219+
monitoredSource.RLock()
219220
extsCreated := TryCreateMissingExtensions(ctx, monitoredSource, extsToCreate, monitoredSource.Extensions)
221+
monitoredSource.RUnlock()
220222
srcL.Infof("%d/%d extensions created based on --try-create-listed-exts-if-missing input %v", len(extsCreated), len(extsToCreate), extsCreated)
221223
}
222224

internal/sources/conn.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"math"
77
"regexp"
88
"strconv"
9+
"sync"
910
"time"
1011

1112
"github.com/cybertec-postgresql/pgwatch/v3/internal/db"
@@ -49,6 +50,7 @@ type (
4950
Conn db.PgxPoolIface
5051
ConnConfig *pgxpool.Config
5152
RuntimeInfo
53+
sync.RWMutex
5254
}
5355

5456
SourceConns []*SourceConn
@@ -123,6 +125,8 @@ func (md *SourceConn) GetDatabaseName() string {
123125

124126
// GetMetricInterval returns the metric interval for the connection
125127
func (md *SourceConn) GetMetricInterval(name string) float64 {
128+
md.RLock()
129+
defer md.RUnlock()
126130
if md.IsInRecovery && len(md.MetricsStandby) > 0 {
127131
return md.MetricsStandby[name]
128132
}
@@ -158,6 +162,8 @@ func VersionToInt(version string) (v int) {
158162
}
159163

160164
func (md *SourceConn) FetchRuntimeInfo(ctx context.Context, forceRefetch bool) (err error) {
165+
md.Lock()
166+
defer md.Unlock()
161167
if ctx.Err() != nil {
162168
return ctx.Err()
163169
}

0 commit comments

Comments
 (0)