{"id":"75c0ab83f8a3d2d5","repo":"pytest-dev/pytest","slug":"could-not-create-numbered-dir-with-prefix-prefix","errorCode":null,"errorMessage":"could not create numbered dir with prefix {prefix} in {root} after 10 tries","messagePattern":"could not create numbered dir with prefix (.+?) in (.+?) after 10 tries","errorType":"exception","errorClass":"OSError","httpStatus":null,"severity":"critical","filePath":"src/_pytest/pathlib.py","lineNumber":283,"sourceCode":"        pass\n\n\ndef make_numbered_dir(root: Path, prefix: str, mode: int = 0o700) -> Path:\n    \"\"\"Create a directory with an increased number as suffix for the given prefix.\"\"\"\n    for i in range(10):\n        # try up to 10 times to create the directory\n        max_existing = max(map(parse_num, find_suffixes(root, prefix)), default=-1)\n        new_number = max_existing + 1\n        new_path = root.joinpath(f\"{prefix}{new_number}\")\n        try:\n            new_path.mkdir(mode=mode)\n        except Exception:\n            pass\n        else:\n            _force_symlink(root, prefix + \"current\", new_path)\n            return new_path\n    else:\n        raise OSError(\n            \"could not create numbered dir with prefix \"\n            f\"{prefix} in {root} after 10 tries\"\n        )\n\n\ndef create_cleanup_lock(p: Path) -> Path:\n    \"\"\"Create a lock to prevent premature directory cleanup.\"\"\"\n    lock_path = get_lock_path(p)\n    try:\n        fd = os.open(str(lock_path), os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o644)\n    except FileExistsError as e:\n        raise OSError(f\"cannot create lockfile in {p}\") from e\n    else:\n        pid = os.getpid()\n        spid = str(pid).encode()\n        os.write(fd, spid)\n        os.close(fd)\n        if not lock_path.is_file():","sourceCodeStart":265,"sourceCodeEnd":301,"githubUrl":"https://github.com/pytest-dev/pytest/blob/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/pathlib.py#L265-L301","documentation":"When creating a numbered directory (used by pytest cache and tmp_path factories), pytest tries up to 10 times to compute the next number and mkdir. If every attempt fails it raises OSError. Indicates the destination is not writable or the filesystem rejects creation.","triggerScenarios":"Numbered-dir creation (e.g. under .pytest_cache or tmp_path rootdir) failing 10 consecutive times: permission denied on rootdir, read-only mount, or quota exhaustion.","commonSituations":"CI runners with read-only workspace, containers mounting cache dirs read-only, full disk, NFS permission issues, SELinux denials.","solutions":["Ensure write permission on the rootdir / cache directory","Point --cachedir (or TMPDIR) at a writable location, or use -p no:cacheprovider to disable cache","Free disk space / fix the underlying filesystem error"],"exampleFix":"// before\n# running in read-only /workspace\n// after\npytest --cachedir=/tmp/.pytest_cache","handlingStrategy":"validation","validationCode":"def assert_writable_cache(cache_dir):\n    from pathlib import Path\n    p = Path(cache_dir)\n    try:\n        p.mkdir(parents=True, exist_ok=True)\n        (p / '.write_probe').write_text('x')\n        (p / '.write_probe').unlink()\n    except OSError as e:\n        raise RuntimeError(f'cache dir {p} not writable: {e}')","typeGuard":"def is_writable(p) -> bool:\n    from pathlib import Path\n    import tempfile\n    p = Path(p)\n    try:\n        with tempfile.TemporaryFile(dir=str(p)):\n            return True\n    except OSError:\n        return False","tryCatchPattern":null,"preventionTips":["Run pytest against a writable rootdir / cache dir","In read-only CI containers set --cachedir to a writable tmp path"],"tags":["pathlib","cache","filesystem","permissions","pytest"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}