Fix problem with code reloading #127
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.
Fixes #126
The bug is caused by the presence of two things: Rails code reloading and Lit caching (storing models in globals).
Basically Lit stores instance of
Lit::locale(#111111)class in theLit.loader.cache(it's like a global). Then code is changed by developer and on the next request rails reloads it.Lit::Localeis also reloaded, so we have newLit::Locale(#222222)class now but instance of old class is still stored in the cache.And now we have code
localization.locale = locale, wherelocaleon the right side is outdated instance from the cache.localizationon the left side is using new reloaded code so is expecting newLit::Localeinstance.It's easy to reproduce:
Actually there is easier way to reproduce the problem. Run rails console and then:
I can think of 3 ways to fix it:
ActiveRecord::Baseto any global - it's rather not doableYou may be wondering why there weren't such errors before Thu Nov 22. On that date 1478d85 was introduced which added
localization.locale = localeline. So before that time outdatedLit::Localeobject was stored as well but it wasn't used to createLit::Localizationobjects so there were not any conflicts.BTW
I'm also wondering, shouldn't
Lit.loader.cachebe considered a memory leak in the current form? AR objects are pushed there but never removed in the whole process lifetime. I'm talking mainly about@localization_object_cacheand@localization_key_object_cache.Why are they needed? It seems they are added only when translation doesn't exist yet. For example:
Add to
PostsController#index:/posts-@localization_object_cacheand@localization_key_object_cachewill have 1000 AR objects each./posts- now@localization_object_cacheand@localization_key_object_cacheare empty.So why these cache vars are even needed if they don't seem to be used after persisting?
If this is needed for some reason then shouldn't it be stored per request or something like that?