Skip to content

Commit

Permalink
Prevent concurrent compilation of templates - closes rails#6400
Browse files Browse the repository at this point in the history
  • Loading branch information
pinetops committed May 20, 2012
1 parent 5284e65 commit 565c1b0
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions actionpack/lib/action_view/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,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
Expand Down Expand Up @@ -123,6 +124,7 @@ def initialize(source, identifier, handler, details)
@virtual_path = details[:virtual_path]
@updated_at = details[:updated_at] || Time.now
@formats = Array.wrap(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,
Expand Down Expand Up @@ -224,18 +226,28 @@ def encode!
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
Expand Down

0 comments on commit 565c1b0

Please sign in to comment.