avo icon indicating copy to clipboard operation
avo copied to clipboard

resource.model returns nil sporadically.

Open rajaravivarma-r opened this issue 3 years ago • 5 comments
trafficstars

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

rajaravivarma-r avatar Sep 13 '22 19:09 rajaravivarma-r

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.

adrianthedev avatar Sep 14 '22 10:09 adrianthedev

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.

rajaravivarma-r avatar Sep 14 '22 11:09 rajaravivarma-r

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!

adrianthedev avatar Sep 15 '22 07:09 adrianthedev

Sure, I'll make a recording in sometime.

rajaravivarma-r avatar Sep 16 '22 09:09 rajaravivarma-r

Here is the recording on how I open the links.

https://user-images.githubusercontent.com/1841235/190620368-1a761f52-e549-4789-b7d8-40c1b4db5172.mp4

rajaravivarma-r avatar Sep 16 '22 10:09 rajaravivarma-r

This issue has been marked as stale because there was no activity for the past 15 days.

github-actions[bot] avatar Oct 02 '22 04:10 github-actions[bot]

Closing this because there was no activity for the past 15 days. Feel free to reopen if new information pops up ✌️

github-actions[bot] avatar Oct 17 '22 04:10 github-actions[bot]

Commenting to keep this ticket open.

rajaravivarma-r avatar Oct 19 '22 13:10 rajaravivarma-r

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.

adrianthedev avatar Oct 19 '22 15:10 adrianthedev

I asked a question here https://github.com/ioquatix/ioquatix/discussions/17. If you have any ideas, please, let's get that conversation going.

adrianthedev avatar Oct 20 '22 08:10 adrianthedev

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.

rajaravivarma-r avatar Oct 20 '22 08:10 rajaravivarma-r

Thank you @rajaravivarma-r!

adrianthedev avatar Oct 20 '22 08:10 adrianthedev

@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.

adrianthedev avatar Nov 14 '22 15:11 adrianthedev

That would be great!

rajaravivarma-r avatar Nov 14 '22 17:11 rajaravivarma-r

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.

rajaravivarma-r avatar May 29 '23 17:05 rajaravivarma-r