Skip to content

release the GVL during copy_memory and write_to_memory - #446

Merged
jcupitt merged 1 commit into
libvips:masterfrom
andrewfader:fix-gvl-memory
Oct 31, 2026
Merged

jcupitt merged 1 commit into
libvips:masterfrom
andrewfader:fix-gvl-memory

Conversation

@andrewfader

Copy link
Copy Markdown
Contributor

vips_image_copy_memory and vips_image_write_to_memory are attached without blocking: true, but both evaluate the whole lazy pipeline. FFI holds the GVL for the full computation, so every other Ruby thread in the process stops until they return. Only vips_cache_operation_build is attached blocking today.

We hit this in a Rails app running on Falcon, where requests are fibers on a few threads. Decoding thumbnails with copy_memory / write_to_memory in a background thread still froze every other request on that process. A thread ticking every 10ms saw gaps of 225-283ms during the call. Attaching both functions with blocking: true brought the gaps down to the normal ~15ms.

This change attaches both with blocking: true, the same way vips_cache_operation_build is attached.

Tests: two new examples in spec/image_spec.rb run a ticker thread while copy_memory / write_to_memory evaluate a gaussnoise.gaussblur pipeline, and check that the ticker advanced. Without the change the count is exactly 0 on every run, because the ticker can't get the GVL. With it, both pass, including 8/8 runs with every core busy. The full suite passes (128 examples) and standardrb is clean.

Both calls evaluate the whole lazy pipeline, but they were attached without
blocking: true, so FFI held the GVL for the full computation and every other
Ruby thread stalled until they returned.
@jcupitt

jcupitt commented Oct 31, 2026

Copy link
Copy Markdown
Member

Hi @andrewfader, thank you for finding and fixing this dumbness!

@jcupitt
jcupitt merged commit e80dbcd into libvips:master Oct 31, 2026
11 checks passed
@andrewfader
andrewfader deleted the fix-gvl-memory branch October 31, 2026 18:58
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