pytest-dev/pytest · error · OSError

The temporary directory {rootdir} is not owned by the curren

Error message

The temporary directory {rootdir} is not owned by the current user. Fix this and try again.

What it means

Raised by TempPathFactory.getbasetemp as an OSError when the basetemp root (/tmp/pytest-of-<user>) exists but its owning uid does not match the current process uid. Because the temp root lives under a shared /tmp, pytest enforces ownership to prevent another user from pre-creating the directory (a known DoS / data-leak vector) before this user writes private test artifacts into it.

Source

Thrown at src/_pytest/tmpdir.py:194

            # TOCTOU vulnerability.
            # This check makes us vulnerable to a DoS - a user can `mkdir
            # /tmp/pytest-of-otheruser` and then `otheruser` will fail this
            # check. For now we don't consider it a real problem. otheruser can
            # change their TMPDIR or --basetemp, and maybe give the prankster a
            # good scolding.
            uid = get_user_id()
            if uid is not None:
                stat_follow_symlinks = (
                    False if os.stat in os.supports_follow_symlinks else True
                )
                rootdir_stat = rootdir.stat(follow_symlinks=stat_follow_symlinks)
                if stat.S_ISLNK(rootdir_stat.st_mode):
                    raise OSError(
                        f"The temporary directory {rootdir} is a symbolic link. "
                        "Fix this and try again."
                    )
                if rootdir_stat.st_uid != uid:
                    raise OSError(
                        f"The temporary directory {rootdir} is not owned by the current user. "
                        "Fix this and try again."
                    )
                if (rootdir_stat.st_mode & 0o077) != 0:
                    chmod_follow_symlinks = (
                        False if os.chmod in os.supports_follow_symlinks else True
                    )
                    rootdir.chmod(
                        rootdir_stat.st_mode & ~0o077,
                        follow_symlinks=chmod_follow_symlinks,
                    )
            keep = self._retention_count
            if self._retention_policy == "none":
                keep = 0
            basetemp = make_numbered_dir_with_cleanup(
                prefix="pytest-",
                root=rootdir,
                keep=keep,

View on GitHub (pinned to 98b357f69e)

Solutions

  1. Delete the stale directory: `sudo rm -rf /tmp/pytest-of-<user>` so pytest recreates it as the current user.
  2. Run pytest with `--basetemp=<dir>` owned by the current user, or set TMPDIR to a directory you own.
  3. Run the test process as the same uid that owns /tmp/pytest-of-<user> (e.g. drop sudo).
  4. In Dockerfiles, avoid creating /tmp/pytest-of-* at build time.

Example fix

// before
$ sudo -u bob pytest tests/   # /tmp/pytest-of-bob owned by uid 1000, sudo makes you uid 0

# OSError: not owned by the current user

// after
$ sudo rm -rf /tmp/pytest-of-bob
$ pytest tests/   # recreates as current user
Defensive patterns

Strategy: validation

Validate before calling

import os, pathlib, getpass

def ensure_basetemp_owned(p: pathlib.Path):
    p.mkdir(parents=True, exist_ok=True)
    if os.stat(p).st_uid != os.getuid():
        raise OSError(f"{p} not owned by uid {os.getuid()}; chown or remove it")
    return p

Type guard

import os

def is_owned_by_me(p) -> bool:
    try:
        return os.stat(p).st_uid == os.getuid()
    except OSError:
        return False

Prevention

When it happens

Trigger: A different OS user previously ran pytest and created /tmp/pytest-of-<user>, then the current process runs as a different uid (e.g. via sudo, container user remap, or a username collision across machines). The ownership check fails at the first tmp_path usage.

Common situations: Running tests as root after a non-root user created the dir; container images that bake in a /tmp/pytest-of-* from a build stage; shared dev boxes where multiple humans share a username but different uids; CI that runs some jobs as one user and others as another.

Related errors


AI-assisted analysis of pytest-dev/pytest@98b357f69e (2026-08-04). Data as JSON: /data/errors/4339b8d650adc2d1.json. Report an issue: GitHub.