From ebbe76420de915a2d4efce3d8e46c79ec93dde85 Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 12 Oct 2026 21:57:19 +0800 Subject: [PATCH] provider: a Claude sign-in test's `claude auth login` run ends with the test, however it ends, and the mise-wrapper test waits the sign-in's own 30s for a link (#1802) #1802: TestClaudeSignInThroughMiseWrapper failed once at load 80-89 in 1.45s, and every provider test after it failed in 0.00s. The run's messages weren't kept. What was found, by forcing each failure by hand rather than rerunning: - The sign-in's run of `claude auth login` outlived the test that started it. A sign-in that gives up (no link in linkWait), or a test that fails between StartSignIn and the sign-in's end, leaves the run's goroutine letting go after the test: it reads claudeKeychain, removes its folder, forgets the account caches and, had the callback gone in, adds the account into whatever home is set by then. Under -race, TestClaudeSignInWrapperThatNeverStarts fails on main with that race (clearClaudeDir reading claudeKeychain while isolate's cleanup puts it back). - TestClaudeSignInThroughMiseWrapper shortened linkWait to 5s, so a busy machine starting sh, the fake mise and sh again could miss it. - miseWrapped set claudeExecutable and relied on fakeClaudeLogin's cleanup to put it back. - The cascade itself didn't come back. Forced failures with the sign-in waiting (A), finishing (E) and failed early (D, the CLI exiting at once, ~1.9s like the report), on the old code and the whole package for D: only the forced test failed, the ~100 after it ran and passed. Nothing found in the test explains every later test failing in 0.00s. One unproven candidate: this machine's disk is at 99% (9.9-12 GiB free today), and a full disk fails every test that writes a file at once. #1802 stays open for a run that keeps -v output. What changed: - claude_signin.go: claudeLogins, a WaitGroup of the `claude auth login` runs still going on, to the end of their goroutine. - signin_claude_test.go: fakeClaudeLogin, which every Claude sign-in test uses, registers endClaudeSignIns: when the test ends it cancels every Claude sign-in still listed (killing the CLI's process group) and waits for claudeLogins, failing the test if a run is still going after signInDeadline. It runs before the test's home and fakes are put back. - signin_claude_wrapper_test.go: miseWrapped puts claudeExecutable back itself; the through-the-wrapper test keeps linkWait's own 30s (the link comes as soon as it is printed); the never-starts test checks it gives up no sooner than linkWait and no later than linkWait + 1m. What was run (macOS, sandbox HOME, GOMODCACHE pinned, a private GOCACHE since the shared one was being emptied): - Old code (1bf722e28's tests), -race, -run 'TestClaude|TestCodexSignIn|SignIn': TestClaudeSignInWrapperThatNeverStarts FAILs "race detected". With this change: 181 pass, no race. - This change with a forced t.Fatal in TestClaudeSignInThroughMiseWrapper, with the sign-in waiting (A) and finishing (E), -race, the Claude, sign-in, Vertex, WorkBuddy-add, store-error and key-allowance tests: only the forced test fails, 306 pass each time. - gofmt -l clean; go vet ./... on darwin, GOOS=linux (-tags nogui) and GOOS=windows. - go test -tags nogui -timeout 40m ./internal/provider ./internal/proc: provider ok (774s), proc ok (26s), at load 75-90. --- internal/provider/claude_signin.go | 8 ++++ internal/provider/signin_claude_test.go | 37 +++++++++++++++++++ .../provider/signin_claude_wrapper_test.go | 16 +++++--- 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/internal/provider/claude_signin.go b/internal/provider/claude_signin.go index 2a21961cf..284b53d50 100644 --- a/internal/provider/claude_signin.go +++ b/internal/provider/claude_signin.go @@ -189,7 +189,9 @@ func startClaudeSignIn(s *signInFlow) error { printed := make(chan string, 1) exited := make(chan string, 1) var said lastSaid + claudeLogins.Add(1) go func() { + defer claudeLogins.Done() sc := bufio.NewScanner(out) var tail []string for sc.Scan() { @@ -266,6 +268,12 @@ func startClaudeSignIn(s *signInFlow) error { } } +// claudeLogins counts the runs of `claude auth login` still going on, +// to the end of what each does after the CLI exits (the account added, its +// folder removed). A sign-in that gave up has killed its run, but the run +// may still be letting go; tests wait for it before the next one starts. +var claudeLogins sync.WaitGroup + // lastSaid is the last line a login command printed, which says why // there's no link. type lastSaid struct { diff --git a/internal/provider/signin_claude_test.go b/internal/provider/signin_claude_test.go index 8594da6f4..72e5ce4b7 100644 --- a/internal/provider/signin_claude_test.go +++ b/internal/provider/signin_claude_test.go @@ -98,9 +98,46 @@ echo "Login successful." claudeExecutable, claudeURLOpener = oldExe, oldOpener forgetClaudeStatus() }) + endClaudeSignIns(t) return exe } +// endClaudeSignIns makes a Claude sign-in the test started end with it, +// however the test ends: a test that fails between StartSignIn and the +// sign-in's end (#1802), or a sign-in that gave up, leaves its `claude auth +// login` run letting go after the test, and what that run does — add the +// account, read claudeKeychain, forget the account caches — would land in +// a later test, whose home and fakes are the ones set by then. It cancels +// every Claude sign-in still listed, which kills the CLI's process group, +// and waits for every run to finish. Registered after the test's home and +// fakes are set, it runs while they are still the test's. +func endClaudeSignIns(t *testing.T) { + t.Helper() + t.Cleanup(func() { + signIns.Lock() + var ids []string + for id, s := range signIns.m { + if s.status().Agent == "claude" { + ids = append(ids, id) + } + } + signIns.Unlock() + for _, id := range ids { + CancelSignIn(id) + } + ended := make(chan struct{}) + go func() { + claudeLogins.Wait() + close(ended) + }() + select { + case <-ended: + case <-time.After(signInDeadline): + t.Errorf("a Claude sign-in still ran %s after the test", signInDeadline) + } + }) +} + // noAnthropic fails the test if anything is asked of claudeBase. func noAnthropic(t *testing.T) { t.Helper() diff --git a/internal/provider/signin_claude_wrapper_test.go b/internal/provider/signin_claude_wrapper_test.go index 9258092b6..530007581 100644 --- a/internal/provider/signin_claude_wrapper_test.go +++ b/internal/provider/signin_claude_wrapper_test.go @@ -40,7 +40,9 @@ shift exec "$@" `, which, d)) t.Setenv("PATH", bin+string(os.PathListSeparator)+miseBin+string(os.PathListSeparator)+os.Getenv("PATH")) - claudeExecutable = claudecode.Find // fakeClaudeLogin's cleanup puts it back + old := claudeExecutable + claudeExecutable = claudecode.Find + t.Cleanup(func() { claudeExecutable = old }) return wrapper } @@ -50,10 +52,10 @@ func TestClaudeSignInThroughMiseWrapper(t *testing.T) { claudeHome(t) noAnthropic(t) exe := fakeClaudeLogin(t, fakeClaudeAccount{email: "mise@example.com", plan: "max", refresh: "sk-ant-ort01-mise"}, "", false) + // linkWait stays the sign-in's own 30s: the link comes as soon as + // Claude Code prints it, so the wait only costs time on a busy machine, + // where sh, the fake mise and sh again can take seconds to start (#1802) miseWrapped(t, exe, true) - was := linkWait - linkWait = 5 * time.Second - t.Cleanup(func() { linkWait = was }) st, err := StartSignIn("claude") if err != nil { @@ -87,8 +89,10 @@ func TestClaudeSignInWrapperThatNeverStarts(t *testing.T) { if err == nil { t.Fatal("signed in through a wrapper that never starts Claude Code") } - if d := time.Since(start); d > 20*time.Second { - t.Fatalf("took %s", d) + // it gives up when linkWait is over, not before, and not long after: + // the upper bound is for a busy machine, the point is it isn't for ever + if d := time.Since(start); d < linkWait || d > linkWait+time.Minute { + t.Fatalf("gave up after %s, linkWait %s", d, linkWait) } if !strings.Contains(err.Error(), wrapper) || !strings.Contains(err.Error(), "printed nothing") || !strings.Contains(err.Error(), "in a terminal") { t.Fatalf("error %q doesn't name %s and what to do", err, wrapper)