From d0b8e8b25db2b5042f7107c6e8974c8910043b7d Mon Sep 17 00:00:00 2001 From: Jamis Buck Date: Tue, 26 Jun 2007 05:11:34 +0000 Subject: [PATCH] Make variable accesses thread safe git-svn-id: http://svn.rubyonrails.org/rails/tools/capistrano@7127 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- CHANGELOG | 2 ++ lib/capistrano/configuration/variables.rb | 34 +++++++++++++++-------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 26964081..b185cfb2 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Make variable accesses thread safe [via Adrian Danieli] + * Make user input for yes/no prompts work correctly in the Mercurial module [Matthew Elder] * Use single quotes to escape semicolon in find command, instead of a backslash [via michael.italia@gmail.com] diff --git a/lib/capistrano/configuration/variables.rb b/lib/capistrano/configuration/variables.rb index 79aad638..ce7bc159 100644 --- a/lib/capistrano/configuration/variables.rb +++ b/lib/capistrano/configuration/variables.rb @@ -1,3 +1,5 @@ +require 'thread' + module Capistrano class Configuration module Variables @@ -37,8 +39,10 @@ module Capistrano # Removes any trace of the given variable. def unset(variable) sym = variable.to_sym - @original_procs.delete(sym) - @variables.delete(sym) + @variable_lock.synchronize do + @original_procs.delete(sym) + @variables.delete(sym) + end end # Returns true if the variable has been defined, and false otherwise. @@ -51,11 +55,13 @@ module Capistrano # true if the variable was actually reset. def reset!(variable) sym = variable.to_sym - if @original_procs.key?(sym) - @variables[sym] = @original_procs.delete(sym) - true - else - false + @variable_lock.synchronize do + if @original_procs.key?(sym) + @variables[sym] = @original_procs.delete(sym) + true + else + false + end end end @@ -73,12 +79,15 @@ module Capistrano return yield(variable) if block_given? raise IndexError, "`#{variable}' not found" end - - if @variables[sym].respond_to?(:call) - @original_procs[sym] = @variables[sym] - @variables[sym] = @variables[sym].call + + @variable_lock.synchronize do + if @variables[sym].respond_to?(:call) + @original_procs[sym] = @variables[sym] + @variables[sym] = @variables[sym].call + end + + @variables[sym] end - @variables[sym] end def [](variable) @@ -89,6 +98,7 @@ module Capistrano initialize_without_variables(*args) @variables = {} @original_procs = {} + @variable_lock = Mutex.new set :ssh_options, {} set :logger, logger