{"record":{"id":"f7a12af6fa0495d8","repo":"PrefectHQ/fastmcp","slug":"could-not-read-cli-state-path-name","errorCode":null,"errorMessage":"Could not read CLI state: {path.name}","messagePattern":"Could not read CLI state: (.+?)","errorType":"exception","errorClass":"StateFileError","httpStatus":null,"severity":"error","filePath":"fastmcp_slim/fastmcp/cli/deploy/state.py","lineNumber":166,"sourceCode":"    *,\n    secret: bool = False,\n) -> ModelT | None:\n    \"\"\"Read and validate a versioned JSON state file.\"\"\"\n    if not path.exists():\n        return None\n    if path.is_symlink():\n        raise StateFileError(f\"CLI state must not be a symbolic link: {path.name}\")\n\n    if secret:\n        _restrict_access(path.parent, directory=True)\n        _restrict_access(path)\n\n    try:\n        return model.model_validate_json(path.read_text(encoding=\"utf-8\"))\n    except (ValidationError, ValueError):\n        raise StateFileError(f\"CLI state is invalid: {path.name}\") from None\n    except OSError as exc:\n        raise StateFileError(f\"Could not read CLI state: {path.name}\") from exc\n\n\ndef write_state(path: Path, data: dict[str, Any]) -> None:\n    \"\"\"Write JSON through a restricted temporary file and atomic replacement.\"\"\"\n    _prepare_directory(path.parent)\n    payload = (json.dumps(data, indent=2, sort_keys=True) + \"\\n\").encode()\n    descriptor: int | None = None\n    temporary_path: Path | None = None\n\n    try:\n        descriptor, temporary_name = tempfile.mkstemp(\n            dir=path.parent,\n            prefix=f\".{path.name}.\",\n            suffix=\".tmp\",\n        )\n        temporary_path = Path(temporary_name)\n        if os.name != \"nt\":\n            os.fchmod(descriptor, 0o600)","sourceCodeStart":148,"sourceCodeEnd":184,"githubUrl":"https://github.com/PrefectHQ/fastmcp/blob/1f021142978e0861cd910c8df4e8074bc7cf3978/fastmcp_slim/fastmcp/cli/deploy/state.py#L148-L184","documentation":"StateFileError raised by read_state when path.read_text raises OSError after the existence and symlink checks pass. The file is present but unreadable at the OS level, so the CLI cannot load the state and reports the filename in the message.","triggerScenarios":"read_state (via load) on a file whose permissions deny the current user read access (e.g. state created under another account, or a prior _restrict_access left it 0o600 owned by root), or an I/O error while reading (failing disk, file deleted between exists() and read).","commonSituations":"Running the CLI with sudo previously so the state file is now root-owned; different user account after a machine migration; EFS/encrypted-volume errors; race where another process removed the file mid-read.","solutions":["Fix ownership/permissions on the file: chown $USER or chmod u+r on the state file (and 0o700 on its directory)","Stop mixing elevated and normal runs (avoid sudo for CLI commands, or chown the state back afterwards)","Delete the unreadable file and re-authenticate to regenerate it","Check disk/health (dmesg) if I/O errors persist"],"exampleFix":"// before (root-owned after sudo run)\n-rw------- root root ~/.fastmcp/state/session.json\n// after\n$ sudo chown $USER ~/.fastmcp/state/session.json","handlingStrategy":"validation","validationCode":"import os\nfrom pathlib import Path\n\ndef assert_readable(path: Path) -> None:\n    if path.exists() and not os.access(path, os.R_OK):\n        raise RuntimeError(f\"{path} is not readable; fix ownership/permissions\")","typeGuard":null,"tryCatchPattern":"try:\n    state = load(state_path)\nexcept StateFileError as exc:\n    if \"Could not read\" in str(exc):\n        subprocess.run([\"chown\", str(os.getuid()), str(state_path)], check=False)\n        state = load(state_path)\n    else:\n        raise","preventionTips":["Avoid running the CLI with sudo (root-owned 0o600 state is unreadable later)","After using elevated runs, chown the state directory back to your user","Keep state on a healthy local disk; investigate repeated I/O errors","Check file readability after account/machine migrations"],"tags":["permissions","filesystem","io"],"backgroundTag":"file-permission-denied","analyzedSha":"1f021142978e0861cd910c8df4e8074bc7cf3978","analyzedAt":"2026-08-29T14:31:16.082Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}