mirror of
https://github.com/FlashML-org/FreeToken.git
synced 2026-11-21 01:52:30 +08:00
* feat(server): require an API key on every route but /health * feat(shell): send the server's API key * feat(ctl): send the server's API key * feat(launch): hand the server's API key to the agent * feat(daemon): pass the serve's API key as apiKey The daemon reaches the serve's /v1/stats and prepare-stop itself, so the key travels in the start request, reaches the serve through its environment and is kept in the state file for re-adoption. The daemon's own FREETOKEN_API_KEY is no longer passed down to the serve. * fix(launch): keep the API key out of the variable dsh's web search reads dsh's web search reads DEEPSEEK_API_KEY and sends it to api.deepseek.com, so dsh now reads the key from FREETOKEN_API_KEY through the apiKeyEnv of its llm-deepseek section, and DEEPSEEK_API_KEY stays the dummy. * fix(server): check the API key on OPTIONS requests * fix(launch): write agent configs that hold the API key owner-only * chore: drop comments that restate the code
116 lines
3.7 KiB
Python
116 lines
3.7 KiB
Python
"""Single-instance lock + persisted serve state.
|
|
|
|
``SingleInstance`` is a flock-held pidfile: an advisory ``LOCK_EX | LOCK_NB`` on an fd we keep
|
|
open for the daemon's whole life. flock (not a bare port-bind) avoids TIME_WAIT races and is
|
|
released automatically if the daemon dies, so a crashed daemon never wedges its own restart.
|
|
|
|
``ServeStateStore`` persists ``{model, port, pid, args, starttime, log_path, api_key}`` as JSON on every
|
|
lifecycle change, so a restarted daemon can re-adopt a still-running serve. ``starttime`` +
|
|
``args`` are what make re-adoption PID-reuse-safe and config-exact.
|
|
The file is owner-only because it holds the serve's API key."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
from dataclasses import asdict, dataclass, field
|
|
|
|
|
|
class AlreadyRunning(RuntimeError):
|
|
"""Another daemon holds the single-instance lock."""
|
|
|
|
|
|
class SingleInstance:
|
|
def __init__(self, path: str) -> None:
|
|
self.path = path
|
|
self._fd: int | None = None
|
|
|
|
def acquire(self) -> None:
|
|
import fcntl # POSIX-only; the daemon's reference platform is Linux/WSL
|
|
|
|
os.makedirs(os.path.dirname(self.path) or ".", exist_ok=True)
|
|
fd = os.open(self.path, os.O_RDWR | os.O_CREAT, 0o644)
|
|
try:
|
|
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
|
except OSError as exc:
|
|
os.close(fd)
|
|
raise AlreadyRunning(f"another ft daemon holds {self.path}") from exc
|
|
os.ftruncate(fd, 0)
|
|
os.write(fd, f"{os.getpid()}\n".encode())
|
|
os.fsync(fd)
|
|
self._fd = fd
|
|
|
|
def release(self) -> None:
|
|
if self._fd is not None:
|
|
try:
|
|
os.close(self._fd) # closing drops the flock
|
|
except OSError:
|
|
pass
|
|
self._fd = None
|
|
|
|
def __enter__(self) -> "SingleInstance":
|
|
self.acquire()
|
|
return self
|
|
|
|
def __exit__(self, *exc) -> None:
|
|
self.release()
|
|
|
|
|
|
@dataclass
|
|
class ServeState:
|
|
model: str
|
|
port: int
|
|
pid: int
|
|
args: list[str]
|
|
starttime: int | None = None
|
|
log_path: str | None = None
|
|
api_key: str | None = field(default=None, repr=False)
|
|
|
|
|
|
class ServeStateStore:
|
|
def __init__(self, path: str) -> None:
|
|
self.path = path
|
|
|
|
def save(self, state: ServeState) -> None:
|
|
os.makedirs(os.path.dirname(self.path) or ".", exist_ok=True)
|
|
tmp = f"{self.path}.tmp"
|
|
# unlink first: O_CREAT applies the 0o600 mode only to a file it creates
|
|
try:
|
|
os.remove(tmp)
|
|
except FileNotFoundError:
|
|
pass
|
|
with os.fdopen(os.open(tmp, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600), "w") as fh:
|
|
json.dump(asdict(state), fh)
|
|
fh.flush()
|
|
os.fsync(fh.fileno())
|
|
os.replace(tmp, self.path) # atomic swap — never a torn/partial state file
|
|
|
|
def clear(self) -> None:
|
|
try:
|
|
os.remove(self.path)
|
|
except FileNotFoundError:
|
|
pass
|
|
except OSError: # pragma: no cover - defensive
|
|
pass
|
|
|
|
def load(self) -> ServeState | None:
|
|
try:
|
|
with open(self.path) as fh:
|
|
doc = json.load(fh)
|
|
except (FileNotFoundError, ValueError):
|
|
return None
|
|
except OSError: # pragma: no cover - defensive
|
|
return None
|
|
try:
|
|
return ServeState(
|
|
model=doc["model"],
|
|
port=int(doc["port"]),
|
|
pid=int(doc["pid"]),
|
|
args=list(doc.get("args") or []),
|
|
starttime=doc.get("starttime"),
|
|
log_path=doc.get("log_path"),
|
|
api_key=doc.get("api_key"),
|
|
)
|
|
except (KeyError, TypeError, ValueError):
|
|
return None
|