ccstatusline icon indicating copy to clipboard operation
ccstatusline copied to clipboard

macOS: per-render `security` keychain reads pile up unlock requests after a securityd restart and wedge securityd (screen unlock hangs)

Open OakGimbap opened this issue 1 month ago • 0 comments

Summary

On macOS, if the status line has a usage widget whose data isn't in Claude Code's rate_limits stdin payload (here fable-weekly-usage), ccstatusline runs security find-generic-password on every render, and falls back to security dump-keychain. Normally that's just overhead.

After securityd restarts, though, the login keychain comes back locked, and every one of those reads becomes a keychain-unlock request that nobody answers. In my case securityd piled up 47 of them in under two minutes and then stopped responding altogether. About 80 minutes later, unlocking the screen hung for good: the password was accepted, but the spinner never finished, because loginwindow was waiting on securityd to unlock the login keychain. Only a forced reboot got the machine back.

The securityd crash itself is an Apple bug, not ccstatusline's. The problem here is that an unattended status line that reads the keychain every few seconds keeps doing so while the keychain is locked, and that turns a recoverable daemon restart into a system-wide hang.

Environment

  • macOS 27.0.1 (26A434), Apple Silicon
  • Claude Code 2.1.301
  • ccstatusline 2.2.32, run as bunx -y ccstatusline@latest with refreshInterval: 10
  • 2 Claude Code sessions open, machine unattended
  • Usage widgets: session-usage, reset-timer, weekly-usage, weekly-reset-timer, fable-weekly-usage

Timeline

From log show; times are relative to the securityd crash.

Time Event
Before security find-generic-password … -w runs about every 5 s (2 sessions × 10 s). The keychain is unlocked and every read succeeds.
T+0 s securityd aborts (Probable bug: error destroying Mutex: 16, SIGABRT). launchd respawns it ~0.3 s later.
T+0 to T+110 s The new securityd has the login keychain locked. There are 67 security invocations in this window: 45 find-generic-password -w and 22 full dump-keychain enumerations. securityd logs unlocking for makeUnlocked() / new SecurityAgentXPCQuery 47 times, each on a different thread, and none of them completes. 45 of the 47 start within 50 ms of a security call.
T+110 s securityd stops logging and never logs again. From here on, security calls hang in SecKeychainSearchCreateFromAttributes until the 5 s timeout kills them.
T+~2.5 min The screen auto-locks; nobody is at the machine.
T+~80 min Screen unlock: Screensaver authorization succeeded, then Attempting to unlock login keychain … fails with mach error: 268435459 (a stale port to the dead securityd). The retry goes to the new, wedged securityd and never returns. The spinner runs until a forced reboot.

Why this points at ccstatusline

  • The call pattern (find-generic-password -s "Claude Code-credentials" -w, then dump-keychain, then find-generic-password again) matches the fallback chain in getUsageCredentials() in src/utils/usage-fetch.ts.
  • fable-weekly-usage has no apiBucketKey (src/utils/usage-types.ts). prefetchUsageDataIfNeeded() can therefore never fill it from rate_limits, and it calls fetchUsageData() on every render.
  • fetchUsageData() resolves credentials before it checks the file cache ("Resolve the credentials up front …"). Each render is a fresh process, so the in-memory cache never applies. The result is one keychain read per render per session, even when usage.json is fresh.

Reproduction (no securityd crash needed)

Put a security wrapper first on PATH that logs its arguments and then execs /usr/bin/security. Render once with a fixed stdin payload:

PATH="$WRAPPER_DIR:$PATH" bunx [email protected] --config ./cfg.json < ./stdin.json
Config stdin with rate_limits stdin without rate_limits (new session, before the first response)
includes fable-weekly-usage 1 security call per render 1 per render
without fable-weekly-usage 0 1 per render (session/weekly fall back to the API)

Related

  • #683 added a 5 s timeout to these security calls. That keeps the status line itself from stalling, but the next render still calls again, and each new call adds another request in securityd while the earlier ones are still pending. Nothing backs off from a locked keychain.
  • #521 / #532 are about which keychain item gets read, not how often it's read.
  • #560 asks to prefer the live usage API over rate_limits. If that lands, every usage widget would read the keychain on every render and this would get worse.

Suggestions

  1. Check the usage file cache before touching the keychain. The keychain read could wait until an API call is actually needed, at most once per CACHE_MAX_AGE across all sessions. Right now the token fingerprint forces the read up front. A cheaper key, such as the credentials source already used for the no-credentials lock, could gate the cache instead.
  2. After a keychain read fails or times out, back off for minutes rather than 30 s, and share the backoff across sessions. A locked keychain won't unlock itself while the user is away.
  3. Keep security dump-keychain out of the render path. It enumerates the whole keychain on every render where it runs.
  4. Document that model-scoped weekly widgets such as fable-weekly-usage cost one keychain read and one API call per render. Alternatively, add an option to use only the stdin rate_limits and never fall back to the keychain or the API.

Workaround

Remove fable-weekly-usage. Replace the session and weekly widgets with custom-command widgets that read only stdin, for example usage.sh five_hour Session and usage.sh seven_day Weekly:

#!/bin/sh
jq -r --arg k "$1" --arg l "$2" '
  .rate_limits[$k] as $b
  | if $b == null then "\($l): –" else
      ($b.used_percentage | floor) as $p
      | (($b.resets_at - now) | if . < 0 then 0 else . end) as $s
      | "\($l): \($p)% | \(if $s >= 86400 then "\($s/86400|floor)d \($s%86400/3600|floor)h" else "\($s/3600|floor)h \($s%3600/60|floor)m" end)"
    end'

With this, the wrapper above logs zero security calls in both stdin cases.

OakGimbap avatar Oct 16 '26 09:10 OakGimbap