Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions lib/google/cloud/env/lazy_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
90 changes: 90 additions & 0 deletions test/google/cloud/env/lazy_value_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading