fix: Change Account signed out BEFORE the browser opened
Reported 2026-09-27: click avatar -> Change Account -> cancel in the Google chooser -> back to a signed-out app, then "cannot sign-in because the callback url was not released". Two independent defects: 1. ChangeAccountAsync ran `await SignOutYouTubeAsync(); await SignInAsync();`. SignOutYouTubeAsync -> TokenStore.Clear() -> deletes ytLlive.auth, so the working account's token was destroyed before the browser even opened. It is now just `await SignInAsync()`: success overwrites the token via the sessionChanged hook, cancel/failure keeps the current session. The explicit sign-out was never needed. 2. Abandoning the browser gives the app no signal, so the OAuth attempt held localhost:8765 for its full 5-minute timeout and a retry could not bind. A new AuthenticateAsync now cancels and awaits the abandoned attempt first. That takeover is serialised by a SemaphoreSlim: the first version cleared the in-flight marker before awaiting, so a caller arriving in that window saw a free port and raced to bind it -- reproducing the very failure it was written to prevent. My own test caught that; reasoning had "proved" it fine. Also adds an `openBrowser` ctor seam so tests never shell out to a real browser, and corrects the ai.md line that documented "Logout / Change Account -> SignOutYouTubeAsync" as intended behaviour. Test: YouTubeAuthServiceTests.RetryingAfterAnAbandonedAttempt_ReleasesTheCallbackPort Full suite 370/370, 0 warnings. Coverage gap noted in HANDOFF.md: the ordering fix has no automated test because _youtubeAuth is hardcoded in MainViewModel and ChangeAccountAsync is private. The reported cross-instance effect is NOT this cause and stays open -- no code path can sign out a second instance (per-instance TokenStore, local delete, no server-side revoke).
This commit is contained in:
@@ -1177,3 +1177,60 @@ well-formed, unverified* claim got to speak without a check — a dead EV-certif
|
||||
policy number with no scope, a refund lever I did not control. **Sound reasoning about a
|
||||
mechanism I had not verified is the failure mode, not the safety net.** Before any external
|
||||
mechanism drives a decision, name the one fact that would falsify it, then go get that fact.
|
||||
|
||||
## 2026-09-27 — "Change Account" signed out BEFORE the browser opened: destroying state you cannot get back
|
||||
|
||||
**The symptom:** the creator clicked their avatar → Change Account, cancelled in the Google account
|
||||
chooser, came back **signed out of a perfectly working account**, and then could not sign in again
|
||||
("the callback url was not released"). They also observed it hitting *both* running instances.
|
||||
|
||||
**The cause, and it is embarrassing in its simplicity.** `ChangeAccountAsync` was:
|
||||
|
||||
```csharp
|
||||
await SignOutYouTubeAsync(); // → TokenStore.Clear() → File.Delete(ytLlive.auth)
|
||||
await SignInAsync(); // the browser has not even opened yet
|
||||
```
|
||||
|
||||
The sign-out ran **first**, before any possibility of success, and `SignOutYouTubeAsync` does not
|
||||
"log out" — it **deletes the DPAPI token file**. So the moment the creator committed to the intent of
|
||||
switching accounts, the old account was already unrecoverable. Cancelling the browser flow then had
|
||||
nothing to fall back to. The explicit sign-out was not merely redundant, it was the bug: success
|
||||
already overwrites the token through the `sessionChanged → TokenStore.Save` hook.
|
||||
|
||||
**The transferable rule — order of operations on unrecoverable state:**
|
||||
|
||||
> **Never destroy the working state before the replacement is secured. Authenticate first, then
|
||||
> replace.** If the new value can fail, cancel, or be abandoned, the old value is the *only* thing
|
||||
> standing between the user and a re-authentication loop — so it must outlive the attempt, not
|
||||
> precede it.
|
||||
|
||||
The tell for this class of bug: a call named for an *intent* ("Change Account") that internally
|
||||
performs a *destructive reset* before doing the requested work. "Change" implies A→B, and the naive
|
||||
implementation of A→B is `Reset(); Set(B)`. The safe one is `B = Compute(); if (B != null) Commit(B)`.
|
||||
|
||||
**The second defect was hiding behind the first, and the fix for it was also a race.** Abandoning the
|
||||
browser gives the app no signal, so the OAuth attempt just sat on its loopback port `localhost:8765`
|
||||
for the full 5-minute timeout; a retry could not bind and reported the callback URL as "not released".
|
||||
Cancel-and-await-the-previous fixed the serial case, but my first version still had a window:
|
||||
`Interlocked.Exchange(ref _authInFlight, null)` nulls the field *before* awaiting the old attempt, so
|
||||
a third caller arriving in that window sees "nothing in flight" and races to bind the same port
|
||||
anyway — **the takeover reintroduced the exact failure it was written to prevent.** A `SemaphoreSlim`
|
||||
around cancel→await→bind made it atomic. My own test caught this; reasoning alone had "proved" it
|
||||
correct. The generalisation: **when you serialise a handover, the whole handover is one critical
|
||||
section — clearing the "who is doing this" marker early is the same bug as not having the lock.**
|
||||
|
||||
**Also worth recording: the cross-instance part of the report was NOT the cause.** The creator
|
||||
believed one instance's sign-out hit both. It could not have — `TokenStore.DefaultPath` resolves
|
||||
through `InstanceProfile.DataRoot` and `Clear()` only deletes a local file, never a server-side revoke,
|
||||
and no second instance had a token to clear. The two claims were independent, and believing they were
|
||||
linked would have sent me to debug the wrong subsystem. **One report can carry two unrelated defects;
|
||||
split the report into claims and check each against the code before building a theory on it.**
|
||||
|
||||
### Known timing flake: `NotificationAreaIntegrationTests.Info_Auto_Dismisses_And_Error_Sticks`
|
||||
|
||||
Failed once on 2026-09-27 with "info toast never auto-dismissed" after an 8s wait, then passed in
|
||||
isolation and on a clean full-suite run. It is a real-time WPF toast timer, so it loses under
|
||||
parallel load — **it is not a signal that your change broke notifications.** Before believing it,
|
||||
re-run it alone (`vstest --Tests:Info_Auto_Dismisses_And_Error_Sticks`); only treat it as yours if it
|
||||
fails in isolation too. See also the two other suite-level timing traps already recorded above
|
||||
(the one-tick pipe read, and blocking waits on the shared `RealApp` STA thread).
|
||||
|
||||
Reference in New Issue
Block a user