Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(FACT-2390) Use thread local to check recursion #1890

Merged
merged 1 commit into from
Feb 21, 2020
Merged
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
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this not a boolean to start?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we use it as a key to a value in the map of current thread vars. We don't change it to a boolean later, but rather the value it points to.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh oh oh wow, reading is hard 🤦‍♀


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