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) {
}
go func() {
for range m.notifyCh {
m.mx.Lock()
for _, lch := range m.listeners {
select {
case lch <- struct{}{}:
default:
}
}
m.mx.Unlock()
}
}()
return m, nil
......
......@@ -4,6 +4,7 @@ import (
"context"
"log"
"path/filepath"
"sync"
"testing"
"time"
......@@ -48,6 +49,7 @@ func (e dummyMetadataEntry) toBackup() *Backup {
}
type dummyMetadataStore struct {
mx sync.Mutex
log []dummyMetadataEntry
}
......@@ -118,6 +120,9 @@ func groupByBackup(dbAtoms []dummyMetadataEntry) []*Backup {
}
func (d *dummyMetadataStore) FindAtoms(_ context.Context, req *FindRequest) ([]*Backup, error) {
d.mx.Lock()
defer d.mx.Unlock()
var tmp []dummyMetadataEntry
for _, l := range d.log {
if !l.match(req) {
......@@ -130,6 +135,9 @@ func (d *dummyMetadataStore) FindAtoms(_ context.Context, req *FindRequest) ([]*
}
func (d *dummyMetadataStore) AddDataset(_ context.Context, backup *Backup, ds *Dataset) error {
d.mx.Lock()
defer d.mx.Unlock()
log.Printf("AddDataset: %+v", *ds)
for _, atom := range ds.Atoms {
path := filepath.Join(ds.Source, ds.Name, atom.Name)
......
......@@ -7,11 +7,13 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"sync"
"time"
"github.com/hashicorp/go-version"
......@@ -25,7 +27,7 @@ type resticRepository struct {
excludeFiles []string
autoPrune bool
initialized bool
initialized sync.Once
}
func (r *resticRepository) resticCmd() string {
......@@ -122,20 +124,17 @@ func (r *resticRepository) Close() error {
}
func (r *resticRepository) Init(ctx context.Context, rctx RuntimeContext) error {
if r.initialized {
return nil
}
// Restic init will fail the second time we run it, ignore
// errors.
err := rctx.Shell().Run(ctx, fmt.Sprintf(
"%s init --quiet || true",
r.resticCmd(),
))
if err == nil {
r.initialized = true
}
return err
r.initialized.Do(func() {
// Restic init will fail if the repository is already
// initialized, ignore errors (but log them).
if err := rctx.Shell().Run(ctx, fmt.Sprintf(
"%s init --quiet || true",
r.resticCmd(),
)); err != nil {
log.Printf("restic repository init failed (likely harmless): %v", err)
}
})
return nil
}
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