test(desktop): pin that a reclaimed auth-token lock cannot crash the write

Merged
SA
3mo ago
+65-0

#6790 fixed a fatal, unhandled crash on the encrypted sign-in token write path, but shipped only a negative test for the lock it fails to acquire. The path it actually fixed -- losing a lock we already hold -- had no coverage at all.

The regression this catches

Deleting the onCompromised handler from withAuthLock reintroduces the crash, and every one of the 18 existing tests stays green. proper-lockfile's default handler is (err) => { throw err }, thrown from inside its own refresh timer where no caller can catch it -- which is why those Sentry reports carry no stack.

The test also covers the second half of #6790: replacing the best-effort await release().catch(...) with a plain await release() makes the same run reject with ERELEASED, reporting a token that was written as a sign-in that failed to save.

How the condition is provoked

proper-lockfile arms a refresh for the lock it hands back, on a timer derived from stale -- stale / 2, so five seconds at the current 10s setting. If the entry is gone when that refresh stats it, the library marks the lock compromised. Waiting five seconds in a file that runs in under a second is not worth it, so the test intercepts that single scheduling call: it removes the lock entry the way a second desktop process reclaiming it would, then runs the library's own refresh callback directly.

The library is not mocked. Its stat, its ENOENT branch, setLockAsCompromised, the onCompromised dispatch and the resulting ERELEASED on release are all real; only the five-second delay is skipped. The test asserts it actually drove that refresh (refreshRan), so it cannot silently degrade into decoration if the library stops scheduling one.

It deliberately does not assert that a compromised lock makes the write succeed. It asserts that the write's own outcome survives: this write landed, so it is reported as landed and readable. The existing negative test -- a write that never happened still reports failure -- is untouched.

Verification

bunx biome check apps/desktop/src/lib/trpc/routers/auth/utils/auth-functions.test.ts

Checked 1 file in 13ms. No fixes applied.

cd apps/desktop && bun run typecheck -- clean, no pre-existing errors in this worktree:

$ bun run generate:icons && bun run generate:routes
$ bun run scripts/generate-file-icons.ts
Generated file icons: 1067 SVGs copied, 2046 file names, 1152 extensions, 4528 folder names
$ tsr generate
$ tsc --noEmit

bun test apps/desktop/src/lib/trpc/routers/auth/ -- 18 pass before, 19 pass after, same wall-clock:

 19 pass
 0 fail
 44 expect() calls
Ran 19 tests across 1 file. [904.00ms]

Full desktop suite, cd apps/desktop && bun test: 2799 pass, 0 fail across 302 files.

Proof it bites. With the onCompromised line deleted from withAuthLock:

error: ENOENT: no such file or directory, stat '/var/folders/.../auth-functions-test-HzBGf8/auth-token.enc.lock'
    path: ".../auth-token.enc.lock",
 syscall: "stat",
   errno: -2,
    code: "ECOMPROMISED"

(fail) auth token storage > survives another writer reclaiming the write lock mid-write [0.98ms]

 18 pass
 1 fail
Ran 19 tests across 1 file. [900.00ms]

That is the uncatchable throw itself failing the test -- Bun's runner attributes an unhandled error in a timer to the running test; a process.on("uncaughtException") listener never sees it. Note the other 18 tests pass without the fix, which is the gap.

With onCompromised restored but release().catch(...) reduced to await release():

error: Lock is already released
 code: "ERELEASED"
      at withAuthLock (apps/desktop/src/lib/trpc/routers/auth/utils/auth-functions.ts:68:9)

(fail) auth token storage > survives another writer reclaiming the write lock mid-write [2.12ms]

 18 pass
 1 fail

Restored, both lines present:

 19 pass
 0 fail
Ran 19 tests across 1 file. [904.00ms]

Run 35 consecutive times with the fix in place: 35/35 green. The ordering is structural rather than timed -- the compromise's stat is issued before the write's first filesystem call, so the loss is always recorded before saveToken resolves. No sleeps, no fixed waits, no timing-dependent assertions.

auth-functions.ts is unchanged; the diff is the test file only.

Refs DESKTOP-11M

https://claude.ai/code/session_01H1nPckannUVVytgNdnd3YG


Summary by cubic

Ensures reclaiming the auth-token write lock mid-write cannot crash or misreport the write. Previously, a compromised lock from proper-lockfile could throw from its timer or yield ERELEASED; this pins the intended outcome.

  • Runs the real proper-lockfile refresh immediately by intercepting its single timer, deleting the lock entry, and invoking the library callback; asserts the refresh ran. No mocks; only the 5s delay is skipped.
  • Waits for the compromise to be reported by the library’s stat callback before asserting to remove scheduling races; a missing report times out.
  • Verifies the write result stands: saveToken resolves and loadToken returns the token; lock loss only logs.
  • Fails on regressions: removing onCompromised in withAuthLock (uncatchable throw) or replacing release().catch(...) with await release() (ERELEASED).

Refs DESKTOP-11M

Summary by CodeRabbit

  • Tests
    • Added regression coverage for token storage when another process reclaims the write lock during saving.
    • Verified token writes complete successfully, remain readable, and handle lock-change warnings without interrupting the result.