aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorTom Clarke <tom@u2i.com>2012-05-20 12:41:58 -0400
committerTom Clarke <tom@u2i.com>2012-05-21 14:31:43 -0400
commitef19e8020d831758006a9f0472582e9c2c3b7916 (patch)
treed6835929c65bda24e7f4e395c5c722d0221988d4 /actionpack
parentb23ac936a6ef980547666da002681a77d8340573 (diff)
downloadrails-ef19e8020d831758006a9f0472582e9c2c3b7916.tar.gz
rails-ef19e8020d831758006a9f0472582e9c2c3b7916.tar.bz2
rails-ef19e8020d831758006a9f0472582e9c2c3b7916.zip
Prevent concurrent compilation of templates - closes #6400
This addresses an issue where in multi-threaded environments multiple threads can attempt to compile a template at the same time, which occasionally causes particular templates to end up in a bad state. So, add synchronization such that only a single thread can attempt to compile a template at one time.
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/lib/action_view/template.rb32
1 files changed, 22 insertions, 10 deletions
diff --git a/actionpack/lib/action_view/template.rb b/actionpack/lib/action_view/template.rb
index edb3d427d5..cd79468502 100644
--- a/actionpack/lib/action_view/template.rb
+++ b/actionpack/lib/action_view/template.rb
@@ -1,6 +1,7 @@
require 'active_support/core_ext/object/blank'
require 'active_support/core_ext/object/try'
require 'active_support/core_ext/kernel/singleton_class'
+require 'thread'
module ActionView
# = Action View Template
@@ -122,6 +123,7 @@ module ActionView
@virtual_path = details[:virtual_path]
@updated_at = details[:updated_at] || Time.now
@formats = Array(format).map { |f| f.is_a?(Mime::Type) ? f.ref : f }
+ @compile_mutex = Mutex.new
end
# Returns if the underlying handler supports streaming. If so,
@@ -223,18 +225,28 @@ module ActionView
def compile!(view) #:nodoc:
return if @compiled
- if view.is_a?(ActionView::CompiledTemplates)
- mod = ActionView::CompiledTemplates
- else
- mod = view.singleton_class
- end
+ # Templates can be used concurrently in threaded environments
+ # so compilation and any instance variable modification must
+ # be synchronized
+ @compile_mutex.synchronize do
+ # Any thread holding this lock will be compiling the template needed
+ # by the threads waiting. So re-check the @compiled flag to avoid
+ # re-compilation
+ return if @compiled
+
+ if view.is_a?(ActionView::CompiledTemplates)
+ mod = ActionView::CompiledTemplates
+ else
+ mod = view.singleton_class
+ end
- compile(view, mod)
+ compile(view, mod)
- # Just discard the source if we have a virtual path. This
- # means we can get the template back.
- @source = nil if @virtual_path
- @compiled = true
+ # Just discard the source if we have a virtual path. This
+ # means we can get the template back.
+ @source = nil if @virtual_path
+ @compiled = true
+ end
end
# Among other things, this method is responsible for properly setting