spikex / strongbox

Secures ActiveRecord attributes with public key encryption
http://stuff-things.net/2009/04/17/introducing-strongbox/
MIT License
398 stars 42 forks source link

"stack level too deep" With Database Migration #22

Closed carsonreinke closed 12 years ago

carsonreinke commented 12 years ago

Adding the gem causes a "stack level too deep" of the db:schema:dump rake task on Rails 3.2.2 / Ruby 1.9.3.

Here is the output from the task using --trace:

* Invoke db:schema:dump (first_time) * Invoke environment (first_time) * Execute environment * Invoke db:load_config (first_time) * Invoke rails_env (first_time) * Execute rails_env * Execute db:load_config * Execute db:schema:dump rake aborted! stack level too deep /Users/carsonreinke/.rvm/gems/ruby-1.9.3-p125/gems/rake-0.9.2.2/lib/rake/task.rb:162 Tasks: TOP => db:schema:dump

carsonreinke commented 12 years ago

Possibly related? https://github.com/lomba/schema_plus/issues/42

spikex commented 12 years ago

I'm not seeing this with Rails 3.2.3 and Ruby 1.9.2. What versions are you using? What other gems have you added to your app?

Thanks.

carsonreinke commented 12 years ago

Rails 3.2.2 / Ruby 1.9.3.

Looks like I have found the problem, I had an initializer that chained ActiveRecord::ConnectionAdapters::Mysql2Adapter#configure_connection to add some additional functionality. Removing that fixes the problem.

Adding this below will reproduce the issue:

if defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter)
    class ActiveRecord::ConnectionAdapters::Mysql2Adapter
        def configure_connection_with_test()
            configure_connection_without_test()
        end

        alias_method_chain :configure_connection, :test
    end
end
spikex commented 12 years ago

Hmm, still not able to reproduce this. If you start a clean project with just the above and strongbox, do you get the same error?

carsonreinke commented 12 years ago

So sorry, forgot to add an important piece of information. The file is included in the config.after_initialize block.

Yes, I was able to reproduce with a clean project.

spikex commented 12 years ago

I tried 1.9.2/3.2.2 and still can't reproduce it. Can you throw the minimum project that fails for you in a repo?

carsonreinke commented 12 years ago

Sorry for the delay, here you go: https://github.com/carsonreinke/strongbox-test

spikex commented 12 years ago

config/initializers/mysql_test.rb is getting loaded twice first automatically by rails the initialization and then explicitly by you in config.after_initialize.

Without Strongbox it works because when Rails loads the initialization file ActiveRecord::ConnectionAdapters::Mysql2Adapter isn't defined, but it is when you load the file.

With Strongbox ActiveRecord::ConnectionAdapters::Mysql2Adapter is loaded when Rails first loads the file. This cause two calls to alias_method_chain which creates the loop.

I assume this happens because Strongbox's call to ActiveRecord::Base.send causes Rails lazy loading to pull in that class sooner.

So, it's not a bug in Strongbox, just a question the gem effecting when the class is loaded. Two way to work with that:

  1. Move the initializer code out of the initializers directory so it's only loaded once.
  2. Make sure alias_method_chain can only be called once:
unless method_defined? :configure_connection_with_time_zone
  alias_method_chain :configure_connection, :time_zone
end
carsonreinke commented 12 years ago

Ah, good call. Sorry for the bother.

spikex commented 12 years ago

No worries, it's a pretty subtle issue.