Skip to content

Capture errors in LayerInfo::write's parallel compress loop - #224

Merged
EmilDohne merged 1 commit into
EmilDohne:masterfrom
imdanberry:fix-compress-loop-terminate
Nov 1, 2026
Merged

EmilDohne merged 1 commit into
EmilDohne:masterfrom
imdanberry:fix-compress-loop-terminate

Conversation

@imdanberry

Copy link
Copy Markdown
Contributor

LayerInfo::write compresses each layer's channels inside
std::for_each(std::execution::par, …). An exception escaping a callable
run under std::execution::par calls std::terminate ([algorithms.parallel.exceptions]),
so any error raised while compressing — a codec failure, an allocation
failure, or PSAPI_LOG_ERROR's throw for an unsupported bit depth — ends
the host process instead of reaching the caller's catch.

This change wraps the per-layer body in a try / catch (...), records the
first exception under a mutex, and rethrows it once the loop has finished,
before any layer record is written. Behaviour on success is unchanged: the
same work runs in the same loop, and the output is byte-identical.

LayerInfo::read's parallel channel loop has the same exposure — a
truncated or corrupt channel throws inside it — and the same capture fixes
it there. Happy to send that as a follow-up.

Verification

Against a fresh clone of master at 51bb741, with only this change applied:

  • PhotoshopAPI/src/PhotoshopFile/LayerAndMaskInformation.cpp compiles with AppleClang on arm64 (-std=gnu++20, the flags of our own build), both normally and with -D_LIBCPP_REMOVE_TRANSITIVE_INCLUDES. No includes were added: the translation unit gets <mutex> from Util/Logger.h and <exception> from nlohmann/json's detail/exceptions.hpp (via compressed-image's channel.h), both explicit includes.
  • Limits: that is one translation unit only. I did not run the full library build or the test suite, and did not build with libstdc++ or MSVC.

In our fork the same change has been in a full rebuild (macOS arm64 / x86_64 and iOS arm64) and our test suite, where a test throws from inside this loop at every layer and requires a clean exception at the call site with the process still running.

Reporter context

Found while adding per-layer cancellation to exports in Flattener (an
iPad / Mac comics flatting app built on PhotoshopAPI): a cancel thrown from
that loop would have terminated the app, and so, today, does any
compression error that throws there.

🤖 Generated with Claude Code

An exception escaping a std::execution::par callable calls
std::terminate, so any error raised while compressing a layer's channels
(a codec or allocation failure, or PSAPI_LOG_ERROR's throw for an
unsupported bit depth) ended the host process instead of reaching the
caller. Capture the first exception and rethrow it once the loop has
finished, before any layer record is written. Behaviour on success is
unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@EmilDohne
EmilDohne merged commit ffd83ca into EmilDohne:master Nov 1, 2026
28 checks passed
@EmilDohne

Copy link
Copy Markdown
Owner

Thanks for the fix @imdanberry, LGTM I've merged it now. I'm working on some performance improvements and will create a new release once that is done!

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.

2 participants