Skip to content

bug(fs): scan fails when a file disappears during the walk #11679

Open

Description

@nikpivkin

trivy fs and trivy rootfs fail and write no report when a file is removed while Trivy walks the filesystem. On busy machines this happens often, because build output, package manager caches and temp files come and go during the scan. Scanning the host from a container fails the same way on /proc entries of processes that exit during the scan.

Steps to reproduce

dir="$(mktemp -d)"
mkdir "$dir/scan" "$dir/cache"
for i in $(seq 1 2000); do : > "$dir/scan/keep-$i.tmp"; done
mkdir "$dir/scan/churn"
(cd "$dir/scan/churn" && while :; do touch $(seq -f 'c%g.tmp' 1 2000); rm -f -- c*.tmp; done) &
churn=$!
for run in 1 2 3 4 5 6 7 8; do
  trivy fs --format cyclonedx --cache-dir "$dir/cache" --output /dev/null "$dir/scan" || break
done
kill "$churn"

The loop stops at the first failed run with this error.

walk dir error: unknown error with <dir>/scan/churn/c151.tmp: file info error: lstat <dir>/scan/churn/c151.tmp: no such file or directory

The expected result is that Trivy skips the missing file and scans the rest of the tree.

A file can disappear at three points, and each of them fails the scan.

  1. The walker calls lstat through d.Info() in pkg/fanal/walker/fs.go, and onError stops the walk on any error except permission errors. The same happens when a subdirectory disappears before it is read.
  2. AnalyzeFile in pkg/fanal/analyzer/analyzer.go opens the file for each analyzer and skips only permission errors.
  3. Post-analyzers read the files through mapfs after the whole analysis step. ReadDir in pkg/mapfs/file.go calls os.Stat for every file in the directory, so one missing file fails the whole directory. fsutils.WalkDir also stops when Open fails. The post-analyzer returns the error, and PostAnalyze fails the scan.

Skipping every "not exist" error is not enough. Trivy has no separate check that the scan target exists, and today trivy fs /nonexistent fails only because of the same walk error. The fix must skip missing files inside the tree but still fail when the target itself does not exist.

Related discussions: #11670, #7554

Activity

  1. added
    kind/bug
    on Nov 19, 2026
  2. self-assigned this
    on Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions