Skip to content

Report custom source/target callback errors to libvips - #448

Closed
janko wants to merge 1 commit into
libvips:masterfrom
janko:custom-callback-errors
Closed

janko wants to merge 1 commit into
libvips:masterfrom
janko:custom-callback-errors

Conversation

@janko

@janko janko commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Fixes a few problems with custom sources and targets that I ran into while adding IO streaming to ImageProcessing.

Callback exceptions were treated as EOF. When an on_read or on_write block raised, MARSHAL_READ / MARSHAL_WRITE printed the exception to stdout and returned 0. For reads, libvips took that as EOF. For a network IO that failed partway, the result was a silently truncated image. The callbacks now return -1, so libvips sees a read or write error.

With this, a failed read makes WebP, TIFF, HEIF and GIF loads fail. Before, GIF was silently truncated, and the others failed with an unrelated message. The JPEG and PNG loaders still treat a failed read as a truncated file, which is only a warning unless fail_on: :truncated is set. Should libvips treat a source read error as a hard error regardless of fail_on? An I/O error isn't the same as a truncated file.

I first tried adding the exception message to the libvips error buffer, so that Vips::Error would include it. But when the JPEG or PNG loader swallows the error, the message stays in the buffer and ends up in the next unrelated Vips::Error, so I left that out.

Callback exceptions are printed with warn instead of puts. The message is a diagnostic, so it belongs on stderr: stdout may be carrying the image itself, e.g. when writing to a target on fd 1, and the message would end up in the middle of that data. warn also goes through Warning.warn, so apps can send it to their logger. The trade-off is that it's silenced when $VERBOSE is nil.

SourceCustom#on_read fixes:

  • It used return 0 if chunk.nil? inside the signal_connect block, which raised LocalJumpError at every EOF. It was caught and printed as read: unexpected return (seen in Random segfaults, lockups @ high CPU usage, and "read: unexpected return" messages #384). Changed to next 0.
  • It called chunk.clear on the returned string, which raises FrozenError for frozen strings. Before this change, that error was then treated as EOF. Frozen strings are now left alone.
  • It wrote the returned chunk into libvips' buffer without checking its size, so a block returning more than length bytes overflowed the buffer. On master this crashes the process with GLib-ERROR: adding 4097 to array would overflow. It now raises, which is reported as a read error.

🤖 Generated with Claude Code

* read and write callbacks that raise now return -1 instead of 0, so libvips
  sees an error rather than EOF or a zero-length write, and the exception is
  printed to stderr rather than stdout
* SourceCustom#on_read used `return` inside a block, which raised
  LocalJumpError at EOF (printing "read: unexpected return")
* SourceCustom#on_read called #clear on the returned chunk, which raised on
  frozen strings
* SourceCustom#on_read wrote past the buffer when the block returned more
  bytes than requested
@kleisauke

Copy link
Copy Markdown
Member

Please do not use LLMs to generate code or PR descriptions when contributing to libvips:
https://github.com/libvips/libvips/blob/master/CONTRIBUTING.md#ai-policy

Evidence

The GitHub audit log indicate that the contribution was made using Claude Code:

user_agent	GitHub CLI 2.95.0 Agent/claude-code_2-1-330_agent

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