bug: map import fails on 16384 px at tile size 64 #83

Closed
opened 2026-11-21 20:19:02 +01:00 by SirStone · 1 comment
Owner

Part of #63

Owner ran, with the server stopped:

devbox run -- ./run/fff-vtt import --state-dir=run/lan --image=run/map16384.png --tile-size=64

It ran ~5 min at 100% CPU, RSS climbed 4.2 -> 4.8 GB, wrote 0 usable tiles, then failed and cleaned up through failImport (server/importer.nim:251). The owner saw no progress output, so it looked hung. This is a regression of #79's "16384 px memory-sane" claim.

Diagnosis (reproduced)

server/importer.nim:245-251 runs ONE crop:

magick -limit memory 256MiB -limit map 512MiB map16384.png -crop 64x64 +repage +adjoin tile_%d.png

ImageMagick materialises all 65,536 cropped images in one image list before/while writing. Each live image has its own pixel cache (fd + mapping), and -limit memory/-limit map are per image, not global: the Q16-HDRI source cache alone is 16384^2 x 16 B = 4.29 GB and stays resident (observed RSS 8.3 GB once tiles were in flight). Reproduction with ulimit -n 1024 on a copy of the owner file:

  • 20 s: 771 open fds, RSS 2.1 GB, 0 tiles
  • 160 s: 771 fds, RSS 4.3 GB, 0 tiles
  • 300 s: 1023 fds (soft limit), RSS 8.3 GB, 59,312 tiles
  • fail: UnableToOpenFile '/tmp/magick-...': Too many open files @ error/cache.c/ReadPixelCachePixels/4860 -> No IDATs written into file .../tile_13334.png -> exit 1

Fix requirements

  • Stream the source once and cut bounded bands (one tile row at a time) so live pixel caches and fds are bounded by the worker count, not by the tile count.
  • Peak RSS bounded (< 1 GB) and fd count independent of image size.
  • Measured wall time for 16384/64 on this 16-core box (a few minutes max).
  • Visible progress on stderr (rows 37/256, tiles 9472/65536) and a clear failure message including the underlying magick stderr.
  • Atomicity unchanged (temp dir -> rename, scene row only on success, lock semantics, exit codes).
  • Tiles identical to the old path on small images (byte- or pixel-identical), proven.

Closes the #77 blocker.

Part of #63 Owner ran, with the server stopped: ``` devbox run -- ./run/fff-vtt import --state-dir=run/lan --image=run/map16384.png --tile-size=64 ``` It ran ~5 min at 100% CPU, RSS climbed 4.2 -> 4.8 GB, wrote 0 usable tiles, then failed and cleaned up through `failImport` (server/importer.nim:251). The owner saw no progress output, so it looked hung. This is a regression of #79's "16384 px memory-sane" claim. ## Diagnosis (reproduced) `server/importer.nim:245-251` runs ONE crop: ``` magick -limit memory 256MiB -limit map 512MiB map16384.png -crop 64x64 +repage +adjoin tile_%d.png ``` ImageMagick materialises all 65,536 cropped images in one image list before/while writing. Each live image has its own pixel cache (fd + mapping), and `-limit memory/-limit map` are *per image*, not global: the Q16-HDRI source cache alone is 16384^2 x 16 B = 4.29 GB and stays resident (observed RSS 8.3 GB once tiles were in flight). Reproduction with `ulimit -n 1024` on a copy of the owner file: - 20 s: 771 open fds, RSS 2.1 GB, 0 tiles - 160 s: 771 fds, RSS 4.3 GB, 0 tiles - 300 s: 1023 fds (soft limit), RSS 8.3 GB, 59,312 tiles - fail: `UnableToOpenFile '/tmp/magick-...': Too many open files @ error/cache.c/ReadPixelCachePixels/4860` -> `No IDATs written into file .../tile_13334.png` -> exit 1 ## Fix requirements - Stream the source once and cut bounded bands (one tile row at a time) so live pixel caches and fds are bounded by the worker count, not by the tile count. - Peak RSS bounded (< 1 GB) and fd count independent of image size. - Measured wall time for 16384/64 on this 16-core box (a few minutes max). - Visible progress on stderr (`rows 37/256, tiles 9472/65536`) and a clear failure message including the underlying magick stderr. - Atomicity unchanged (temp dir -> rename, scene row only on success, lock semantics, exit codes). - Tiles identical to the old path on small images (byte- or pixel-identical), proven. Closes the #77 blocker.
SirStone added the bugready-for-agent labels 2026-11-21 20:19:06 +01:00
SirStone added a new dependency 2026-11-21 20:19:06 +01:00
Author
Owner

Fixed in c8b5289 on main (fix: stream map import so 16384 px at 64 px is memory- and fd-bounded).

Cause confirmed: -limit memory/-limit map are per image, and the one-shot -crop ... +adjoin held 65,536 tile images plus the 4.29 GB Q16-HDRI source cache. Reproduced on a copy of the owner file with ulimit -n 1024: 771 fds at 20 s (RSS 2.1 GB, 0 tiles), 1023 fds at 300 s (RSS 8.3 GB, 59,312 tiles), then Too many open files @ cache.c/ReadPixelCachePixels -> exit 1.

Design: one magick decode to raw samples (disk-backed pixel cache under -limit memory 32MiB -limit map 32MiB), the CLI reads the source one tile row at a time into a scratch band file, and a pool of 8 magick children (no pipes, poParentStreams) cuts each band into tiles. Live pixel caches and fds are bounded by the worker count, never by the tile count. Progress on stderr (rows 37/256, tiles 9472/65536); failures quote the image, the row band and magick's own message. Atomicity, lock and exit codes unchanged; a tile row too large to stream falls back to the one-shot crop (then small by construction).

Measured, 16 cores:

  • owner's run/map16384.png (copy) at 64 px: 19 s, 391 MiB peak RSS, 10 fds/process, 65,536 tiles
  • generated 16384x16384 RGB at 64 px: 15-16 s, 486 MiB, 71 fds total
  • decoder alone: 52 MiB RSS
  • small fixtures: pixel-identical to the old one-shot crop, tile by tile (RGB 8-bit, partial edges, 16-bit, RGBA, grayscale, WebP +/- alpha)

Tests: tools/import.test.mjs keeps the fast fixtures and adds the pixel-identity proof and a truncated-source failure case; new devbox run import-scale runs 16384/64 under ulimit -n 256 and leaves results/import-scale.log (devbox run test skips it, ~25 s). README import section documents the progress output and the ~20 s/400 MB expectation.

Gates: devbox run test (167 pass, 1 skipped), client-test (183), e2e (29) all green.

Fixed in c8b5289 on main (`fix: stream map import so 16384 px at 64 px is memory- and fd-bounded`). **Cause confirmed**: `-limit memory/-limit map` are per image, and the one-shot `-crop ... +adjoin` held 65,536 tile images plus the 4.29 GB Q16-HDRI source cache. Reproduced on a copy of the owner file with `ulimit -n 1024`: 771 fds at 20 s (RSS 2.1 GB, 0 tiles), 1023 fds at 300 s (RSS 8.3 GB, 59,312 tiles), then `Too many open files @ cache.c/ReadPixelCachePixels` -> exit 1. **Design**: one `magick` decode to raw samples (disk-backed pixel cache under `-limit memory 32MiB -limit map 32MiB`), the CLI reads the source one tile row at a time into a scratch band file, and a pool of 8 `magick` children (no pipes, `poParentStreams`) cuts each band into tiles. Live pixel caches and fds are bounded by the worker count, never by the tile count. Progress on stderr (`rows 37/256, tiles 9472/65536`); failures quote the image, the row band and magick's own message. Atomicity, lock and exit codes unchanged; a tile row too large to stream falls back to the one-shot crop (then small by construction). **Measured, 16 cores**: - owner's `run/map16384.png` (copy) at 64 px: **19 s, 391 MiB peak RSS, 10 fds/process, 65,536 tiles** - generated 16384x16384 RGB at 64 px: 15-16 s, 486 MiB, 71 fds total - decoder alone: 52 MiB RSS - small fixtures: pixel-identical to the old one-shot crop, tile by tile (RGB 8-bit, partial edges, 16-bit, RGBA, grayscale, WebP +/- alpha) **Tests**: `tools/import.test.mjs` keeps the fast fixtures and adds the pixel-identity proof and a truncated-source failure case; new `devbox run import-scale` runs 16384/64 under `ulimit -n 256` and leaves `results/import-scale.log` (`devbox run test` skips it, ~25 s). README import section documents the progress output and the ~20 s/400 MB expectation. Gates: `devbox run test` (167 pass, 1 skipped), `client-test` (183), `e2e` (29) all green.
Sign in to join this conversation.