Skip to content

The agent that grows with you

fix(dashboard): the Codex saver takes the profile scope before the session lock

John Paul Solivacc5cb8f96366
diff --git a/hermes_cli/web_routers/oauth.py b/hermes_cli/web_routers/oauth.py
index da500c2eba2..027f42c2f64 100644
--- a/hermes_cli/web_routers/oauth.py
+++ b/hermes_cli/web_routers/oauth.py
@@ -297,12 +297,13 @@ def _codex_full_login_worker(session_id: str) -> None:
# The cancellation check and the save are one atomic critical section
# under the lock cancel_oauth_session() uses; otherwise DELETE could
# flip "cancelled" between the check and the save and tokens would be
- # persisted after the user believed the login was aborted.
- with _oauth_sessions_lock:
+ # persisted after the user believed the login was aborted. The profile scope
+ # (which holds _SKILLS_PROFILE_LOCK) is entered first, as in every other saver:
+ # the reverse order deadlocks against a Nous/xAI/MiniMax save finishing at once.
+ with _profile_scope(session_profile), _oauth_sessions_lock:
if _codex_cancelled(sess, session_id, " before token save"):
return
- with _profile_scope(session_profile):
- _save_codex_tokens(tokens)
+ _save_codex_tokens(tokens)
sess["status"] = "approved"
_log.info("oauth/device: openai-codex login completed (session=%s)", session_id)
except Exception as e:
diff --git a/tests/hermes_cli/test_web_oauth_dispatch.py b/tests/hermes_cli/test_web_oauth_dispatch.py
index 019906cf022..5e3a9d1aebb 100644
--- a/tests/hermes_cli/test_web_oauth_dispatch.py
+++ b/tests/hermes_cli/test_web_oauth_dispatch.py
@@ -21,6 +21,7 @@ The fix:
These tests pin the corrected behavior.
"""
+import contextlib
import json
import time
from unittest.mock import patch
@@ -426,9 +427,23 @@ def test_codex_worker_final_save_is_atomic_with_cancel_delete(tmp_path, monkeypa
monkeypatch.setattr(auth_mod, "_save_codex_tokens", fake_save)
monkeypatch.setattr(ws.time, "sleep", lambda *_a, **_k: None)
+ # Lock order: every saver enters the profile scope (_SKILLS_PROFILE_LOCK) before
+ # _oauth_sessions_lock; one saver taking them the other way round deadlocks with the rest.
+ real_scope = _rt_oauth._profile_scope
+ session_lock_held_at_scope = []
+
+ @contextlib.contextmanager
+ def recording_scope(profile):
+ session_lock_held_at_scope.append(_web_server_oauth._oauth_sessions_lock.locked())
+ with real_scope(profile) as scoped:
+ yield scoped
+
+ monkeypatch.setattr(_rt_oauth, "_profile_scope", recording_scope)
+
sid, _ = _rt_oauth._new_oauth_session("openai-codex", "device_code", profile="coder")
_rt_oauth._codex_full_login_worker(sid)
+ assert session_lock_held_at_scope == [False], "profile scope entered while holding the session lock"
# The lock is released now (worker returned), so the DELETE thread can
# finally complete.