This project is archived and is in readonly mode.

#5340 ✓stale
thedarkone

Rails 3 ActionView is not thread-safe

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

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.

Comments and changes to this ticket

  • José Valim

    José Valim October 10th, 2010 @ 06:30 PM

    • 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?

  • thedarkone

    thedarkone October 10th, 2010 @ 11:48 PM

    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.

  • Ryan Bigg

    Ryan Bigg October 19th, 2010 @ 08:23 AM

    • Tag cleared.

    Automatic cleanup of spam.

  • Nick Sieger

    Nick Sieger November 19th, 2010 @ 05:37 PM

    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.

  • thedarkone

    thedarkone February 4th, 2011 @ 10:04 PM

    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.

  • rails

    rails May 5th, 2011 @ 01:00 AM

    • 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.

  • rails

    rails May 5th, 2011 @ 01:00 AM

    • State changed from “open” to “stale”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages