carlos / platform Public

carlos auth login: never delete another attempt's pending file

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 1201ebcca55344dce3596aa6326671ce6cc13b60 parent 290434f
2 files changed, +99 −34
  • authpending.go +43 −32
  • authpending_test.go +56 −2
diff --git a/authpending.go b/authpending.go
index adb9700ed..1e90747f8 100644
--- a/authpending.go
+++ b/authpending.go
@@ -40,8 +40,7 @@ func pendingLoginFile(deviceCode string) string {
}
// savePendingLogin writes this attempt's own file, 0600 through a temp file
-// in the same directory, then removes every other attempt's file: the newest
-// --no-wait wins. The device code is a secret until the login is claimed.
+// in the same directory, and deletes no other live attempt: several can sit side by side. The device code is a secret until the login is claimed.
func savePendingLogin(p pendingLogin) error {
dir := pendingLoginPath()
if err := os.MkdirAll(dir, 0o700); err != nil {
@@ -68,43 +67,34 @@ func savePendingLogin(p pendingLogin) error {
if err := tmp.Close(); err != nil {
return err
}
- own := pendingLoginFile(p.DeviceCode)
- if err := os.Rename(tmp.Name(), own); err != nil {
+ if err := os.Rename(tmp.Name(), pendingLoginFile(p.DeviceCode)); err != nil {
return err
}
- entries, err := os.ReadDir(dir)
- if err != nil {
- return err
- }
- for _, e := range entries {
- path := filepath.Join(dir, e.Name())
- if path == own || !strings.HasSuffix(e.Name(), ".json") {
- continue
- }
- if err := os.Remove(path); err != nil && !errors.Is(err, os.ErrNotExist) {
- return err
- }
- }
- return nil
+ // Nothing else is touched except attempts that have already expired.
+ _, err = readPendingLogins(true)
+ return err
}
-// loadPendingLogin returns the newest attempt (latest ExpiresAt), or nil, nil
-// when none is waiting. Expired attempts are still returned: --wait turns
-// one into the "expired" answer and removes it by its exact path.
-func loadPendingLogin() (*pendingLogin, error) {
- entries, err := os.ReadDir(pendingLoginPath())
+// readPendingLogins lists every attempt file. With sweep set it also removes
+// the expired ones, by exact path: an expired attempt is dead, so anyone may.
+// It never removes an unexpired file. The result holds unexpired attempts only.
+func readPendingLogins(sweep bool) ([]pendingLogin, error) {
+ dir := pendingLoginPath()
+ entries, err := os.ReadDir(dir)
if errors.Is(err, os.ErrNotExist) {
return nil, nil
}
if err != nil {
return nil, err
}
- var best *pendingLogin
+ now := time.Now()
+ var live []pendingLogin
for _, e := range entries {
if !strings.HasSuffix(e.Name(), ".json") {
continue
}
- raw, err := os.ReadFile(filepath.Join(pendingLoginPath(), e.Name()))
+ path := filepath.Join(dir, e.Name())
+ raw, err := os.ReadFile(path)
if errors.Is(err, os.ErrNotExist) {
continue
}
@@ -113,20 +103,41 @@ func loadPendingLogin() (*pendingLogin, error) {
}
var p pendingLogin
if err := json.Unmarshal(raw, &p); err != nil {
- return nil, errors.New(filepath.Join(pendingLoginPath(), e.Name()) + " is not readable as a pending login; delete it and run carlos auth login --no-wait again")
+ return nil, errors.New(path + " is not readable as a pending login; delete it and run carlos auth login --no-wait again")
}
- if best == nil || p.ExpiresAt.After(best.ExpiresAt) {
- cp := p
- best = &cp
+ if !p.ExpiresAt.IsZero() && !now.Before(p.ExpiresAt) {
+ if sweep {
+ if err := os.Remove(path); err != nil && !errors.Is(err, os.ErrNotExist) {
+ return nil, err
+ }
+ }
+ continue
+ }
+ live = append(live, p)
+ }
+ return live, nil
+}
+
+// loadPendingLogin returns the newest unexpired attempt (latest ExpiresAt),
+// or nil, nil when none is waiting. Expired files met on the way are removed.
+func loadPendingLogin() (*pendingLogin, error) {
+ live, err := readPendingLogins(true)
+ if err != nil || len(live) == 0 {
+ return nil, err
+ }
+ best := live[0]
+ for _, p := range live[1:] {
+ if p.ExpiresAt.After(best.ExpiresAt) {
+ best = p
}
}
- return best, nil
+ return &best, nil
}
// clearPendingLoginIf removes the file for exactly this attempt, by its own
// path. It never reads or removes another attempt's file, so a --wait that
-// finishes late cannot delete the login a newer --no-wait started. The name
-// is kept from the single-file version; the "if" is now the path itself.
+// finishes late cannot delete a login a newer --no-wait started. The name is
+// kept from the single-file version; the "if" is now the path itself.
func clearPendingLoginIf(deviceCode string) error {
if err := os.Remove(pendingLoginFile(deviceCode)); err != nil && !errors.Is(err, os.ErrNotExist) {
return err
diff --git a/authpending_test.go b/authpending_test.go
index 782f4a90a..c5a62d90c 100644
--- a/authpending_test.go
+++ b/authpending_test.go
@@ -68,6 +68,59 @@ func TestPendingLoginInterleavedCleanupKeepsNewer(t *testing.T) {
}
}
+// Two concurrent --no-wait writers: neither may delete the other's attempt.
+func TestPendingLoginConcurrentWritersKeepBoth(t *testing.T) {
+ useTempCredentials(t)
+ a := pendingLogin{Console: "https://a.example", DeviceCode: "dev-A", ExpiresAt: time.Now().Add(time.Minute)}
+ b := pendingLogin{Console: "https://b.example", DeviceCode: "dev-B", ExpiresAt: time.Now().Add(2 * time.Minute)}
+ if err := savePendingLogin(a); err != nil {
+ t.Fatal(err)
+ }
+ if err := savePendingLogin(b); err != nil {
+ t.Fatal(err)
+ }
+ // Each writer's post-save step runs again (its sweep of expired files).
+ if _, err := readPendingLogins(true); err != nil {
+ t.Fatal(err)
+ }
+ for _, c := range []string{"dev-A", "dev-B"} {
+ if _, err := os.Stat(pendingLoginFile(c)); err != nil {
+ t.Fatalf("%s's file is gone: %v", c, err)
+ }
+ }
+ if got, _ := loadPendingLogin(); got == nil || got.DeviceCode != "dev-B" {
+ t.Fatalf("load picked %+v, want B", got)
+ }
+}
+
+func TestPendingLoginExpiredFilesAreSweptOthersKept(t *testing.T) {
+ useTempCredentials(t)
+ dead := pendingLogin{Console: "https://a.example", DeviceCode: "dev-dead", ExpiresAt: time.Now().Add(-time.Second)}
+ live := pendingLogin{Console: "https://b.example", DeviceCode: "dev-live", ExpiresAt: time.Now().Add(time.Minute)}
+ if err := savePendingLogin(dead); err != nil {
+ t.Fatal(err)
+ }
+ if err := savePendingLogin(live); err != nil { // the save sweeps the dead one
+ t.Fatal(err)
+ }
+ if _, err := os.Stat(pendingLoginFile("dev-dead")); !os.IsNotExist(err) {
+ t.Fatalf("expired file survived a save: %v", err)
+ }
+ if _, err := os.Stat(pendingLoginFile("dev-live")); err != nil {
+ t.Fatalf("unexpired file removed: %v", err)
+ }
+ // And a load sweeps too.
+ if err := savePendingLogin(pendingLogin{Console: "https://c.example", DeviceCode: "dev-dead2", ExpiresAt: time.Now().Add(-time.Second)}); err != nil {
+ t.Fatal(err)
+ }
+ if got, _ := loadPendingLogin(); got == nil || got.DeviceCode != "dev-live" {
+ t.Fatalf("load picked %+v, want live", got)
+ }
+ if _, err := os.Stat(pendingLoginFile("dev-dead2")); !os.IsNotExist(err) {
+ t.Fatal("expired file survived a load")
+ }
+}
+
func TestAuthLoginApprovedButUnsavableEndsTheAttempt(t *testing.T) {
path := useTempCredentials(t)
fc := newFakeConsole(t)
@@ -243,8 +296,9 @@ func TestAuthLoginWaitTerminalStatesClear(t *testing.T) {
start := time.Now()
var out bytes.Buffer
err := authLogin(&out, []string{"--wait"})
- if err == nil || !strings.Contains(err.Error(), "expired") {
- t.Fatalf("err = %v, want expired", err)
+ // A dead attempt is swept on load, so there is nothing waiting.
+ if err == nil || !strings.Contains(err.Error(), "no login waiting") {
+ t.Fatalf("err = %v, want no login waiting", err)
}
if polls.Load() != 0 || time.Since(start) > 500*time.Millisecond {
t.Fatalf("polls = %d after %v, want none, prompt", polls.Load(), time.Since(start))