Memcache/interlock problem (i think?)

Hey all

I have a line of code that’s causing a “undefined method `include?’ for
nil:NilClass” exception, but i can’t work out what’s actually going
wrong with it. After a bit of experimentation i discovered that it’s
caused by simply asking for the id of an AR object i have.

      ldb "question.id = #{question.id}"

ldb is just a logging method i have - it’s not the problem as i got the
same error initially when i tried to use question.id in a find call.
Here’s the line with some other logging output and the stack trace:

      ldb "options = #{options.inspect}"
      ldb "question = #{question.inspect}"
      ldb "question.id = #{question.id}"

stack trace and debug logger output:

./app/models/quiz.rb:458:in `auto_generate_quizzes_from_questions’:

options = {:age_group_id=>3, :style=>“sequential”, :tag=>#<Tag id: 60,
name: “speaking”, created_at: nil>, :size=>6}

./app/models/quiz.rb:459:in `auto_generate_quizzes_from_questions’:

question = #<Question id: 619, text: “Which of these words has only one
syllable?”, kind: “text”, media_instruction_text: nil, qid: “619”,
editor_id: 22, editor_name: “Ian McNeilly”, user_id: 15, author_ext_id:
“22”, original_target_age: “11”, qtype: “Curriculum”, keywords: “— \n-
Curriculum\n- English\n- School\n- Which\n- has\n…”, created_at:
“2010-02-25 13:20:02”, updated_at: “2010-04-01 16:17:03”, checked:
false, subject_id: 1, official: nil, privacy: 1>

NoMethodError (undefined method include?' for nil:NilClass): lib/core_extensions.rb:6:inmethod_missing’
app/models/quiz.rb:460:in auto_generate_quizzes_from_questions' app/models/quiz.rb:456:ineach’
app/models/quiz.rb:456:in auto_generate_quizzes_from_questions' app/controllers/admin/subjects_controller.rb:9:inmake_quizzes’
vendor/plugins/haml/rails/./lib/sass/plugin/rack.rb:44:in call' /usr/lib/ruby/1.8/mongrel/rails.rb:76:inprocess’
/usr/lib/ruby/1.8/mongrel/rails.rb:74:in synchronize' /usr/lib/ruby/1.8/mongrel/rails.rb:74:inprocess’
/usr/lib/ruby/1.8/mongrel.rb:159:in process_client' /usr/lib/ruby/1.8/mongrel.rb:158:ineach’
/usr/lib/ruby/1.8/mongrel.rb:158:in process_client' /usr/lib/ruby/1.8/mongrel.rb:285:inrun’
/usr/lib/ruby/1.8/mongrel.rb:285:in initialize' /usr/lib/ruby/1.8/mongrel.rb:285:innew’
/usr/lib/ruby/1.8/mongrel.rb:285:in run' /usr/lib/ruby/1.8/mongrel.rb:268:ininitialize’
/usr/lib/ruby/1.8/mongrel.rb:268:in new' /usr/lib/ruby/1.8/mongrel.rb:268:inrun’
/usr/lib/ruby/1.8/mongrel/configurator.rb:282:in run' /usr/lib/ruby/1.8/mongrel/configurator.rb:281:ineach’
/usr/lib/ruby/1.8/mongrel/configurator.rb:281:in run' /usr/lib/ruby/1.8/mongrel/command.rb:212:inrun’

Line 460 is the last one listed above. I can see from the debug output
that question is a regular AR object.

The reason that i thought it might be memcache/interlock related is that
if i restart mongrel then it magically works. Then the second time i
try it it breaks again. This is totally consistent, it always works
first time after restart and fails every subsequent time.

kind of stumped - any ideas anyone?

thanks, max

On May 5, 9:49 am, Max W. [email protected] wrote:

Hey all

I have a line of code that’s causing a “undefined method `include?’ for
nil:NilClass” exception, but i can’t work out what’s actually going
wrong with it. After a bit of experimentation i discovered that it’s
caused by simply asking for the id of an AR object i have.

does it only happen if config.cache_classes is set to false (remember
to restart the app after you change that) ?

Fred

Hi Fred - good question - yes, it does. IE, setting cache_classes to
true fixes it. hmmm.

Though obviously i still want to fix it properly :slight_smile:

Frederick C. wrote:

On May 5, 10:44�am, Max W. [email protected] wrote:

Hi Fred - good question - yes, it does. �IE, setting cache_classes to
true fixes it. �hmmm.

Then it’s a class reloading problem. When an activerecord class gets
reloaded, the old copy has all of its methods removed. If something is
still holding onto an instance like that and tries to call any methods
of it then it will fail

Fred

Hmm…like a ‘frozen hash’ error? But what is holding onto it though? I
tried clearing my memcache CACHE, that didn’t work. I do have some
dynamically generated class methods in that class, but i commented them
out and the problem remains the same.

thanks, max

On May 5, 10:44 am, Max W. [email protected] wrote:

Hi Fred - good question - yes, it does. IE, setting cache_classes to
true fixes it. hmmm.

Then it’s a class reloading problem. When an activerecord class gets
reloaded, the old copy has all of its methods removed. If something is
still holding onto an instance like that and tries to call any methods
of it then it will fail

Fred

ok - someone pointed me at this discussion

https://rails.lighthouseapp.com/projects/8994/tickets/1339

and i tried this patch

https://rails.lighthouseapp.com/projects/8994/tickets/1339#ticket-1339-29

which seems to have worked. But there might be memory leaks as a
consequence, though in dev mode only. I’m a bit none the wiser for a
lot of that discussion’s overall conclusions (if there were any, it
seemed quite unresolved apart from agreeing to try to fix it later
rather than now).

On May 5, 2:46 pm, Max W. [email protected] wrote:

lot of that discussion’s overall conclusions (if there were any, it
seemed quite unresolved apart from agreeing to try to fix it later
rather than now).

I expect you’ll notice that if you change your code the bit of your
app that was causing this problem runs as if you hadn’t made the
change at all.
As to what is holding onto an old instance of a class, that can be a
bit hard to track down. I’d go back to the line of code that
originally called the problem. Where does the instance of question
that was causing the problems come from ?

Fred