Skip to content

fix(auth): in daemon mode, sleep for rate-limit window not respawn-cascade (kills daemon-rate-limit-loop) - #416

Merged
joelteply merged 1 commit into
canaryfrom
fix/airc-rate-limit-aware-backoff
May 2, 2026
Merged

joelteply merged 1 commit into
canaryfrom
fix/airc-rate-limit-aware-backoff

Conversation

@joelteply

Copy link
Copy Markdown
Contributor

Joel 2026-05-02: 'gh rate limits are killing airc.'

Pre-fix: daemon hits rate-limit → exits → launchd respawns 10s later → hits again → cascade deepens throttle. Daemon never recovers.

Fix: when AIRC_BACKGROUND_OK=1 (daemon-launcher marker), sleep in-process for AIRC_RATE_LIMIT_WAIT_SEC (default 600s) instead of exiting. Daemon stays alive (no respawn = no new API calls), limit clears naturally.

Interactive mode keeps fail-fast for human-driven recovery.

🤖 Generated with Claude Code

…pawn-cascade

Joel 2026-05-02: "gh rate limits are killing airc."

Pre-fix: when daemon-mode airc-connect hit a gh secondary rate
limit at startup, airc_ensure_gh_auth_or_heal returned 1, the
process exited, launchd's KeepAlive (ThrottleInterval=10s) respawned
us 10s later, we hit gh-auth-status again → still rate-limited →
exit → respawn. The cascade DEEPENS the throttle since each respawn
makes more API calls. Daemon never recovers.

Tonight's exact repro 2026-05-02 after the bus-stability
teardown+restart sequence: daemon entered respawn cascade, status
showed 'state = spawn scheduled' / 'last exit code = 1' on a loop,
gh secondary rate limit kept extending.

Fix: when AIRC_BACKGROUND_OK=1 (set by the daemon launchers in
launchd plist + systemd unit + Windows .bat) and we hit
rate_limited, SLEEP in-process for AIRC_RATE_LIMIT_WAIT_SEC (default
600s = 10min) instead of exiting. The daemon stays alive (no
respawn = no new API calls), the limit clears naturally, we
re-check, succeed, proceed normally.

Interactive mode (no AIRC_BACKGROUND_OK) keeps the existing fail-
fast behavior — a human at the terminal can see the error and
decide whether to wait, change networks, or use a different account.

Trade-off accepted: in-process sleep holds the daemon process for
10min during throttle. It's bash sleeping — minimal cost. WAY better
than respawn cascade making the throttle worse.

Configurable: AIRC_RATE_LIMIT_WAIT_SEC env var. Default 600s
(10min) matches gh's published secondary rate-limit window of
"5-15 minutes."

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Copilot AI review requested due to automatic review settings May 2, 2026 16:57
@joelteply
joelteply merged commit ae173e5 into canary May 2, 2026
8 checks passed
@joelteply
joelteply deleted the fix/airc-rate-limit-aware-backoff branch May 2, 2026 16:57

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adjusts the GitHub-auth “rate_limited” handling to avoid daemon respawn cascades when airc connect is run under launchd/systemd (background mode), by sleeping inside the process for a configurable wait window instead of immediately returning failure.

Changes:

  • Add daemon-mode handling for the rate_limited auth state that sleeps for AIRC_RATE_LIMIT_WAIT_SEC (default 600s) before re-checking auth state.
  • Emit daemon-mode specific log lines explaining the in-process sleep behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/airc_bash/lib_auth.sh
Comment on lines +223 to +236
echo " [daemon mode] sleeping ${_wait_secs}s in-process (avoids respawn cascade)..." >&2
sleep "$_wait_secs" || return 1
# Re-check after wait; loop if still rate-limited (gives full
# window every cycle without exiting + respawning).
local _new_state; _new_state="$(airc_detect_gh_auth_state)"
if [ "$_new_state" = "ok" ]; then
echo " [daemon mode] rate-limit cleared, proceeding." >&2
return 0
fi
# Still throttled — fall through to return 1 + let daemon
# respawn give us a fresh process state. Respawn cycle is now
# at least _wait_secs apart, not 10s.
echo " [daemon mode] still rate-limited after wait; deferring to launchd respawn." >&2
return 1
Comment thread lib/airc_bash/lib_auth.sh
echo " Context: $context" >&2
echo "" >&2
if [ "${AIRC_BACKGROUND_OK:-0}" = "1" ]; then
local _wait_secs="${AIRC_RATE_LIMIT_WAIT_SEC:-600}"
joelteply added a commit that referenced this pull request May 2, 2026
…e root cause (#419)

fix(auth): cache 'gh auth status' OK state for 300s — root cause of /user throttle

b69f's diagnostic 2026-05-02: core gh rate limit was 4766/5000 (fine).
The secondary throttle was on /user specifically — the endpoint
'gh auth status' probes. Every airc-connect startup calls auth-state
detection → hits /user. Daemon respawn cascade = many /user calls/min
= secondary throttle locks for ~15min.

#416 fixed the symptom (don't cascade on rate-limit). This PR fixes
the cause (don't call /user on every startup).

Cache the "ok" state for AIRC_AUTH_CACHE_SEC (default 300s = 5min)
at /tmp/airc-gh-auth-ok-<uid>. Only "ok" is cached — failure paths
(rate_limited, invalid, env_token_invalid) run fresh classification
so we never hide a real failure behind a stale cache. Cache file is
deleted on every non-ok detection.

Effect:
- Daemon respawn (within 5min of last successful auth check) =
  cache hit, zero gh API calls
- First call after 5min OR after auth failure = fresh check, hits
  /user once
- Worst-case rate of /user calls drops from "once per startup" to
  "once per 5min per process tree"

This compounds with #415 (don't delete gist on teardown = fewer
respawns) + #416 (sleep on rate-limit instead of cascade) for the
"gh rate limits killing airc" structural fix.

## Tests

- bash syntax clean
- TTL math uses standard stat fall-through (Mac %m, Linux %Y, default 0)
- Cache invalidation runs on every non-ok path so stale "ok" can't persist

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants