New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
uninitialized constant ActiveRecord::Associations::HasManyThroughAssociation #26273
Comments
Can you create an executable test script using one of the templates here, that would demonstrate the problem you are seeing? |
Looking through our issue archives, this doesn't seem to have appeared in the past. Could be related to constant loading...? cc @fxn |
@maclover7 unfortunately, I don't think that would do much good, as we've only seen it once and I don't have any way of reproducing it. We saw another constant loading error recently, but it was in the aws-sdk gem, so seems unrelated. |
This smells to a missing require or autoload. Rails constant autoloading (dependencies.rb) is unrelated, because it only works for application code, not for framework code. It could be the case that the file where the This could be happening only occasionally because it could depend on the runtime execution order. If a require or autoload is missing but the constant is already loaded by luck when execution reaches that line, the issue does not show up. |
Still seeing this error coming up in various places, also on |
For what it's worth, we saw a similar error happen immediately after restarting our app servers: Have not been able to reproduce it since. Rails 5.0.0.1, Ruby 2.3.1p112 |
Is this still happening on 5.1.0? |
@rafaelfranca we just got this last night on 5.0.2 https://sentry.io/share/issue/3133343238302e323536333030333334/ |
Is this a job processor? Does sidekiq eager loaded your application boot? That class should be loaded as soon the first model with |
We've been seeing these kinds of errors a lot more frequently recently. Still on
No deploys or restarts at that time, though I can't account for puma_worker_killer. It's in a web dyno, not a background job. Here's a stack trace:
|
For us it's Rails 5 in a web process, not worker. |
Guys, is there a workaround for this? We also get this error all the time in production. |
We're seeing this in Rails 4 on Heroku, web processes. |
Seeing this on my app pretty regularly (in a rake task) in production with
|
We just saw this for the first time.
|
We were expecting those classes to be loaded when an association is defined but they are not. If you add a debugger on the first line of any of those classes and try to define the corresponding association you will see that the classes are not loaded. Fixes #26273
We were expecting those classes to be loaded when an association is defined but they are not. If you add a debugger on the first line of any of those classes and try to define the corresponding association you will see that the classes are not loaded. Fixes #26273
That might fix it, let's see. But if it does, I don't get why. The error message tells us the constant is being accessed and it is not found. If the There has to be something special with this particular constant, because if I am not mistaken this has not been reported for the other association types. |
It is because threads are also on the mix. Two threads are trying to autoload the same constant, one is succeeding, the other is not because Ruby started to loaded the constant but didn't finish before it is being used. |
We've seen it for a handful of association types, as well as that |
@rafaelfranca In theory it should not matter |
That is not what I'm seeing unfortunately. When I reproduced this error I had two threads doing the same thing and the first one finished and the second one didn't. |
I also checked if the |
I think we eager load framework code that we know is going to be used to have as much framework code as possible in the master process, to be CoW-friendly. Don't think it is related to thread-safety. At least, it shouldn't matter if |
Oh, that makes sense. |
For the record, we've started seeing this from time to time since we upgraded to Rails 5.1. It did not occur in 4.x or 5.0 for us. This seems to be a (somewhat) reliable pattern to reproduce the error: class SomeJob
include Sidekiq::Worker
def perform
Record.find_each do |record|
OtherJob.perform_async record.id
# OtherJob then tries to access an association of record
end
end
end |
I'm still seeing occasional uninitialized constant errors in 5.2.1, with the following constants:
For now we've added an initializer to force them to load: ActiveSupport.on_load(:active_record) do
# preload common constants to work around https://github.com/rails/rails/issues/26273
ActiveRecord::StatementCache
ActiveRecord::Associations::JoinDependency::JoinBase
ActiveRecord::ConnectionAdapters::TransactionManager
end |
It's unclear what caused the issue, but we got a one-off error when loading a page in our application:
The line of code that triggered the error:
Stack trace:
System configuration
Rails version: 4.2.7.1
Ruby version: 2.3.1p112
The text was updated successfully, but these errors were encountered: