Repository navigation
fix(auth): in daemon mode, sleep for rate-limit window not respawn-cascade (kills daemon-rate-limit-loop) - #416
Merged
Conversation
…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]>
There was a problem hiding this comment.
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_limitedauth state that sleeps forAIRC_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 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 |
| 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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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