From f5fd457986ad11cd223b7ec6ce8752adbed13b03 Mon Sep 17 00:00:00 2001 From: Wangmerlyn Date: Thu, 2 Jul 2026 17:48:50 +0000 Subject: [PATCH] fix(rpc): classify zero visible GPUs as unavailable --- AGENTS.md | 5 ++-- docs/guides/mcp.md | 3 ++- .../global_gpu_controller.py | 2 ++ tests/mcp/test_server.py | 26 +++++++++++++++++++ 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 580328c..fb717da 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -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` diff --git a/docs/guides/mcp.md b/docs/guides/mcp.md index 1772b0a..7aaeac2 100644 --- a/docs/guides/mcp.md +++ b/docs/guides/mcp.md @@ -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. diff --git a/src/keep_gpu/global_gpu_controller/global_gpu_controller.py b/src/keep_gpu/global_gpu_controller/global_gpu_controller.py index e9c21f2..9b09ded 100644 --- a/src/keep_gpu/global_gpu_controller/global_gpu_controller.py +++ b/src/keep_gpu/global_gpu_controller/global_gpu_controller.py @@ -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] diff --git a/tests/mcp/test_server.py b/tests/mcp/test_server.py index 215ddea..80874c6 100644 --- a/tests/mcp/test_server.py +++ b/tests/mcp/test_server.py @@ -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")