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.

Rails 3 ActionView is not thread-safe

#5340

ActionView::Resolver relies on pure Ruby Hashes for caching template look-ups (http://github.com/rails/rails/blob/master/actionpack/lib/action_vie...) and since its instances are used as a global thread-shared objects they are not thread-safe.

This is a new Rails 3 development, Rails 2 template look-up code is thread-safe.

Reported by thedarkone · August 9th, 2010 @ 07:34 PM

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

Activity

  1. José Valim
    José Valim
    • Assigned user set to José Valim
    • Importance changed from to Low

    Hey mate!

    The worse scenario is that two threads would hit nil at the same time and we would do two lookups. I believe this is completely fine, you won't end up with garbage data.

    Or am I missing something?

    October 10th, 2010 @ 06:30 PM

  2. thedarkone
    thedarkone

    Hey,

    no, what you are describing would be the worst case scenario if Hash were thread-safe. The only guarantees for example JRuby makes for concurrent modification of Hashes is that you shouldn't be able to segfault JVM.

    Otherwise worst-case scenarios right now are IndexOutOfBound exceptions or some threads stuck in an infinite loops resizing corrupted Hash instances. Of course you need to be pretty unlucky to run into these issues, but that's how it is with threads.

    October 10th, 2010 @ 11:48 PM

  3. Ryan Bigg
    Ryan Bigg
    • Tag cleared.

    Automatic cleanup of spam.

    October 19th, 2010 @ 08:23 AM

  4. Nick Sieger
    Nick Sieger

    I agree with @thedarkone on this one. JRuby's hash implementation requires synchronization around a shared data structure like a hash or array. The worst that can happen in JRuby's case is that we will raise a ConcurrencyError if the hash is modified by two different threads in a short timeframe. Unfortunately, the code is probably not prepared for this possibility and the error may end up blowing up the whole request.

    I haven't looked at the code and am not prepared to suggest an alternative. I'd like to think that there's a way to do this without using a mutex around cache accesses, but I'm not sure. At the very least protecting against errors while reading and/or modifying the cache would be a good start.

    November 19th, 2010 @ 05:37 PM

  5. thedarkone
    thedarkone

    No, ConcurencyError exception is unfortunately not the worst thing that can happen on JRuby (or on any Ruby VM with real threads for that matter). Modifying non thread-safe data structures in parallel threads is like parsing HTML with regular expressions. I wouldn't be surprised that it's possible to even get it to mix-up key-value pairs (I haven't read the source code, so I might be wrong). My simple testing however showed threads stuck in infinite loops are nothing out of the ordinary.

    February 4th, 2011 @ 10:04 PM

  6. rails
    rails
    • State changed from new to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    May 5th, 2011 @ 01:00 AM

  7. rails
    rails
    • State changed from open to stale

    May 5th, 2011 @ 01:00 AM