Repository navigation
Acquire account locks before starting account updates #1790
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -491,3 +491,68 @@ def test_status_prints_account_email_after_plan(helper, capsys, provider, email, | |
| result = {'tier': 0, 'plan': 'max', 'email': email, 'windows': [], 'unknown_reason': 'no data'} | ||
| helper.print_status({'checked_at': helper.now(), 'providers': {provider: {'active': 'a', 'accounts': {'a': result}}}}) | ||
| assert shown in capsys.readouterr().out | ||
|
|
||
|
|
||
| def test_account_lock_timeout_leaves_files_untouched(helper, monkeypatch): | ||
| for name in ("main", "b", "c"): | ||
| auth = {"tokens": {"account_id": name, "access_token": "expiring"}} | ||
| helper.write_private(helper.codex_home(name) / "auth.json", json.dumps(auth)) | ||
| helper.set_active("codex", "main") | ||
| status = helper.STATE / "status.json" | ||
| cooldown = {"exhausted": {helper.identity("codex", "main") + ":model": helper.now() + 300}} | ||
| helper.write_private(status, json.dumps(cooldown)) | ||
| for key in (*helper.EXPLICIT_ROUTES["codex"], "CODEX_HOME"): | ||
| monkeypatch.delenv(key, raising=False) | ||
| elapsed = 0.0 | ||
|
|
||
| def advance(seconds): | ||
| nonlocal elapsed | ||
| elapsed += seconds | ||
|
|
||
| def premature_probe(*args): | ||
| pytest.fail("account probing started before the home lock was acquired") | ||
|
|
||
| monkeypatch.setattr(helper.time, "monotonic", lambda: elapsed) | ||
| monkeypatch.setattr(helper.time, "sleep", advance) | ||
| monkeypatch.setattr(helper, "check_claude", premature_probe) | ||
| monkeypatch.setattr(helper, "codex_in_use", lambda _: False) | ||
| monkeypatch.setattr(helper, "refresh_codex", premature_probe) | ||
| monkeypatch.setattr(helper, "_http", premature_probe) | ||
| with (helper.STATE / ".lock").open("a") as holder: | ||
| helper.fcntl.flock(holder, helper.fcntl.LOCK_EX) | ||
| with pytest.raises(helper.AccountLockTimeout, match=r"^\.lock is busy"): | ||
| helper.check(quiet=True) | ||
| with (helper.STATE / ".codex-sync.lock").open("a") as holder: | ||
| helper.fcntl.flock(holder, helper.fcntl.LOCK_EX) | ||
| operations = ( | ||
| lambda: helper.check(quiet=True), | ||
| lambda: helper.launch("codex", "model", {}), | ||
| lambda: helper.cmd_add("codex", "new"), | ||
| lambda: helper.cmd_pinned_exec("codex", helper.identity("codex", "b"), ["unused"]), | ||
| lambda: helper.exhausted("codex", helper.identity("codex", "main"), "model", retry_env={}), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Exercise the Codex retry through Calling |
||
| ) | ||
| for operation in operations: | ||
| with pytest.raises(helper.AccountLockTimeout, match=r"\.codex-sync\.lock is busy"): | ||
| operation() | ||
| assert elapsed == 360 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Check the timeout for each lock acquisition The 360-second total can pass even if one operation exceeds its deadline and another returns early. Capture the fake clock before each operation, including the separate |
||
| assert not helper.codex_home("new").exists() | ||
| assert helper.active("codex") == "main" | ||
| assert helper.active("claude") == "a" | ||
| assert json.loads(status.read_text()) == cooldown | ||
|
|
||
| acquire = helper._locked | ||
| home_lock_entries = [] | ||
|
|
||
| def count_home_lock(name): | ||
| if name == ".codex-sync.lock": | ||
| home_lock_entries.append(name) | ||
| return acquire(name) | ||
|
|
||
| monkeypatch.setattr(helper, "_locked", count_home_lock) | ||
| monkeypatch.setattr(helper, "check_claude", lambda _: {"tier": -1}) | ||
| monkeypatch.setattr(helper, "check_codex", lambda _: {"tier": -1}) | ||
| helper.check(quiet=True) | ||
| assert home_lock_entries == [".codex-sync.lock"] | ||
| helper.set_active("codex", "b") | ||
| assert helper.with_accounts("codex,codex", {})["CODEX_HOME"] == str(helper.codex_home("b")) | ||
| assert home_lock_entries == [".codex-sync.lock"] * 2 | ||
Uh oh!
There was an error while loading. Please reload this page.