ruby-vips icon indicating copy to clipboard operation
ruby-vips copied to clipboard

[UIFontDescriptor initialize] may have been in progress in another thread when fork() was called

Open erdemgezer opened this issue 9 months ago • 9 comments

Having interesting errors both on Mac and Ubuntu (Docker) while adding text on top of an image using ruby-vips.

text = Vips::Image.text(
        text_content,
        width: width,
        height: height,
        align: vips_align,
        font: font_name,
        fontfile: font_file,
        spacing: line_spacing
      )
image.composite(text, :over, x: x, y: y)

On my mac development environment, If I call this method from an ActiveJob, it fails with:

objc[25970]: +[UIFontDescriptor initialize] may have been in progress in another thread when fork() was called.
objc[25970]: +[UIFontDescriptor initialize] may have been in progress in another thread when fork() was called. We cannot safely call it or ignore it in the fork() child process. Crashing instead. Set a breakpoint on objc_initializeAfterForkError to debug.

Using of env var OBJC_DISABLE_INITIALIZE_FORK_SAFETY=yes fixes it as suggested in https://github.com/libvips/ruby-vips/issues/155#issuecomment-951665377

However, on Ubuntu docker environment I randomly get segmentation fault errors or floating point exception errors when running the from Rails console.

Everthing works fine if I don't use the .text method in both environments.

Mac stack MacOS 15.3.1 (M2) Ruby 3.3.5 Rails 7.2.1 vips-8.16.0

Ubuntu stack Dockerized x86_64 Linux 116.202.15.119-1f5530ef76b3 6.8.0-54-generic Using libvips42 package Ruby 3.3.5 Rails 7.2.1

erdemgezer avatar Mar 12 '25 12:03 erdemgezer

Hi @erdemgezer,

There have been some improvements to locking in text rendering in recent libvipses. Have you tried your Ubuntu example with current stable libvips (8.16.1)?

Text rendering is extremely complex, unfortunately, and interacts with a huge number of different libraries and system components on the various platforms :(

jcupitt avatar Mar 13 '25 11:03 jcupitt

Hi @erdemgezer,

There have been some improvements to locking in text rendering in recent libvipses. Have you tried your Ubuntu example with current stable libvips (8.16.1)?

Text rendering is extremely complex, unfortunately, and interacts with a huge number of different libraries and system components on the various platforms :(

Hi @jcupitt , Thanks for letting me know about the latest stable release. I used to install libvips via apt-get from the Dockerfile but I believe I need to build it from source to be able to use the latest version. I'll give it a go and share the results.

erdemgezer avatar Mar 13 '25 22:03 erdemgezer

@erdemgezer Were you able to make any progress with this?

Linux 116.202.15.119-1f5530ef76b3 6.8.0-54-generic

For triage purposes: this looks like Ubuntu 24.04, which provides libvips 8.15.1.

kleisauke avatar Apr 11 '25 13:04 kleisauke

@kleisauke I've tried building latest libvips 8.16.1 but didn't help with random segmentation fault errors on Ubuntu.

erdemgezer avatar Apr 11 '25 14:04 erdemgezer

Does this only occur if you run it from the Rails console? Can you reproduce this without the fontfile argument? I tried this:

$ docker run -e DEBIAN_FRONTEND=noninteractive -v $(pwd):/src -w /src -it ubuntu:24.04 sh -c "\
    apt-get update -y && \
    apt-get install -y --no-install-recommends libvips-tools && \
    vips text hello_world.png '<span foreground=\"#ff5500\" background=\"#c8c8c8\">Hello, world!</span>' --width 500 --height 500 --dpi 300 --rgba"

Which produces: Image

So at least that seems to work without any issues.

kleisauke avatar Apr 11 '25 14:04 kleisauke

I think it only occurs when I use the fontfile argument and run from Rails console. I'll try to reproduce again and let you know asap.

erdemgezer avatar Apr 11 '25 14:04 erdemgezer

Note to self: check if this is related to Rails (or Puma) its fork() behaviour. Perhaps the Pango thread here is spawned before the fork() call?

kleisauke avatar Apr 11 '25 15:04 kleisauke

@kleisauke it's also randomly failing when it's called from a SolidQueue Job (ActiveJob). It usually works on the second retry.

Type | SolidQueue::Processes::ProcessExitError
Process pid=46 exited unexpectedly. Received unhandled signal 8.

erdemgezer avatar Apr 15 '25 09:04 erdemgezer

I might have identified the underlying issue, see PR https://github.com/libvips/libvips/pull/4542.

kleisauke avatar May 28 '25 08:05 kleisauke

@erdemgezer Were you able to make any progress with this? Note that PR https://github.com/libvips/libvips/pull/4542 was landed in libvips v8.17.0.

kleisauke avatar Jul 03 '25 12:07 kleisauke