fix: lock-mgr get key from arg instead of env

This commit is contained in:
h z
2026-04-13 15:37:13 +01:00
parent 311d9f4d9f
commit dcc91ead9b

View File

@@ -121,7 +121,7 @@ func releaseMgrLock(mgrPath string) error {
return writeLockFile(mgrPath, lf) return writeLockFile(mgrPath, lf)
} }
func cmdAcquire(mgrPath, filePath string) error { func cmdAcquire(mgrPath, filePath, key string) error {
// Steps 3-8: acquire meta-lock // Steps 3-8: acquire meta-lock
if _, err := acquireMgrLock(mgrPath); err != nil { if _, err := acquireMgrLock(mgrPath); err != nil {
return err return err
@@ -145,10 +145,8 @@ func cmdAcquire(mgrPath, filePath string) error {
xKey = entry.Key xKey = entry.Key
} }
envKey := os.Getenv("LOCK_MGR_KEY")
// Step 11: if locked by someone else, wait and retry // 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 { if time.Since(startTime) > 30*time.Second {
_ = releaseMgrLock(mgrPath) _ = releaseMgrLock(mgrPath)
return fmt.Errorf("timeout waiting to acquire lock on %s", filePath) return fmt.Errorf("timeout waiting to acquire lock on %s", filePath)
@@ -165,23 +163,12 @@ func cmdAcquire(mgrPath, filePath string) error {
continue continue
} }
// Step 12: not locked (or already owned by us) — acquire // Step 12: not locked (or already owned by caller's key) — acquire
var newKey string
if envKey != "" {
newKey = envKey
} else {
newKey, err = generateUUID()
if err != nil {
_ = releaseMgrLock(mgrPath)
return err
}
}
if lf[filePath] == nil { if lf[filePath] == nil {
lf[filePath] = &LockEntry{} lf[filePath] = &LockEntry{}
} }
lf[filePath].Locked = true lf[filePath].Locked = true
lf[filePath].Key = newKey lf[filePath].Key = key
lf[filePath].Time = time.Now().UTC().Format(time.RFC3339) lf[filePath].Time = time.Now().UTC().Format(time.RFC3339)
// Step 13: delete meta-lock entry and write atomically // Step 13: delete meta-lock entry and write atomically
@@ -190,12 +177,11 @@ func cmdAcquire(mgrPath, filePath string) error {
return err return err
} }
fmt.Println(newKey)
return nil return nil
} }
} }
func cmdRelease(mgrPath, filePath string) error { func cmdRelease(mgrPath, filePath, key string) error {
// Steps 3-8: acquire meta-lock // Steps 3-8: acquire meta-lock
if _, err := acquireMgrLock(mgrPath); err != nil { if _, err := acquireMgrLock(mgrPath); err != nil {
return err return err
@@ -214,20 +200,14 @@ func cmdRelease(mgrPath, filePath string) error {
xKey = entry.Key xKey = entry.Key
} }
envKey := os.Getenv("LOCK_MGR_KEY")
// Step 14: validate preconditions // Step 14: validate preconditions
if envKey == "" {
_ = releaseMgrLock(mgrPath)
return fmt.Errorf("LOCK_MGR_KEY environment variable not set")
}
if !xLocked { if !xLocked {
_ = releaseMgrLock(mgrPath) _ = releaseMgrLock(mgrPath)
return fmt.Errorf("file %s is not locked", filePath) return fmt.Errorf("file %s is not locked", filePath)
} }
if xKey != envKey { if xKey != key {
_ = releaseMgrLock(mgrPath) _ = 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 // Step 15: delete file lock and meta-lock, write
@@ -255,13 +235,32 @@ func cmdForceUnlock(mgrPath, filePath string) error {
} }
func main() { func main() {
if len(os.Args) < 3 { action := ""
fmt.Fprintln(os.Stderr, "Usage: lock-mgr <acquire|release|force-unlock> <file>") 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 <file>")
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 <file> <key>\n", action)
os.Exit(1)
}
if action == "" || (action != "acquire" && action != "release" && action != "force-unlock") {
fmt.Fprintln(os.Stderr, "Usage: lock-mgr <acquire|release> <file> <key>")
fmt.Fprintln(os.Stderr, " lock-mgr force-unlock <file>")
os.Exit(1) os.Exit(1)
} }
action := os.Args[1]
fileArg := os.Args[2] 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 // Step 1: resolve tool directory and locate (or create) the lock file
execPath, err := os.Executable() execPath, err := os.Executable()
@@ -296,15 +295,11 @@ func main() {
switch action { switch action {
case "acquire": case "acquire":
err = cmdAcquire(mgrPath, filePath) err = cmdAcquire(mgrPath, filePath, key)
case "release": case "release":
err = cmdRelease(mgrPath, filePath) err = cmdRelease(mgrPath, filePath, key)
case "force-unlock": case "force-unlock":
err = cmdForceUnlock(mgrPath, filePath) err = cmdForceUnlock(mgrPath, filePath)
default:
fmt.Fprintf(os.Stderr, "error: unknown action %q\n", action)
fmt.Fprintln(os.Stderr, "Usage: lock-mgr <acquire|release|force-unlock> <file>")
os.Exit(1)
} }
if err != nil { if err != nil {