Avoid recalculating CounterCache.counter_cached_association_names
#49917
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
After merging #49866, a flaky test started to appear - https://buildkite.com/rails/rails/builds/101543#018b9610-f396-4e76-90e9-5bae14521f1e and https://buildkite.com/rails/rails/builds/101530#018b9485-eef8-49a9-8826-be82230fe7fe
The failing line is the
rails/activerecord/test/cases/encryption/concurrency_test.rb
Line 22 in 8c82595
which calls down the road
rails/activerecord/lib/active_record/locking/optimistic.rb
Lines 160 to 162 in 8c82595
which calls
columns_hash
which always callsload_schema
which always recalculates and reassignscounter_cached_association_names
https://github.com/rails/rails/pull/49866/files#diff-0441501ea93f2580c66bc58d845fcc2ad9dc5110cce249ce184c0b59483e253bR188-R194Since the failing test from the referenced builds uses threads and
counter_cached_association_names
is defined as aclass_attribute
which is defined viarails/activesupport/lib/active_support/core_ext/class/attribute.rb
Line 108 in 8c82595
redefined_method
and which is defined asrails/activesupport/lib/active_support/core_ext/module/redefine_method.rb
Lines 17 to 22 in 8c82595
Lines 19-20 should be executed atomically, but I suspect, that Thread1 executes line 19, Thread2 executes line 19, then Thread1 executes line 20 and when Thread2 tries to execute line 20, it gets the mentioned error.
cc @byroot @nvasilevski