Skip to content

Handle unknown UID/GID in _OsBackend.stat() gracefully#789

Open
bzantium wants to merge 1 commit into
google:mainfrom
bzantium:fix/handle-unknown-uid-in-stat
Open

Handle unknown UID/GID in _OsBackend.stat() gracefully#789
bzantium wants to merge 1 commit into
google:mainfrom
bzantium:fix/handle-unknown-uid-in-stat

Conversation

@bzantium

@bzantium bzantium commented Apr 9, 2026

Copy link
Copy Markdown

Summary

  • _OsBackend.stat() calls pwd.getpwuid() and grp.getgrgid() to resolve file owner/group names, but raises KeyError when the UID/GID is not present in /etc/passwd or /etc/group.
  • This is common in container environments and shared filesystems (e.g. NFS) where files may be owned by UIDs that don't exist locally.
  • Falls back to the numeric UID/GID as a string, consistent with how ls -n and other standard tools handle unknown UIDs.

Fixes #790

Reproduction

# In a container where files are owned by UID 1000 but /etc/passwd has no entry for it:
from etils import epath
p = epath.Path("/some/file/owned/by/unknown/uid")
p.stat()  # KeyError: 'getpwuid(): uid not found: 1000'

Impact

This crashes downstream libraries like orbax-checkpoint which call epath.Path.stat() internally during checkpoint loading.

Test plan

  • Verified fix resolves the crash in orbax-checkpoint on a shared filesystem with unknown UIDs
  • Existing behavior unchanged for known UIDs

In container and shared filesystem environments, files may be owned by
UIDs/GIDs that are not present in /etc/passwd or /etc/group. The current
code calls pwd.getpwuid() and grp.getgrgid() without handling KeyError,
causing crashes in downstream libraries like orbax-checkpoint.

Fall back to the numeric UID/GID string when the name lookup fails,
consistent with how standard tools like `ls -n` handle this case.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

KeyError in _OsBackend.stat() for unknown UID/GID in container environments

1 participant