Skip to content

Autonomous AI sales agent powered by Claude Code or Codex. Finds prospects, writes cold emails, sends campaigns, handles replies, books meetings. No API keys needed — runs on your Claude or Codex subscriptions.

Fix Windows dashboard token startup read race

Ethan+ffd2d8a2ba62
diff --git a/harvey/dashboard_auth.py b/harvey/dashboard_auth.py
index 2b92f18..77bb732 100644
--- a/harvey/dashboard_auth.py
+++ b/harvey/dashboard_auth.py
@@ -82,6 +82,10 @@ class _ExtraLinkError(TokenError):
dashboard publishes it: see _publish_token)."""
+class _WindowsReadBusyError(TokenError):
+ """Windows refused an open that may overlap token publication."""
+
+
# ── Token file ──
@@ -117,7 +121,14 @@ def _read_token(path: Path) -> str | None:
if e.errno in (errno.ELOOP, errno.EMLINK):
raise TokenError(f"{path} is a symbolic link. Remove it and restart "
"the dashboard.") from e
- raise TokenError(f"Can't read {path}: {e.strerror}.") from e
+ error_type = TokenError
+ # The CRT can reduce sharing violations to EACCES without winerror.
+ # Real ACL denials use the same code; they still fail closed after
+ # the bounded settling window, without replacing the token.
+ if _IS_WINDOWS and (e.errno == errno.EACCES
+ or getattr(e, "winerror", None) in (5, 32, 33)):
+ error_type = _WindowsReadBusyError
+ raise error_type(f"Can't read {path}: {e.strerror}.") from e
try:
_check_open_file(fd, path)
data = os.read(fd, 4097)
@@ -174,12 +185,15 @@ def _publish_token(path: Path, token: str):
def _read_token_settled(path: Path) -> str | None:
- """_read_token, waiting briefly (about 0.5s) for a concurrent publisher
- to drop its temporary link. A link that stays is still refused."""
+ """Wait at most 50 short intervals for publication-related read failures.
+
+ Persistent access errors or extra links are still refused. Token contents
+ and all other file-security checks are never retried or relaxed.
+ """
for _ in range(50):
try:
return _read_token(path)
- except _ExtraLinkError:
+ except (_ExtraLinkError, _WindowsReadBusyError):
time.sleep(0.01)
return _read_token(path)
diff --git a/tests/test_dashboard_auth.py b/tests/test_dashboard_auth.py
index e92c4a0..f683e64 100644
--- a/tests/test_dashboard_auth.py
+++ b/tests/test_dashboard_auth.py
@@ -9,6 +9,7 @@ persistence, loopback-only binding, a real server started as the CLI does,
and the browser-side token handling under node.
"""
+import errno
import json
import os
import re
@@ -447,7 +448,8 @@ def test_token_is_stable_and_unique_per_install(tmp_path):
assert auth.load_or_create_token(tmp_path / "b" / "t") != a
-def test_concurrent_creation_yields_one_token(tmp_path):
[email protected]("round_number", range(10))
+def test_concurrent_creation_yields_one_token(tmp_path, round_number):
path = tmp_path / "data" / "dashboard-token"
results, errors = [], []
barrier = threading.Barrier(8)
@@ -463,11 +465,107 @@ def test_concurrent_creation_yields_one_token(tmp_path):
for t in threads:
t.start()
for t in threads:
- t.join()
- assert not errors and len(set(results)) == 1
+ t.join(timeout=10)
+ assert not any(t.is_alive() for t in threads)
+ assert not errors and len(results) == 8 and len(set(results)) == 1
assert [p.name for p in path.parent.iterdir()] == ["dashboard-token"]
[email protected]("error_code, winerror", [
+ (errno.EACCES, None), (errno.EINVAL, 5), (errno.EINVAL, 32), (errno.EINVAL, 33),
+])
+def test_windows_transient_token_open_recovers_without_replacing(tmp_path, monkeypatch, error_code, winerror):
+ path = tmp_path / "dashboard-token"
+ original = auth.load_or_create_token(path)
+ before = path.read_bytes()
+ real_open = auth.os.open
+ calls, sleeps = [], []
+
+ def open_busy(name, flags, *args, **kwargs):
+ if Path(name) == path:
+ calls.append(name)
+ if len(calls) <= 2:
+ error = OSError(error_code, "Permission denied")
+ if winerror is not None:
+ error.winerror = winerror
+ raise error
+ return real_open(name, flags, *args, **kwargs)
+
+ monkeypatch.setattr(auth, "_IS_WINDOWS", True)
+ monkeypatch.setattr(auth.os, "open", open_busy)
+ monkeypatch.setattr(auth.time, "sleep", sleeps.append)
+ monkeypatch.setattr(auth, "_publish_token", lambda *a: pytest.fail("existing token must not be replaced"))
+ assert auth.load_or_create_token(path) == original
+ assert len(calls) == 3 and sleeps == [0.01, 0.01]
+ assert path.read_bytes() == before
+ assert list(tmp_path.iterdir()) == [path]
+
+
[email protected]("windows, expected_attempts", [(True, 51), (False, 1)])
+def test_token_open_denied_fails_closed_with_bounded_retry(tmp_path, monkeypatch, windows, expected_attempts):
+ path = tmp_path / "dashboard-token"
+ auth.load_or_create_token(path)
+ before = path.read_bytes()
+ attempts, sleeps = [], []
+ denied = PermissionError(errno.EACCES, "Permission denied")
+
+ def open_denied(*args, **kwargs):
+ attempts.append(1)
+ raise denied
+
+ monkeypatch.setattr(auth, "_IS_WINDOWS", windows)
+ monkeypatch.setattr(auth.os, "open", open_denied)
+ monkeypatch.setattr(auth.time, "sleep", sleeps.append)
+ monkeypatch.setattr(auth, "_publish_token", lambda *a: pytest.fail("denial must not create a token"))
+ with pytest.raises(auth.TokenError, match="Can't read") as caught:
+ auth.load_or_create_token(path)
+ assert caught.value.__cause__ is denied
+ assert len(attempts) == expected_attempts
+ assert sleeps == [0.01] * (expected_attempts - 1)
+ assert path.read_bytes() == before
+ assert list(tmp_path.iterdir()) == [path]
+
+
+def test_windows_unrelated_open_error_is_not_retried(tmp_path, monkeypatch):
+ path = tmp_path / "dashboard-token"
+ auth.load_or_create_token(path)
+ before = path.read_bytes()
+ error = OSError(errno.EIO, "I/O error")
+
+ def fail_open(*args, **kwargs):
+ raise error
+
+ monkeypatch.setattr(auth, "_IS_WINDOWS", True)
+ monkeypatch.setattr(auth.os, "open", fail_open)
+ monkeypatch.setattr(auth.time, "sleep", lambda *a: pytest.fail("I/O failure must fail immediately"))
+ monkeypatch.setattr(auth, "_publish_token", lambda *a: pytest.fail("read errors must not replace the token"))
+ with pytest.raises(auth.TokenError, match="Can't read") as caught:
+ auth.load_or_create_token(path)
+ assert caught.value.__cause__ is error
+ assert path.read_bytes() == before
+
+
[email protected]("failure", ["malformed", "symlink", "security"])
+def test_windows_validation_errors_are_not_retried(tmp_path, monkeypatch, failure):
+ path = tmp_path / "dashboard-token"
+ auth.load_or_create_token(path)
+ if failure == "malformed":
+ path.write_text("invalid")
+ before = path.read_bytes()
+ monkeypatch.setattr(auth, "_IS_WINDOWS", True)
+ monkeypatch.setattr(auth.time, "sleep", lambda *a: pytest.fail("validation must fail immediately"))
+ monkeypatch.setattr(auth, "_publish_token", lambda *a: pytest.fail("invalid token must not be replaced"))
+ if failure == "symlink":
+ monkeypatch.setattr(auth.os.path, "islink", lambda p: True)
+ elif failure == "security":
+ def reject(*args):
+ raise auth.TokenError("not a regular file")
+ monkeypatch.setattr(auth, "_check_open_file", reject)
+ with pytest.raises(auth.TokenError):
+ auth.load_or_create_token(path)
+ assert path.read_bytes() == before
+
+
def _expect_locked(path, match):
with pytest.raises(auth.TokenError, match=match):
auth.load_or_create_token(path)