Skip to content

Commit

Permalink
Merge pull request #1890 from pcarlisle/fact-2390-recursion-threads
Browse files Browse the repository at this point in the history
(FACT-2390) Use thread local to check recursion
  • Loading branch information
mwaggett authored Feb 21, 2020
2 parents 9936ce7 + cce1fb4 commit f81a706
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/facter/util/fact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ class Facter::Util::Fact
# @api private
def initialize(name, options = {})
@name = name.to_s.downcase.intern
@recursion_key = "facter_searching_#{@name}".intern

extract_ldapname_option!(options)

@ldapname ||= @name.to_s

@resolves = []
@searching = false

@value = nil
end
Expand Down Expand Up @@ -132,19 +132,27 @@ def extract_ldapname_option!(options)

# Are we in the midst of a search?
def searching?
@searching
Thread.current[@recursion_key]
end

def mark_searching
Thread.current[@recursion_key] = true
end

def end_searching
Thread.current[@recursion_key] = false
end

# Lock our searching process, so we never ge stuck in recursion.
def searching
raise RuntimeError, "Caught recursion on #{@name}" if searching?

# If we've gotten this far, we're not already searching, so go ahead and do so.
@searching = true
mark_searching
begin
yield
ensure
@searching = false
end_searching
end
end

Expand Down

0 comments on commit f81a706

Please sign in to comment.