Skip to content

Commit 79babff

Browse files
committed
Treat empty lockfiles as stale
Had this happen when testing something in Caddy. A crash at startup left a lockfile created but empty. (This was not a production crash, just dev.) Empty lockfiles have been reported before. I think we should treat them as stale. It's not perfect but it's best-effort.
1 parent 5deb7c2 commit 79babff

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

filestorage.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,13 @@ func (s *FileStorage) Lock(ctx context.Context, name string) error {
172172
if err == nil {
173173
err2 := json.NewDecoder(f).Decode(&meta)
174174
f.Close()
175-
if err2 != nil {
175+
if errors.Is(err2, io.EOF) {
176+
// lockfile is empty or truncated; I *think* we can assume the previous
177+
// acquirer either crashed or had some sort of failure that caused them
178+
// to be unable to fully acquire or retain the lock, therefore we should
179+
// treat it as if the lockfile did not exist
180+
log.Printf("[INFO][%s] %s: Empty lockfile (%v) - likely previous process crashed or storage medium failure; treating as stale", s, filename, err2)
181+
} else if err2 != nil {
176182
return fmt.Errorf("decoding lockfile contents: %w", err2)
177183
}
178184
}
@@ -196,7 +202,7 @@ func (s *FileStorage) Lock(ctx context.Context, name string) error {
196202
s, name, meta.Created, meta.Updated, filename)
197203
if err = os.Remove(filename); err != nil { // hopefully we can replace the lock file quickly!
198204
if !errors.Is(err, fs.ErrNotExist) {
199-
return fmt.Errorf("unable to delete stale lock; deadlocked: %w", err)
205+
return fmt.Errorf("unable to delete stale lockfile; deadlocked: %w", err)
200206
}
201207
}
202208
continue

0 commit comments

Comments
 (0)