Better double checked locking in load_schema #37288
Merged
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.
So we've witnessed some weird issues in production. Some
serialized
attributes ended up not being deserialized from time to time.After investigating a bit, it end up being the fallout of the following exception:
What happened here is that a call to
connection.lookup_cast_type_from_column
raises and error, which leave the model with@columns_hash
set, but not all attribute methods etc defined and@schema_loaded = false
.So effectively the model is only half-way initialized, but further calls to
load_schema
bail out early because@columns_hash
is set.Fix
So this is a very straightforward change, we just make sure that the condition used for bailing out early is one that prove everything was properly initialized.
This is not perfect though, because the model still end up in an inconsistent state, however it will recover on the next
load_schema
call. What we could do would be to add arescue
block to unset@column_hash
and others. Let me know what you think about this.Testing
I'd like to add a regression test for this, but to reproduce the error I think I need some stubbing helper, and I'm having a hard time to understand the ones available in Rails test suite.
@etiennebarrie @deuxpi @rafaelfranca @Edouard-chin @eileencodes @seejohnrun