LoadImageDataSetFromFolder loads images in os.listdir order (filesystem-dependent), scrambling frame sequences
- Dominant language
- Python
- Stars
- 133k
- Forks
- 15.7k
- Avg merge
- 1d 7h
- Merged PRs (30d)
- 158
Description
### Affected
`comfy_extras/nodes_dataset.py` — `LoadImageDataSetFromFolderNode.execute()` (and the same pattern in `LoadImageTextDataSetFromFolderNode`, including its kohya-style subfolder branch).
Observed on ComfyUI v0.32.0, macOS (APFS), Python 3.12.
### Bug
`execute()` builds its file list directly from `os.listdir()`:
```python
image_files = [
f
for f in os.listdir(sub_input_dir)
if any(f.lower().endswith(ext) for ext in valid_extensions)
]
```
`os.listdir()` order is explicitly undefined and filesystem-dependent. On APFS it returns filename-hash order, so a frame sequence `frame_000001.png ... frame_000145.png` is loaded in an effectively scrambled order, e.g.:
```
frame_000117.png, frame_000103.png, frame_000088.png, frame_000063.png, frame_000077.png, ...
```
The node's output IMAGE list (and anything downstream — `RebatchImages` into a single batch, per-frame inference, `SaveImageAdvanced` with its sequential counter) inherits that order.
### Impact
- Per-frame outputs (e.g. depth per frame) are written under counters that do not correspond to the input frame numbers — output `frame_00070` is not input frame 70.
- Any temporally-aware downstream node silently computes garbage: video trackers (e.g. SAM3_VideoTrack) track across a shuffled sequence, optical flow runs between unrelated frame pairs, etc. No error is raised anywhere.
- The behavior is platform-dependent: on filesystems that happen to return sorted or insertion order the graph works, so the bug ships silently and only appears on other systems.
### Repro
1. `mkdir ComfyUI/input/seq && cd ComfyUI/input/seq` and create numbered frames (any small PNGs) `frame_000001.png ... frame_000020.png` on macOS/APFS.
2. `python3 -c "import os; print([f for f in os.listdir('.') if f.endswith('.png')])"` — observe non-sorted order.
3. Run a graph `LoadImageDataSetFromFolder -> SaveImage`: outputs are numbered in that scrambled order, not frame order.
### Suggested fix
Sort the listing (matches what `get_input_subfolders`' sibling helper in the same file already does with `return sorted(found)`):
```python
image_files = sorted(
f
for f in os.listdir(sub_input_dir)
if any(f.lower().endswith(ext) for ext in valid_extensions)
)
```
Same one-line change applies to `LoadImageTextDataSetFromFolderNode`'s two `os.listdir()` sites (its image/caption pairing is name-derived so sorting is safe there too).
Happy to open a PR with the above if useful.
Contributor guide
Research direction
Start in comfy_extras/nodes_dataset.py at LoadImageDataSetFromFolderNode.execute() and the two os.listdir() sites in LoadImageTextDataSetFromFolderNode, including its kohya-style subfolder branch. Compare them with the sorted get_input_subfolders helper and reproduce the numbered-frame case on a filesystem with non-sorted listing order. Done means all relevant image listings are deterministic while image-caption pairing remains intact.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 84/100