Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -508,9 +508,10 @@ This file defines how coding agents should work in this repository.
worker startup can target an invalid ordinal.
- Treat public `gpu_ids` as visible device ordinals after user-supplied CUDA or ROCm visibility filtering. Do not rewrite visibility masks inside KeepGPU command paths; reject explicit CUDA/ROCm ordinals outside the current visible device count before starting keep workers.
- JSON-RPC and MCP tool `start_keep` requests with explicit `gpu_ids` outside
the current visible device count are public invalid-parameter errors
a non-empty current visible device set are public invalid-parameter errors
(`-32602` for direct JSON-RPC, MCP tool `isError=true`), not internal server
failures.
failures. If the current visible device count is zero, startup is
unavailable (`-32000` direct JSON-RPC) even when `gpu_ids` is explicit.
- Keep CUDA telemetry aligned with visible CUDA ordinals:
`get_gpu_utilization(index)` receives the visible rank used by
`CudaGPUController`, and `gpu_monitor.py` resolves `CUDA_VISIBLE_DEVICES`
Expand Down
3 changes: 2 additions & 1 deletion docs/guides/mcp.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ Omitting `gpu_ids` means all GPUs visible to the service process. Omitting
`busy_threshold` uses the eco-safe default `25`. Explicit GPU values are visible
device ordinals in that same process environment. Duplicate or out-of-range
lists are invalid, an explicit empty `gpu_ids` list is invalid, and startup
returns an error if the resolved selection contains zero devices. `interval`
is unavailable if no visible devices exist even when `gpu_ids` is explicit.
Startup also returns an error if the resolved selection contains zero devices. `interval`
must be a finite positive number of
seconds, including fractional seconds, within the Python runtime wait limit;
`NaN`, infinities, and oversized values are rejected before session creation.
Expand Down
2 changes: 2 additions & 0 deletions src/keep_gpu/global_gpu_controller/global_gpu_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ def _resolve_visible_gpu_ids(gpu_ids: Optional[List[int]]) -> List[int]:
visible_count = visible_torch_device_count()
except DeviceEnumerationUnavailableError as exc:
raise NoGPUAvailableError(str(exc)) from exc
if visible_count <= 0:
raise NoGPUAvailableError("No GPUs available for GlobalGPUController")
if gpu_ids is None:
return list(range(visible_count))
invalid_ids = [gpu_id for gpu_id in gpu_ids if gpu_id >= visible_count]
Expand Down
26 changes: 26 additions & 0 deletions tests/mcp/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,32 @@ def test_jsonrpc_start_keep_out_of_range_gpu_ids_returns_invalid_params(monkeypa
server.shutdown()


def test_jsonrpc_start_keep_explicit_gpu_ids_with_zero_visible_devices_is_startup_unavailable(
monkeypatch,
):
import torch

monkeypatch.setattr(pm, "_cached_platform", pm.ComputingPlatform.CUDA)
monkeypatch.setattr(torch.cuda, "device_count", lambda: 0)
server = KeepGPUServer()
monkeypatch.setattr(server, "list_gpus", lambda: {"gpus": []})
req = {
"id": 1,
"method": "start_keep",
"params": {"job_id": "explicit-zero-visible", "gpu_ids": [0]},
}

try:
resp = _handle_request(server, req)

assert "error" in resp
assert resp["error"]["code"] == JSONRPC_STARTUP_UNAVAILABLE
assert "No GPUs available" in resp["error"]["message"]
assert server.status()["active_jobs"] == []
finally:
server.shutdown()


def test_jsonrpc_start_keep_rejects_gpu_ids_hidden_from_listed_gpus(monkeypatch):
def fail_factory(**_kwargs):
raise AssertionError("controller should not start for unlisted gpu_ids")
Expand Down