Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Setting the I18n.locale in ActionMailer raises SystemStackError

#5329

When using I18n.with_locale function inside a function called from ActionMailer::Base, SystemStackError (stack level too deep) is raised. Also, simply changing locale with I18n.locale= results in the same exception. When using outside of ActionMailer, everything works as expected.

Stack trace:

SystemStackError (stack level too deep):
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/action_view/lookup_context.rb:174:in `locale='
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/abstract_controller/rendering.rb:28:in `locale='
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/action_view/lookup_context.rb:175:in `locale='
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/abstract_controller/rendering.rb:28:in `locale='
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/action_view/lookup_context.rb:175:in `locale='
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/abstract_controller/rendering.rb:28:in `locale='
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/action_view/lookup_context.rb:175:in `locale='
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/abstract_controller/rendering.rb:28:in `locale='
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/action_view/lookup_context.rb:175:in `locale='
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/abstract_controller/rendering.rb:28:in `locale='
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/action_view/lookup_context.rb:175:in `locale='
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/abstract_controller/rendering.rb:28:in `locale='
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/action_view/lookup_context.rb:175:in `locale='
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/abstract_controller/rendering.rb:28:in `locale='
….
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/action_view/lookup_context.rb:175:in `locale='
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/abstract_controller/rendering.rb:28:in `locale='
  i18n (0.4.1) lib/i18n.rb:39:in `locale='
  app/models/invoice_pdf.rb:26:in `to_pdf'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/activesupport/lib/active_support/dependencies.rb:442:in `each_with_index'
  app/models/invoice_pdf.rb:20:in `each'
  app/models/invoice_pdf.rb:20:in `each_with_index'
  app/models/invoice_pdf.rb:20:in `to_pdf'
  app/mailers/invoice_mailer.rb:6:in `email'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/abstract_controller/base.rb:148:in `send_action'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/abstract_controller/base.rb:148:in `process_action'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/abstract_controller/base.rb:117:in `process'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/abstract_controller/rendering.rb:40:in `process'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionmailer/lib/action_mailer/old_api.rb:75:in `process'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionmailer/lib/action_mailer/base.rb:446:in `process'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionmailer/lib/action_mailer/base.rb:441:in `initialize'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionmailer/lib/action_mailer/base.rb:425:in `new'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionmailer/lib/action_mailer/base.rb:425:in `method_missing'
  app/controllers/invoice_emails_controller.rb:20:in `create'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/abstract_controller/base.rb:148:in `process_action'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/action_controller/metal/rendering.rb:11:in `process_action'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/abstract_controller/callbacks.rb:18:in `process_action'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/activesupport/lib/active_support/callbacks.rb:445:in `_run__1023616770__process_action__1623385099__callbacks'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/activesupport/lib/active_support/callbacks.rb:404:in `send'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/activesupport/lib/active_support/callbacks.rb:404:in `_run_process_action_callbacks'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/activesupport/lib/active_support/callbacks.rb:93:in `send'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/activesupport/lib/active_support/callbacks.rb:93:in `run_callbacks'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/abstract_controller/callbacks.rb:17:in `process_action'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/action_controller/metal/instrumentation.rb:30:in `process_action'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/activesupport/lib/active_support/notifications.rb:52:in `instrument'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/activesupport/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/activesupport/lib/active_support/notifications.rb:52:in `instrument'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/action_controller/metal/instrumentation.rb:29:in `process_action'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/action_controller/metal/rescue.rb:17:in `process_action'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/abstract_controller/base.rb:117:in `process'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/abstract_controller/rendering.rb:40:in `process'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/action_controller/metal.rb:133:in `dispatch'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/action_controller/metal.rb:173:in `action'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/action_dispatch/routing/route_set.rb:62:in `call'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/action_dispatch/routing/route_set.rb:62:in `dispatch'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/action_dispatch/routing/route_set.rb:27:in `call'
  rack-mount (0.6.9) lib/rack/mount/route_set.rb:148:in `call'
  rack-mount (0.6.9) lib/rack/mount/code_generation.rb:89:in `recognize'
  rack-mount (0.6.9) lib/rack/mount/code_generation.rb:87:in `optimized_each'
  rack-mount (0.6.9) lib/rack/mount/code_generation.rb:88:in `recognize'
  rack-mount (0.6.9) lib/rack/mount/route_set.rb:139:in `call'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/action_dispatch/routing/route_set.rb:492:in `call'
  haml (3.0.15) lib/sass/plugin/rack.rb:41:in `call'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/action_dispatch/middleware/best_standards_support.rb:8:in `call'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/action_dispatch/middleware/head.rb:14:in `call'
  rack (1.2.1) lib/rack/methodoverride.rb:24:in `call'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/action_dispatch/middleware/params_parser.rb:21:in `call'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/action_dispatch/middleware/flash.rb:177:in `call'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/action_dispatch/middleware/session/abstract_store.rb:149:in `call'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/action_dispatch/middleware/cookies.rb:268:in `call'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/action_dispatch/middleware/callbacks.rb:46:in `call'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/activesupport/lib/active_support/callbacks.rb:410:in `_run_call_callbacks'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/action_dispatch/middleware/callbacks.rb:44:in `call'
  rack (1.2.1) lib/rack/sendfile.rb:107:in `call'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/action_dispatch/middleware/show_exceptions.rb:48:in `call'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/railties/lib/rails/rack/logger.rb:13:in `call'
  rack (1.2.1) lib/rack/runtime.rb:17:in `call'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/activesupport/lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.2.1) lib/rack/lock.rb:11:in `call'
  rack (1.2.1) lib/rack/lock.rb:11:in `synchronize'
  rack (1.2.1) lib/rack/lock.rb:11:in `call'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/action_dispatch/middleware/static.rb:30:in `call'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/railties/lib/rails/application.rb:168:in `call'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/railties/lib/rails/application.rb:77:in `send'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/railties/lib/rails/application.rb:77:in `method_missing'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/railties/lib/rails/rack/log_tailer.rb:14:in `call'
  rack (1.2.1) lib/rack/content_length.rb:13:in `call'
  rack (1.2.1) lib/rack/handler/webrick.rb:52:in `service'
  /Users/jzajpt/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
  /Users/jzajpt/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
  /Users/jzajpt/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
  /Users/jzajpt/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/webrick/server.rb:162:in `start'
  /Users/jzajpt/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
  /Users/jzajpt/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/webrick/server.rb:95:in `start'
  /Users/jzajpt/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/webrick/server.rb:92:in `each'
  /Users/jzajpt/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/webrick/server.rb:92:in `start'
  /Users/jzajpt/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/webrick/server.rb:23:in `start'
  /Users/jzajpt/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/webrick/server.rb:82:in `start'
  rack (1.2.1) lib/rack/handler/webrick.rb:13:in `run'
  rack (1.2.1) lib/rack/server.rb:213:in `start'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/railties/lib/rails/commands/server.rb:65:in `start'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/railties/lib/rails/commands.rb:30
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/railties/lib/rails/commands.rb:27:in `tap'
  /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/railties/lib/rails/commands.rb:27
  script/rails:6:in `require'
  script/rails:6

Rendered /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/action_dispatch/middleware/templates/rescues/_trace.erb (89.7ms)
Rendered /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (76.0ms)
Rendered /Users/jzajpt/.rvm/gems/ruby-1.8.7-p299@rails3/bundler/gems/rails-0953c04/actionpack/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (175.8ms)

Reported by Jiří Zajpt · August 7th, 2010 @ 04:04 PM

State: resolved
Milestone: none
Assigned to: José Valim José Valim
Importance: Low

Activity

  1. Don Werve
    Don Werve

    I've got the same problem, with a similar stack trace.

    September 24th, 2010 @ 12:44 AM

  2. Maximilian Schulz
    Maximilian Schulz

    Ok, I should have google earlier… I have the exact same error. I moved my I18n.with_locale code out of the mail vier, as I have always got deprecation warnings for the block helper (although it does not make any sense). So my mailer looks like this:

    I18n.in_locale(locale) do
    mail :to => @user.to_email_recipient,

       :subject => I18n.t("new_message.subject", :title => @topic.subject)
    

    end

    And it seems to work for some methods in the mailer and breaks on others. And I have no clue whats happening!

    December 15th, 2010 @ 12:36 AM

  3. Maximilian Schulz
    Maximilian Schulz

    Ok, this is actually not a problem with the I18n.with_locale feature, but has to do with exceptions raised in the associated mail view. The with_locale call just hides the real error, which is really annoying!

    December 15th, 2010 @ 01:20 AM

  4. Thiago Morello
    Thiago Morello

    I've got the same problem with "with_locale" method.
    When sending using rails console everything works fine, but when calling from a controller I get the same error.

    December 20th, 2010 @ 03:56 PM

  5. aka47
    aka47
    • Assigned user set to José Valim

    i also get this problem in rails 3.0.3 ... the locale= in abstract_controller/rendering.rb and this call action_view/lookup_context.rb and this call the first again. Endless loop.

     # ree-1.8.7-2010.01@rails3/gems/actionpack-3.0.3/lib/abstract_controller/rendering.rb:28:in `locale='
     # ree-1.8.7-2010.01@rails3/gems/actionpack-3.0.3/lib/action_view/lookup_context.rb:175:in `locale='
    

    José added a TODO to deprecate I18n.locale from controllers, but it does not say how the new api should is. I hope this will fix it.

    I could fix it by removing the I18nProxy

     # ree-1.8.7-2010.01@rails3/gems/actionpack-3.0.3/lib/abstract_controller/rendering.rb:39
    
     +      old_config, I18n.config = I18n.config, I18n.config
     -      old_config, I18n.config = I18n.config, I18nProxy.new(I18n.config, lookup_context)
    

    or better patch it via config/initializer/rails3_abstract_controller_rendering_fix.rb

     module AbstractController::Rendering
       def process(*)
         super
       end
     end
    

    December 28th, 2010 @ 06:22 PM

  6. José Valim
    José Valim
    • State changed from new to needs-more-info
    • Importance changed from to Low

    I cannot reproduce. Could someone provide a test case to Action Mailer test suite?

    December 28th, 2010 @ 09:46 PM

  7. Frank Fischer
    Frank Fischer

    I have the same problem using an I18n.locale call within the actionmailer method. The error triggers if the actionmailer-method is called from from inside a controller. Find a diff containing the testcase added.
    Thank you Jose for pointing me in the right direction how to trigger the error in the testcase!

    January 14th, 2011 @ 08:39 AM

  8. f.svehla
    f.svehla
    • Tag changed from actionmailer, locale to actionmailer, i18n, infinity, locale

    Simply calling I18n.locale= in an ActionMailer method is sufficient to trigger this bug.

    January 16th, 2011 @ 08:56 AM

  9. f.svehla
    f.svehla
    • Title changed from Using I18n.with_locale in ActionMailer raises SystemStackError to Setting the I18n.locale in ActionMailer raises SystemStackError

    January 16th, 2011 @ 08:57 AM

  10. Frank Fischer
    Frank Fischer

    @f.svhela: Can you provide a test case to prove your point? It's because I have one proving you are wrong ;). Btw. - This was the reason why Jose couldn't reproduce the error in first place.

    January 18th, 2011 @ 12:02 AM

  11. eddyk
    eddyk

    I can confirm seeing the stack problem when the mailer is created in a controller. In my unit tests, generating the email worked fine.

    The monkey patch from aka47 appears to work.


    module AbstractController::Rendering
    def process(*)

    {mkd-extraction-08d005cc18c52729bf1100e0f5943312}

    end end

    January 19th, 2011 @ 10:04 PM

  12. Repository
  13. Repository
  14. Repository
  15. Repository
  16. eddyk
    eddyk
      module AbstractController
        class I18nProxy
          def initialize(i18n_config, lookup_context)
            @i18n_config, @lookup_context = i18n_config, lookup_context
            @i18n_config = @i18n_config.i18n_config if @i18n_config.respond_to?(:i18n_config)
          end
        end
      end
    

    Here's a bit a code my coworker came up with to prevent the infinite recursion that occurs when setting the I18n config in an actionmailer context, which is called from an actioncontroller.

    January 20th, 2011 @ 12:42 AM