pytest-dev/pytest · critical · OSError
could not create numbered dir with prefix {prefix} in {root}
Error message
could not create numbered dir with prefix {prefix} in {root} after 10 tries What it means
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.
Source
Thrown at src/_pytest/pathlib.py:283
pass
def make_numbered_dir(root: Path, prefix: str, mode: int = 0o700) -> Path:
"""Create a directory with an increased number as suffix for the given prefix."""
for i in range(10):
# try up to 10 times to create the directory
max_existing = max(map(parse_num, find_suffixes(root, prefix)), default=-1)
new_number = max_existing + 1
new_path = root.joinpath(f"{prefix}{new_number}")
try:
new_path.mkdir(mode=mode)
except Exception:
pass
else:
_force_symlink(root, prefix + "current", new_path)
return new_path
else:
raise OSError(
"could not create numbered dir with prefix "
f"{prefix} in {root} after 10 tries"
)
def create_cleanup_lock(p: Path) -> Path:
"""Create a lock to prevent premature directory cleanup."""
lock_path = get_lock_path(p)
try:
fd = os.open(str(lock_path), os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o644)
except FileExistsError as e:
raise OSError(f"cannot create lockfile in {p}") from e
else:
pid = os.getpid()
spid = str(pid).encode()
os.write(fd, spid)
os.close(fd)
if not lock_path.is_file():View on GitHub (pinned to 98b357f69e)
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
Example fix
// before # running in read-only /workspace // after pytest --cachedir=/tmp/.pytest_cache
Defensive patterns
Strategy: validation
Validate before calling
def assert_writable_cache(cache_dir):
from pathlib import Path
p = Path(cache_dir)
try:
p.mkdir(parents=True, exist_ok=True)
(p / '.write_probe').write_text('x')
(p / '.write_probe').unlink()
except OSError as e:
raise RuntimeError(f'cache dir {p} not writable: {e}') Type guard
def is_writable(p) -> bool:
from pathlib import Path
import tempfile
p = Path(p)
try:
with tempfile.TemporaryFile(dir=str(p)):
return True
except OSError:
return False Prevention
- Run pytest against a writable rootdir / cache dir
- In read-only CI containers set --cachedir to a writable tmp path
When it happens
Trigger: 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.
Common situations: CI runners with read-only workspace, containers mounting cache dirs read-only, full disk, NFS permission issues, SELinux denials.
Related errors
- cannot create lockfile in {p}
- lock path got renamed after successful creation
- {path}
- name is not allowed to contain path separators
- Directory '{rootdir}' not found. Check your '--rootdir' opti
AI-assisted analysis of pytest-dev/pytest@98b357f69e (2026-08-04).
Data as JSON: /data/errors/75c0ab83f8a3d2d5.json.
Report an issue: GitHub.