avo
avo copied to clipboard
resource.model returns nil sporadically.
Describe the bug
In a field declaration such as,
field :redemption_bank_account_number, as: :text, hide_on: :index, visible: ->(resource:) { resource.model.redemption_mode != 'NORMAL' }
resource.model returns nil sporadically, but mostly happens when lots of pages are loaded.
When I refresh the page it loads fine. I am attaching a screen recording for your reference.
Steps to Reproduce
Cannot reproduce this consistently.
Expected behavior & Actual behavior
Page should load fine, but doesn't sometimes.
Models and resource files
class Redemption < Order
attribute :investment_date, :indian_datetime
default_scope -> { where(amc_order_type: %w[REDEMPTION REDEEM_ALL]) }
end
class RedemptionResource < Avo::BaseResource
self.title = :uuid
self.includes = [:fund]
self.search_query = lambda { |params:|
scope.ransack(id_eq: params[:q], uuid_eq: params[:q], m: 'or').result(distinct: false)
}
self.search_query_help = '- search by id and old id'
self.model_class = ::Redemption
field :redemption_bank_account_number, as: :text, hide_on: :index,
visible: ->(resource:) { resource.model.redemption_mode != 'NORMAL' }
end
System configuration
Avo version: 2.14.0
Rails version: 6.1.6.1
Ruby version: 3.0.0p0
License type:
- [ ] Community
- [x] Pro
Screenshots
The following screen recording depicts how resource.model returns nil despite being available in the web-console. And how refreshing the page fixes it.
https://user-images.githubusercontent.com/1841235/189988246-cbb252e5-a46d-46f7-babb-ad726a378669.mp4
Additional context
We switch Postgres schemas in config/initializers/avo.rb as follows,
Avo.configure do |config|
config.set_context do
if current_user
tenant_name = current_user.tenant.name
RequestStore.store[:tenant] = tenant_name
RequestStore.store[:environment] = AVO_SCOPE
RequestStore.store[:current_user] = current_user
Order.connection.schema_search_path = tenant_name
{ user: current_user, tenant: tenant_name }
end
end
I doubt if this could be a reason for this behaviour, but I'm not sure. I cannot replicate this in a demo app with a similar setup, but I'm leaving it here for your awareness.
Impact
- [ ] High impact
- [x] Medium impact
- [ ] Low impact
Urgency
- [ ] High urgency
- [x] Medium urgency
- [ ] low urgency
And how is this sporadic? I mean, does it always raise that error on update (and then it has the mode present)? Or it sometimes passes and all is good?
I'm asking because it might be that the error is raised in a different point than the one indicate on the crash page and the model being present in the console os a false positive.
It is not an update. Usually this happens when I click open a lot of links (from the listing page) in a new tab. Some of the pages will load fine, but some will raise errors like this. When refreshed, the page will load just fine.
the model being present in the console os a false positive.
Yeah, I just saw the screen recording again and the model has a different id from the one that we are trying to access. The model in the console has id 67812, but we are trying to access the one with id 67813.
Could you make a recording of how you open multiple records at once? I understand the process, I just want to "get the feel" of that and make sure it's sporadically so I don't go chasing something I don't fully understand, and to be able to reproduce it.
Thank you!
Sure, I'll make a recording in sometime.
Here is the recording on how I open the links.
https://user-images.githubusercontent.com/1841235/190620368-1a761f52-e549-4789-b7d8-40c1b4db5172.mp4
This issue has been marked as stale because there was no activity for the past 15 days.
Closing this because there was no activity for the past 15 days. Feel free to reopen if new information pops up ✌️
Commenting to keep this ticket open.
Hey @rajaravivarma-r. I think I figured it out. It's related to the fact that I keep some global things on class attributes and that messes up concurrent requests.
The temporary fix is to use just one thread in puma. Doing that will decrease your app's throughput so it helps if you bump up the workers value.
max_threads_count = ENV.fetch("RAILS_MAX_THREADS", 1)
min_threads_count = ENV.fetch("RAILS_MIN_THREADS") { max_threads_count }
threads min_threads_count, max_threads_count
workers ENV.fetch('WEB_CONCURRENCY') { 5 }
I am working on refactoring that class attributes and make Avo more thread-safe.
I asked a question here https://github.com/ioquatix/ioquatix/discussions/17. If you have any ideas, please, let's get that conversation going.
Hey @adrianthedev, thanks. This happens rarely that I'll keep the concurrency as it is.
I'll see what I can contribute for the conversation at ioquatix.
Thank you @rajaravivarma-r!
@rajaravivarma-r We'll fix this in Avo 3.0.
We already started, and we made good progress on this issue. A much better architecture using the Current model that is thread-safe.
That would be great!
Hi @adrianthedev, I know it about the fix coming in version 3. But can you please back port it to version 2, because we increase the number of threads, the problem keeps increasing resulting in a lot of 500s in our system.
I know we can increase the number of puma workers, but we would like to keep the cost low, so we cannot move to a CPU optimised server, more processes more cores and so on.