From dcc91ead9b55567797f6c020bac7847c13e7f9e3 Mon Sep 17 00:00:00 2001 From: hzhang Date: Mon, 13 Apr 2026 15:37:13 +0100 Subject: [PATCH] fix: lock-mgr get key from arg instead of env --- lock-mgr/main.go | 67 ++++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 36 deletions(-) diff --git a/lock-mgr/main.go b/lock-mgr/main.go index 0d67ad9..e4fa97b 100644 --- a/lock-mgr/main.go +++ b/lock-mgr/main.go @@ -121,7 +121,7 @@ func releaseMgrLock(mgrPath string) error { return writeLockFile(mgrPath, lf) } -func cmdAcquire(mgrPath, filePath string) error { +func cmdAcquire(mgrPath, filePath, key string) error { // Steps 3-8: acquire meta-lock if _, err := acquireMgrLock(mgrPath); err != nil { return err @@ -145,10 +145,8 @@ func cmdAcquire(mgrPath, filePath string) error { xKey = entry.Key } - envKey := os.Getenv("LOCK_MGR_KEY") - // Step 11: if locked by someone else, wait and retry - if xLocked && xKey != envKey { + if xLocked && xKey != key { if time.Since(startTime) > 30*time.Second { _ = releaseMgrLock(mgrPath) return fmt.Errorf("timeout waiting to acquire lock on %s", filePath) @@ -165,23 +163,12 @@ func cmdAcquire(mgrPath, filePath string) error { continue } - // Step 12: not locked (or already owned by us) — acquire - var newKey string - if envKey != "" { - newKey = envKey - } else { - newKey, err = generateUUID() - if err != nil { - _ = releaseMgrLock(mgrPath) - return err - } - } - + // Step 12: not locked (or already owned by caller's key) — acquire if lf[filePath] == nil { lf[filePath] = &LockEntry{} } lf[filePath].Locked = true - lf[filePath].Key = newKey + lf[filePath].Key = key lf[filePath].Time = time.Now().UTC().Format(time.RFC3339) // Step 13: delete meta-lock entry and write atomically @@ -190,12 +177,11 @@ func cmdAcquire(mgrPath, filePath string) error { return err } - fmt.Println(newKey) return nil } } -func cmdRelease(mgrPath, filePath string) error { +func cmdRelease(mgrPath, filePath, key string) error { // Steps 3-8: acquire meta-lock if _, err := acquireMgrLock(mgrPath); err != nil { return err @@ -214,20 +200,14 @@ func cmdRelease(mgrPath, filePath string) error { xKey = entry.Key } - envKey := os.Getenv("LOCK_MGR_KEY") - // Step 14: validate preconditions - if envKey == "" { - _ = releaseMgrLock(mgrPath) - return fmt.Errorf("LOCK_MGR_KEY environment variable not set") - } if !xLocked { _ = releaseMgrLock(mgrPath) return fmt.Errorf("file %s is not locked", filePath) } - if xKey != envKey { + if xKey != key { _ = releaseMgrLock(mgrPath) - return fmt.Errorf("LOCK_MGR_KEY does not match the lock key for %s", filePath) + return fmt.Errorf("key does not match the lock key for %s", filePath) } // Step 15: delete file lock and meta-lock, write @@ -255,13 +235,32 @@ func cmdForceUnlock(mgrPath, filePath string) error { } func main() { - if len(os.Args) < 3 { - fmt.Fprintln(os.Stderr, "Usage: lock-mgr ") + action := "" + if len(os.Args) >= 2 { + action = os.Args[1] + } + + // force-unlock only needs 2 args + if action == "force-unlock" && len(os.Args) < 3 { + fmt.Fprintln(os.Stderr, "Usage: lock-mgr force-unlock ") + os.Exit(1) + } + // acquire and release need 3 args + if (action == "acquire" || action == "release") && len(os.Args) < 4 { + fmt.Fprintf(os.Stderr, "Usage: lock-mgr %s \n", action) + os.Exit(1) + } + if action == "" || (action != "acquire" && action != "release" && action != "force-unlock") { + fmt.Fprintln(os.Stderr, "Usage: lock-mgr ") + fmt.Fprintln(os.Stderr, " lock-mgr force-unlock ") os.Exit(1) } - action := os.Args[1] fileArg := os.Args[2] + key := "" + if action != "force-unlock" { + key = os.Args[3] + } // Step 1: resolve tool directory and locate (or create) the lock file execPath, err := os.Executable() @@ -296,15 +295,11 @@ func main() { switch action { case "acquire": - err = cmdAcquire(mgrPath, filePath) + err = cmdAcquire(mgrPath, filePath, key) case "release": - err = cmdRelease(mgrPath, filePath) + err = cmdRelease(mgrPath, filePath, key) case "force-unlock": err = cmdForceUnlock(mgrPath, filePath) - default: - fmt.Fprintf(os.Stderr, "error: unknown action %q\n", action) - fmt.Fprintln(os.Stderr, "Usage: lock-mgr ") - os.Exit(1) } if err != nil {