From 986ca20d61b8659f4d1b401814a42cdd0bac77b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Janko=20Marohni=C4=87?= Date: Mon, 23 Nov 2026 10:20:05 +0100 Subject: [PATCH] Release the GVL during Region#fetch vips_region_fetch evaluates the pipeline just like copy_memory and write_to_memory, so other Ruby threads stall until it returns. With a SourceCustom image it deadlocks, as libvips calls the read callback from a worker thread, which needs the GVL held by the thread in fetch. --- CHANGELOG.md | 1 + lib/vips/region.rb | 4 +++- spec/region_spec.rb | 25 +++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aa13362c..b2cfbf8f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * raise Vips::Error if `copy_memory` fails [VitaliiLazebnyi] * `Source.new_from_memory` keeps a reference to the memory area [jeremy] * release the GVL during `copy_memory` and `write_to_memory` [andrewfader] +* release the GVL during `Region#fetch` [janko] ## Version 2.3.0 (2025-12-10) diff --git a/lib/vips/region.rb b/lib/vips/region.rb index 31479db7..73aaa30e 100644 --- a/lib/vips/region.rb +++ b/lib/vips/region.rb @@ -10,7 +10,9 @@ module Vips attach_function :vips_region_new, [:pointer], :pointer if Vips.at_least_libvips?(8, 8) - attach_function :vips_region_fetch, [:pointer, :int, :int, :int, :int, SizeStruct.ptr], :pointer + # fetch evaluates the pipeline, so release the GVL while it runs + attach_function :vips_region_fetch, [:pointer, :int, :int, :int, :int, SizeStruct.ptr], :pointer, + blocking: true attach_function :vips_region_width, [:pointer], :int attach_function :vips_region_height, [:pointer], :int end diff --git a/spec/region_spec.rb b/spec/region_spec.rb index cc14ee9f..9cd63052 100644 --- a/spec/region_spec.rb +++ b/spec/region_spec.rb @@ -26,4 +26,29 @@ expect(region.width == 30) expect(region.height == 40) end + + # fetch evaluates the pipeline, so it must release the GVL, or every other + # Ruby thread stalls until it returns + it "lets other threads run during fetch", version: [8, 8] do + image = Vips::Image.gaussnoise(2000, 2000).gaussblur(4) + region = Vips::Region.new image + + ticks = 0 + running = true + ticker = Thread.new do + while running + ticks += 1 + sleep 0.001 + end + end + sleep 0.01 until ticks > 0 + + before = ticks + region.fetch(0, 0, 2000, 2000) + during = ticks - before + running = false + ticker.join + + expect(during).to be > 0 + end end