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

JRuby fix for concurrency issue with manager cache #2798

Merged
merged 2 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions lib/new_relic/agent/configuration/manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,14 @@ def reset_to_defaults
def reset_cache
return new_cache unless defined?(@cache) && @cache

# Modifying the @cache hash under JRuby - even with a `synchronize do`
# block and a `Hash#dup` operation - has been known to cause issues
# with JRuby for concurrent access of the hash while it is being
# modified. The hash really only needs to be modified for the benefit
# of the security agent, so if JRuby is in play and the security agent
# is not, don't attempt to modify the hash at all and return early.
return @cache if NewRelic::LanguageSupport.jruby? && !Agent.config[:'security.agent.enabled']

@lock.synchronize do
preserved = @cache.dup.select { |_k, v| DEPENDENCY_DETECTION_VALUES.include?(v) }
new_cache
Expand Down
13 changes: 13 additions & 0 deletions test/new_relic/agent/configuration/manager_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,19 @@ def test_logger_does_not_receive_excluded_settings
refute_includes(log, ':license_key')
end

def test_reset_cache_return_early_for_jruby
phony_cache = {dup_called: false}
def phony_cache.dup; self[:dup_called] = true; self; end
@manager.instance_variable_set(:@cache, phony_cache)
NewRelic::LanguageSupport.stub :jruby?, true do
@manager.reset_cache
end

refute phony_cache[:dup_called], 'Expected the use of JRuby to prevent the Hash#dup call!'
ensure
@manager.new_cache
end

private

def assert_parsed_labels(expected)
Expand Down
Loading