diff --git a/params/params.go b/params/params.go index 2da3357..fb59122 100644 --- a/params/params.go +++ b/params/params.go @@ -115,6 +115,11 @@ func PutParam(path string, data []byte) error { if err != nil { return errors.Wrap(err, "could not create temp param file") } + defer func() { + if file != nil { + file.Close() + } + }() tmpName := file.Name() defer os.Remove(tmpName) @@ -127,8 +132,12 @@ func PutParam(path string, data []byte) error { if err != nil { return errors.Wrap(err, "could not fsync temp param file") } + if err = file.Close(); err != nil { + return errors.Wrap(err, "could not close temp param file") + } + file = nil - fileLock := flock.New(filepath.Join(lock_dir, ".lock")) + fileLock := flock.New(filepath.Join(lock_dir, ".lock"), flock.SetPermissions(0o775)) retries := 0 for { @@ -140,12 +149,6 @@ func PutParam(path string, data []byte) error { break } retries += 1 - if retries > 30 { - // try to force the lock to be removed - if err := os.Remove(filepath.Join(lock_dir, ".lock")); err != nil { - slog.Debug("failed to force delete params lock", "error", err) - } - } if retries > 50 { return errors.New("could not obtain lock") } @@ -157,11 +160,6 @@ func PutParam(path string, data []byte) error { slog.Error("could not unlock params directory", "error", err) } }() - defer func() { - if err := os.Remove(filepath.Join(lock_dir, ".lock")); err != nil { - slog.Error("could not remove params lock file", "error", err) - } - }() err = os.Rename(tmpName, path) if err != nil { @@ -172,11 +170,20 @@ func PutParam(path string, data []byte) error { if err != nil { return errors.Wrap(err, "could not open params directory") } + defer func() { + if directory != nil { + directory.Close() + } + }() err = directory.Sync() if err != nil { return errors.Wrap(err, "could not fsync params directory") } + if err = directory.Close(); err != nil { + return errors.Wrap(err, "could not close params directory") + } + directory = nil return nil } @@ -184,7 +191,7 @@ func PutParam(path string, data []byte) error { func RemoveParam(path string) error { dir := filepath.Dir(path) lock_dir := filepath.Dir(dir) - fileLock := flock.New(filepath.Join(lock_dir, ".lock")) + fileLock := flock.New(filepath.Join(lock_dir, ".lock"), flock.SetPermissions(0o775)) retries := 0 for { @@ -196,12 +203,6 @@ func RemoveParam(path string) error { break } retries += 1 - if retries > 30 { - // try to force the lock to be removed - if err := os.Remove(filepath.Join(lock_dir, ".lock")); err != nil { - slog.Debug("failed to force delete params lock", "error", err) - } - } if retries > 50 { return errors.New("could not obtain lock") } @@ -213,11 +214,6 @@ func RemoveParam(path string) error { slog.Error("could not unlock params directory", "error", err) } }() - defer func() { - if err := os.Remove(filepath.Join(lock_dir, ".lock")); err != nil { - slog.Error("could not remove params lock file", "error", err) - } - }() os.Remove(path) @@ -225,11 +221,20 @@ func RemoveParam(path string) error { if err != nil { return errors.Wrap(err, "could not open params directory") } + defer func() { + if directory != nil { + directory.Close() + } + }() err = directory.Sync() if err != nil { return errors.Wrap(err, "could not fsync params directory") } + if err = directory.Close(); err != nil { + return errors.Wrap(err, "could not close params directory") + } + directory = nil return nil }