Out of memory and 100% CPU with threadsafe! #577

Closed
dnagir opened this Issue May 23, 2012 · 10 comments

Comments

Projects
None yet
4 participants

dnagir commented May 23, 2012

When Rails' config.threadsafe! is enabled then simple_form eats up all the memory and uses 100% of the CPU.

Symptoms:

  • Running view spec in RSpec rendering partial form.
  • `RAILS_ENV=test

The spec:

require 'spec_helper'

describe "posts/_form" do
  subject { render partial: "posts/form"  }
  let(:post) { stub_model(Post) }
  before { assign :post, post }

  it { should_not == '' }
end

The view:

= simple_form_for @post do |f|
  = f.button :submit

WORKAROUND: disabling Rails' threadsafety (do not call config.threadsafe! in test environment).

Collaborator

carlosantoniodasilva commented May 23, 2012

Have you tried normal form_for usage to see the results?

= form_for @post do |f|
  = f.submit

Also, make sure you try using ERB as well. This only happens for this particular view that has SimpleForm in it? And with SimpleForm and Rails versions are you using?

Thanks for reporting.

Owner

josevalim commented May 23, 2012

I think this may be the same issue as #478. On the first render, we are autoloading Simple Form classes and, since you are running in thread safe mode, we are probably running into a dead lock. The deadlock is most likely a Ruby bug (it is known autoload dead locks, except in latest jruby). Try requiring the simple form inputs you are using on initialization and it will probably solve your problem (please let us know if it does).

dnagir commented May 24, 2012

It cannot be deadlock. Deadlock doesn't eat up all the memory. And I don't have any custom inputs at all.

I'll give more info a bit later. Sorry for the delay.

Owner

josevalim commented May 24, 2012

@dnagir the CPU can spike on a deadlock if you are frequently checking to see if the thread that blocked you finished or not. For example:

true until Object.const_defined?(:HAHA_NEVER)

In any case, I am just guessing. The problem does not happen with custom inputs, it happens when loading Simple Form internal inputs. Try adding this to an initializer and see what happens next:

  require 'simple_form/components'
  require 'simple_form/error_notification'
  require 'simple_form/map_type'
  require 'simple_form/inputs'
  require 'simple_form/i18n_cache'
  require 'simple_form/helpers'
  require 'simple_form/form_builder'
  require 'simple_form/wrappers'

@josevalim josevalim closed this May 24, 2012

@josevalim josevalim reopened this May 24, 2012

dnagir commented May 24, 2012

More info:

  • It happens on this particular view (at least I can run specs on a couple of others)
  • form_for - exactly the same is happening.
  • ERB - same is happening with both HAML and ERB.
  • tried to require all the inputs of as @josevalim suggested; doesn't help

Using:

  • simple_form 2.0.2
  • rails 3.2.3

The memory jumps up to 1GB in a matter of couple of seconds. There must something that short-circuits the rendering or similar.

Owner

josevalim commented May 24, 2012

If it happens with form_for, this is unlikely to be a simple form issue. Since the memory is jumping, it is unlikely to be autoload as well, maybe something related to the templates compilation or action view resolvers.

dnagir commented May 24, 2012

True. But I haven't found Rails related issue either. Not sure how to find what causes it since it can be anything.

But at least I can disable threaded mode for test environment with no problems. So it is not critical for me.
I just hope that this issue won't slip in to production somehow.

Would someone indicate what version of Ruby they are using while experiencing this problem?

dnagir commented Jun 21, 2012

ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.4.0]

Owner

josevalim commented Aug 21, 2012

As mentioned above, if it fails without Simple Form, it is a Rails issue. Thread safety could definitely be improved on Simple Form and we will do so #478.

@josevalim josevalim closed this Aug 21, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment