GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(challenge): load the role-dispatch file on first use · CortexLM/hypertrain@a493bd5

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src/hypertrain/challenge
    • app.py
  • tests/challenge
    • test_role_dispatch_lazy.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/hypertrain/challenge/app.py‎

Copy file name to clipboardExpand all lines: src/hypertrain/challenge/app.py
+18-1Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import hmac
88
import json
99
import os
10+
import threading
1011
import time
1112
from collections.abc import Awaitable, Callable, Mapping
1213
from dataclasses import dataclass
@@ -278,6 +279,22 @@ async def read_model[M: BaseModel](request: Request, model: type[M]) -> M:
278279
raise ChallengeError(422, f"{where}: {first.get('msg', 'invalid')}") from None
279280

280281

282+
def _lazy_role_launch(path: Path) -> role_dispatch.RoleLaunch:
283+
"""Load on first use: the supervisor canary boots with an empty /data and must answer."""
284+
lock, loaded = threading.Lock(), list[role_dispatch.RoleLaunch]()
285+
286+
def launch(*args: Any) -> Any:
287+
with lock:
288+
if not loaded:
289+
try:
290+
loaded.append(role_dispatch.load(path))
291+
except (OSError, ValueError) as error: # pydantic ValidationError is a ValueError
292+
raise ChallengeError(503, "ROLE_DISPATCH_UNAVAILABLE") from error
293+
return loaded[0](*args)
294+
295+
return launch
296+
297+
281298
def create_app(
282299
config: Config,
283300
*,
@@ -310,7 +327,7 @@ def create_app(
310327
verify_beacon,
311328
objects or LocalFSStore(config.state_dir / "objects"),
312329
_role_launch=(
313-
role_dispatch.load(config.role_dispatch_file)
330+
_lazy_role_launch(config.role_dispatch_file)
314331
if config.role_dispatch_file is not None
315332
else None
316333
),
Collapse file

‎tests/challenge/test_role_dispatch_lazy.py‎

Copy file name to clipboard
+90Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
"""The supervisor canary boots the image with an empty /data: role dispatch loads on first use."""
2+
3+
from __future__ import annotations
4+
5+
import json
6+
import sys
7+
from pathlib import Path
8+
from types import SimpleNamespace
9+
10+
import pytest
11+
from fastapi.testclient import TestClient
12+
13+
from hypertrain.challenge import app as app_module
14+
from hypertrain.challenge.store import ChallengeError
15+
from hypertrain.gpu_ops import role_dispatch
16+
17+
from .conftest import OWNER, make_config
18+
19+
TREE = Path(__file__).parents[2]
20+
21+
22+
def _app(tmp_path: Path, dispatch: Path) -> app_module.FastAPI:
23+
# Canary: empty state dir, no secret files at all.
24+
config = make_config(tmp_path / "state", tmp_path / "no-secrets")
25+
config.role_dispatch_file = dispatch
26+
return app_module.create_app(config)
27+
28+
29+
def _write_valid(path: Path, tmp_path: Path) -> None:
30+
path.write_text(
31+
json.dumps(
32+
{
33+
"tree": str(TREE),
34+
"run_dir": str(tmp_path / "lifecycle"),
35+
"owner_key_file": str(tmp_path / "owner"),
36+
"roles": {OWNER.ss58: "h1"},
37+
"run_id": "a" * 64,
38+
}
39+
)
40+
)
41+
42+
43+
def _foreign(launch: role_dispatch.RoleLaunch, tmp_path: Path) -> None:
44+
# Foreign run: the real factory refuses before any I/O, proving the file was loaded.
45+
with pytest.raises(RuntimeError, match="not the authorized run"):
46+
launch("audit", SimpleNamespace(run_id="b" * 64), tmp_path, {}) # type: ignore[arg-type]
47+
48+
49+
def test_boots_and_answers_version_without_dispatch_file(tmp_path: Path) -> None:
50+
with TestClient(_app(tmp_path, tmp_path / "missing" / "dispatch.json")) as client:
51+
response = client.get("/version")
52+
assert response.status_code == 200
53+
assert response.json()["slug"] == "hypertrain"
54+
55+
56+
def test_missing_then_fixed_file_without_restart(tmp_path: Path) -> None:
57+
dispatch = tmp_path / "dispatch.json"
58+
launch = _app(tmp_path, dispatch).state.store._role_launch
59+
assert launch is not None
60+
for broken in (None, "{not json", json.dumps({"tree": str(TREE), "extra": 1})):
61+
if broken is not None:
62+
dispatch.write_text(broken)
63+
with pytest.raises(ChallengeError) as error:
64+
launch("audit", SimpleNamespace(run_id="a" * 64), tmp_path, {})
65+
assert (error.value.status, error.value.detail) == (503, "ROLE_DISPATCH_UNAVAILABLE")
66+
_write_valid(dispatch, tmp_path)
67+
_foreign(launch, tmp_path)
68+
69+
70+
def test_valid_file_loaded_once_same_behaviour(
71+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
72+
) -> None:
73+
dispatch = tmp_path / "dispatch.json"
74+
_write_valid(dispatch, tmp_path)
75+
calls: list[Path] = []
76+
real = role_dispatch.load
77+
78+
def spy(path: Path) -> role_dispatch.RoleLaunch:
79+
calls.append(path)
80+
return real(path)
81+
82+
monkeypatch.setattr(role_dispatch, "load", spy)
83+
monkeypatch.setattr(sys, "path", [p for p in sys.path if p != str(TREE / "scripts")])
84+
launch = _app(tmp_path, dispatch).state.store._role_launch
85+
assert launch is not None
86+
_foreign(launch, tmp_path)
87+
assert sys.path[0] == str(TREE / "scripts")
88+
_foreign(launch, tmp_path)
89+
_foreign(real(dispatch), tmp_path) # same refusal as the eager loader
90+
assert calls == [dispatch]

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.