OAuth refresh race condition when multiple eca server processes coexist (Anthropic Max, likely others)
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 48/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Ít trao đổi
- Công nghệ
- clojure
- Lĩnh vực
- authentication
Hướng nghiên cứu
Start with oauth-refresh in src/eca/llm_providers/anthropic.clj and compare the parallel refresh paths in src/eca/llm_providers/openai.clj and src/eca/oauth.clj; inspect cache reads and writes in src/eca/db.clj. Reproduce concurrent refreshes and determine how to adopt tokens another process has written. Done means concurrent refreshes recover without invalid_grant errors or requiring /login.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Describe the bug
When multiple eca server processes run on the same machine (e.g. one per workspace in a multi-project workflow), they race during OAuth access-token refresh against https://console.anthropic.com/v1/oauth/token. The losing process receives:
Anthropic refresh token failed: "{\"error\": \"invalid_grant\", \"error_description\": \"Refresh token not found or invalid\"}"
Error: Auth token renew failed
After this fires, the affected session cannot recover automatically. The user has to /login again, which destroys the active chat from the user's perspective.
To Reproduce
- Authenticate ECA with Anthropic Max OAuth (
/login->max). - Start a second
eca serverprocess for a different workspace (e.g. open ECA in another project, or useemacs --daemonand spawn ECA from a second workspace). - Let both processes idle ~1 hour until access tokens approach expiry.
- Send a prompt in both near-simultaneously.
- Observe: one prompt succeeds and rotates the refresh token; the other fails with
Anthropic refresh token failed: invalid_grant.
Higher process counts amplify probability. With N=3+ processes, failures observed daily.
Expected behavior
Token refresh should be safe under concurrent processes. The losers of the race should detect the rotation that already happened, adopt the new tokens from disk, and continue without surfacing an error.
Additional context
Root cause
All eca server processes share ~/.cache/eca/db.transit.json for OAuth state (refresh-token, access-token, expires-at). On startup each process reads this file into its in-memory db* atom. There is no file lock around the refresh flow:
src/eca/llm_providers/anthropic.cljoauth-refresh(around line 548) callsPOST /v1/oauth/tokenwith the in-memoryrefresh-token.- On success it swaps new tokens into
db*and writes to disk viadb/update-global-cache!.
When access tokens expire (~1 hour TTL), multiple processes detect expiry near-simultaneously and each call oauth-refresh with the same refresh-token they loaded at startup. Anthropic rotates the refresh token on every successful refresh; the first call invalidates the old token server-side and subsequent calls within the same window receive invalid_grant. The losers' in-memory and on-disk refresh-token becomes permanently invalid until manual /login.
Likely the same root cause as an earlier report
editor-code-assistant/eca-emacs#177 (closed) included a user report from @snoopier: "Get a lot of 401 today and it's absolutely random". The repo owner replied "I noticed anthropic and other models are throwing this randomly. I believe we can have a way to configure in ECA a match for status-code and body to consider as retry". The fix added retryRules config.
retryRules helps for transient 401s (the access token is stale; retry triggers a fresh refresh). It does not recover from invalid_grant on the refresh endpoint itself, because the refresh-token is permanently invalid. The "absolutely random" pattern matches what you would expect from a refresh race.
Diagnostic data from my setup
$ ps -eo pid,etime,command | grep "[e]ca server"
99996 1-23:55 /opt/homebrew/bin/eca server
97576 2-00:35 /opt/homebrew/bin/eca server
10027 04:35 /opt/homebrew/bin/eca server
29094 59 /opt/homebrew/bin/eca server
29434 55 /opt/homebrew/bin/eca server
5 concurrent eca server processes against a single shared ~/.cache/eca/db.transit.json. Failure observed daily. Error message verbatim:
Anthropic refresh token failed: "{\"error\": \"invalid_grant\", \"error_description\": \"Refresh token not found or invalid\"}"
Anthropic auth in db.transit.json:
{"anthropic" {:step :login/done
:mode :max
:type :auth/oauth
:refresh-token "sk-ant-ort01-..."
:api-key "sk-ant-oat01-..."
:expires-at <epoch>}}
Proposed fix
Wrap oauth-refresh (and the equivalent in openai.clj and oauth.clj's refresh-token!) with a file-lock plus re-read pattern:
(defn ^:private with-token-refresh-lock [cache-file f]
(let [lock-file (io/file (str cache-file ".lock"))]
(io/make-parents lock-file)
(with-open [raf (java.io.RandomAccessFile. lock-file "rw")
ch (.getChannel raf)
lk (.lock ch)]
(f))))
;; Inside the refresh path, after acquiring the lock:
;; 1. Re-read db.transit.json from disk
;; 2. If on-disk refresh-token differs from in-memory: another process
;; refreshed; adopt disk values, skip HTTP, return success
;; 3. Otherwise: call HTTP refresh, write new tokens, return
Java FileLock on the JVM works; the GraalVM native image also supports it. The lock is exclusive and held only across the refresh call (sub-second), so contention is negligible.
Simpler alternative (lower bar): on invalid_grant response, re-read db.transit.json once and retry with the disk-fresh refresh token before surfacing the error. This does not prevent the race but auto-recovers from it.
Affected files
src/eca/llm_providers/anthropic.clj(oauth-refresh,:login/renew-tokenstep)src/eca/llm_providers/openai.clj(paralleloauth-refresh)src/eca/oauth.clj(refresh-token!for MCP server OAuth, same race possible)src/eca/db.clj(good place for the lock wrapper)
Workaround until fixed
- Run only one
eca serverprocess at a time, or - Use
ANTHROPIC_API_KEY(loses Max subscription billing).
Severity
For users with multi-workspace or emacs --daemon workflows: daily auth failures, repeated browser-based re-logins, lost chat context. The issue scales with concurrent process count and is invisible to single-session users, which is likely why it has gone undiagnosed despite the symptom appearing in eca-emacs#177.
- Ngôn ngữ chính
- Clojure
- Star
- 1k
- Fork
- 82
- Merge trung bình
- 21 giờ 52 phút
- Pull request đã merge (30 ngày)
- 13
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của editor-code-assistant/eca
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
editor-code-assistant/eca#717 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Models doesn't work when fetchModels: false is usedCó thể làm lại được @zikajk đã nhận 45 ngày trước và không có pull request nào đang mở. Đang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
editor-code-assistant/eca#661 · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 35/100
editor-code-assistant/eca#719 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 66/100
editor-code-assistant/eca#716 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Add delete_file toolĐang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 48/100
editor-code-assistant/eca#708 ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của editor-code-assistant/eca
Issue tương tự
-
Build fails with `compile-css`Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
nextjournal/clerk#830 ·
-
nREPL server never answers a request with non-ASCII text that arrives in more than one chunkĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
-
IOP tokens release blocker team qa team qa autotests WG adoption
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 66/100
penpot/penpot#13540 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
izhangzhihao/intellij-rainbow-brackets#2903 · 2 bình luận ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 67/100