Posted 2 months ago. Visible to the public.

ActiveJob Inline can break the autoloading in development

We figured out, that ActiveJob Inline might lead to autoloading problems in development. The result was an exception when running an import script, which delivers async mails.

Copy
A copy of XXX has been removed from the module tree but is still active! (ArgumentError)

Our fix was to use .deliver_now and not .deliver_later (this is not a general fix, but it was okey for us). Below there are some debug hints which helped us to locate the problem:

  • We placed a pry debugger in ActiveSupport#clear (this is the part where classes are loaded and unloaded) and stopped spring
  • We ran the script and waited until the debugger was triggered
  • In the debugger we looked up the caller by first running raise, then wtf???????????? in pry (wtf is a pry command, the more ? you add, the longer the backtrace becomes)

The useful lines looked like this in the backtrace:

Copy
59: /home/user/.rvm/gems/ruby-2.5.1/gems/activejob-5.2.1/lib/active_job/execution.rb:22:in `execute' 60: /home/user/.rvm/gems/ruby-2.5.1/gems/activejob-5.2.1/lib/active_job/queue_adapters/inline_adapter.rb:15:in `enqueue'

We didn't dig deeper into the root cause at this point, but it already helped us to understand which part of the importer caused the exception.

Beside this it was also helpful to print to the console when the module tree was cleared (puts in ActiveSupport#clear) and which dependencies where added (puts in ActiveSupport#load_missing_constant).

Here is a simple example output:

Copy
Load the constant named ApplicationRecord which is missing from Object Load the constant named User which is missing from Object Load the constant named User::Importer which is missing from User Clear the dependencies Load the constant named ApplicationRecord which is missing from Object Load the constant named User which is missing from Object Load the constant named User::Importer which is missing from User => A copy of User has been removed from the module tree but is still active! (ArgumentError)

We used this to check if the constant claimed by the exception was loaded before someone threw it away.


If you don't use .deliver_later in your application code and see the error above, you might want to look at Fixing "A copy of Klass has been removed from the module tree but is still active".

Growing Rails Applications in Practice
Check out our new e-book:
Learn to structure large Ruby on Rails codebases with the tools you already know and love.

Owner of this card:

Avatar
Emanuel De
Last edit:
about 1 month ago
by Arne Hartherz
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Emanuel De to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more