From 24dc268f89c6cb5922b1b838fa601582d30c4a03 Mon Sep 17 00:00:00 2001 From: HolyWalley Date: Wed, 28 Oct 2026 14:29:41 +0100 Subject: [PATCH] fix: LazyValue no longer raises ThreadError for concurrent fibers under a fiber scheduler LazyValue identified the computing caller by Thread.current. Under a fiber scheduler (Falcon, Async) every fiber on a thread shares it, so a second fiber calling #get during another fiber's computation was taken for re-entry and raised "deadlock: tried to call LazyValue#get from its own computation". Under a scheduler the caller is now the fiber; without one it stays the thread, so thread-only code behaves as before. --- lib/google/cloud/env/lazy_value.rb | 33 ++++++--- test/google/cloud/env/lazy_value_test.rb | 90 ++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 10 deletions(-) diff --git a/lib/google/cloud/env/lazy_value.rb b/lib/google/cloud/env/lazy_value.rb index 28db2e3..e254810 100644 --- a/lib/google/cloud/env/lazy_value.rb +++ b/lib/google/cloud/env/lazy_value.rb @@ -225,12 +225,13 @@ def initialize retries: nil, &block # The number of threads waiting on backfill. Used to determine # whether to activate backfill_notify when a computation completes. @backfill_count = 0 - # The thread running the current computation. This is tested against - # new requests to protect against deadlocks where a thread tries to - # re-enter from its own computation. This is also tested when a - # computation completes, to ensure that the computation is still - # relevant (i.e. if #set! interrupts a computation, this is reset to - # nil). + # The thread running the current computation (or the fiber, when a + # fiber scheduler is active; see #current_owner). This is tested + # against new requests to protect against deadlocks where a thread + # tries to re-enter from its own computation. This is also tested + # when a computation completes, to ensure that the computation is + # still relevant (i.e. if #set! interrupts a computation, this is + # reset to nil). @computing_thread = nil end @@ -520,7 +521,7 @@ def wait_backfill # This must be called from within the mutex. # def wait_compute - if Thread.current.equal? @computing_thread + if current_owner.equal? @computing_thread raise ThreadError, "deadlock: tried to call LazyValue#get from its own computation" end @backfill_count += 1 @@ -532,13 +533,25 @@ def wait_compute end end + ## + # @private + # Returns the object that identifies the current caller for the + # purpose of tracking which caller is computing. Under a fiber + # scheduler, fibers on the same thread wait for each other + # independently, so each fiber is its own caller. Otherwise, a waiting + # fiber blocks its whole thread, so the thread is the caller. + # + def current_owner + Fiber.scheduler ? Fiber.current : Thread.current + end + ## # @private # Initializes compute mode. # This must be called from within the mutex. # def enter_compute cur_time - @computing_thread = Thread.current + @computing_thread = current_owner @compute_notify = Thread::ConditionVariable.new @expires_at = cur_time @value = @error = nil @@ -591,7 +604,7 @@ def handle_success value expires_at = determine_expiry value.lifetime value = value.value end - if Thread.current.equal? @computing_thread + if current_owner.equal? @computing_thread @retries.finish! @error = nil @value = value @@ -615,7 +628,7 @@ def handle_failure error expires_at = determine_expiry error.lifetime error = error.cause end - if Thread.current.equal? @computing_thread + if current_owner.equal? @computing_thread retry_delay = @retries.next start_time: @expires_at @value = nil @error = error diff --git a/test/google/cloud/env/lazy_value_test.rb b/test/google/cloud/env/lazy_value_test.rb index bc2296c..27d8461 100644 --- a/test/google/cloud/env/lazy_value_test.rb +++ b/test/google/cloud/env/lazy_value_test.rb @@ -17,6 +17,59 @@ require "helper" require "google/cloud/env/lazy_value" +# A minimal Fiber::Scheduler: enough for fibers that sleep or wait on a +# Mutex or ConditionVariable, all on one thread. Scheduled fibers run until +# they first block; the rest runs when the thread exits and calls #close. +class TestFiberScheduler + def initialize + @ready = [] + # Blocked fibers, mapped to the monotonic time they wake at, or to nil if + # they wait for #unblock. + @blocked = {} + end + + def fiber(&block) + fiber = Fiber.new blocking: false, &block + fiber.resume + fiber + end + + def kernel_sleep duration = nil + @blocked[Fiber.current] = duration && (now + duration) + Fiber.yield + end + + def block _blocker, timeout = nil + kernel_sleep timeout + end + + def unblock _blocker, fiber + @blocked.delete fiber + @ready << fiber + end + + def io_wait _io, _events, _timeout + raise NotImplementedError + end + + def close + until @ready.empty? && @blocked.empty? + @ready.shift.resume until @ready.empty? + next if @blocked.empty? + wake_at = @blocked.values.compact.min + raise "TestFiberScheduler: every fiber is blocked" unless wake_at + sleep [wake_at - now, 0].max + @blocked.select { |_fiber, time| time && time <= now }.each_key { |fiber| unblock nil, fiber } + end + end + + private + + def now + Process.clock_gettime Process::CLOCK_MONOTONIC + end +end + describe Google::Cloud::Env::LazyValue do describe "#get" do it "returns the correct value" do @@ -295,6 +348,43 @@ assert_equal "deadlock: tried to call LazyValue#get from its own computation", err.message end + it "causes fibers under a fiber scheduler to wait if a block is already running" do + count = 0 + cache = Google::Cloud::Env::LazyValue.new do + sleep 0.1 + count += 1 + 4 + end + values = [] + Thread.new do + Fiber.set_scheduler TestFiberScheduler.new + 3.times do + Fiber.schedule do + values << cache.get + end + end + end.join + assert_equal [4, 4, 4], values + assert_equal 1, count + end + + it "does not allow fiber re-entry under a fiber scheduler" do + cache = nil + cache = Google::Cloud::Env::LazyValue.new do + cache.get + end + err = nil + Thread.new do + Fiber.set_scheduler TestFiberScheduler.new + Fiber.schedule do + err = assert_raises ThreadError do + cache.get + end + end + end.join + assert_equal "deadlock: tried to call LazyValue#get from its own computation", err.message + end + it "returns an expiring value" do count = 0 cache = Google::Cloud::Env::LazyValue.new do