Commit ad361dfa authored by ale's avatar ale

Add a few locks to prevent data races

parent f01658f1
Pipeline #3478 failed with stage
in 37 seconds
...@@ -269,12 +269,14 @@ func NewConfigManager(config *Config) (*ConfigManager, error) { ...@@ -269,12 +269,14 @@ func NewConfigManager(config *Config) (*ConfigManager, error) {
} }
go func() { go func() {
for range m.notifyCh { for range m.notifyCh {
m.mx.Lock()
for _, lch := range m.listeners { for _, lch := range m.listeners {
select { select {
case lch <- struct{}{}: case lch <- struct{}{}:
default: default:
} }
} }
m.mx.Unlock()
} }
}() }()
return m, nil return m, nil
......
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"context" "context"
"log" "log"
"path/filepath" "path/filepath"
"sync"
"testing" "testing"
"time" "time"
...@@ -48,6 +49,7 @@ func (e dummyMetadataEntry) toBackup() *Backup { ...@@ -48,6 +49,7 @@ func (e dummyMetadataEntry) toBackup() *Backup {
} }
type dummyMetadataStore struct { type dummyMetadataStore struct {
mx sync.Mutex
log []dummyMetadataEntry log []dummyMetadataEntry
} }
...@@ -118,6 +120,9 @@ func groupByBackup(dbAtoms []dummyMetadataEntry) []*Backup { ...@@ -118,6 +120,9 @@ func groupByBackup(dbAtoms []dummyMetadataEntry) []*Backup {
} }
func (d *dummyMetadataStore) FindAtoms(_ context.Context, req *FindRequest) ([]*Backup, error) { func (d *dummyMetadataStore) FindAtoms(_ context.Context, req *FindRequest) ([]*Backup, error) {
d.mx.Lock()
defer d.mx.Unlock()
var tmp []dummyMetadataEntry var tmp []dummyMetadataEntry
for _, l := range d.log { for _, l := range d.log {
if !l.match(req) { if !l.match(req) {
...@@ -130,6 +135,9 @@ func (d *dummyMetadataStore) FindAtoms(_ context.Context, req *FindRequest) ([]* ...@@ -130,6 +135,9 @@ func (d *dummyMetadataStore) FindAtoms(_ context.Context, req *FindRequest) ([]*
} }
func (d *dummyMetadataStore) AddDataset(_ context.Context, backup *Backup, ds *Dataset) error { func (d *dummyMetadataStore) AddDataset(_ context.Context, backup *Backup, ds *Dataset) error {
d.mx.Lock()
defer d.mx.Unlock()
log.Printf("AddDataset: %+v", *ds) log.Printf("AddDataset: %+v", *ds)
for _, atom := range ds.Atoms { for _, atom := range ds.Atoms {
path := filepath.Join(ds.Source, ds.Name, atom.Name) path := filepath.Join(ds.Source, ds.Name, atom.Name)
......
...@@ -7,11 +7,13 @@ import ( ...@@ -7,11 +7,13 @@ import (
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"log"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings" "strings"
"sync"
"time" "time"
"github.com/hashicorp/go-version" "github.com/hashicorp/go-version"
...@@ -25,7 +27,7 @@ type resticRepository struct { ...@@ -25,7 +27,7 @@ type resticRepository struct {
excludeFiles []string excludeFiles []string
autoPrune bool autoPrune bool
initialized bool initialized sync.Once
} }
func (r *resticRepository) resticCmd() string { func (r *resticRepository) resticCmd() string {
...@@ -122,20 +124,17 @@ func (r *resticRepository) Close() error { ...@@ -122,20 +124,17 @@ func (r *resticRepository) Close() error {
} }
func (r *resticRepository) Init(ctx context.Context, rctx RuntimeContext) error { func (r *resticRepository) Init(ctx context.Context, rctx RuntimeContext) error {
if r.initialized { r.initialized.Do(func() {
return nil // Restic init will fail if the repository is already
} // initialized, ignore errors (but log them).
if err := rctx.Shell().Run(ctx, fmt.Sprintf(
// Restic init will fail the second time we run it, ignore "%s init --quiet || true",
// errors. r.resticCmd(),
err := rctx.Shell().Run(ctx, fmt.Sprintf( )); err != nil {
"%s init --quiet || true", log.Printf("restic repository init failed (likely harmless): %v", err)
r.resticCmd(), }
)) })
if err == nil { return nil
r.initialized = true
}
return err
} }
func (r *resticRepository) Prepare(ctx context.Context, rctx RuntimeContext, backup *Backup) error { func (r *resticRepository) Prepare(ctx context.Context, rctx RuntimeContext, backup *Backup) error {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment