From ddcb040c40f624ee4fb4a3143e4bd95ab2ca61c9 Mon Sep 17 00:00:00 2001 From: soh_cah_toa Date: Tue, 14 May 2013 19:12:23 -0400 Subject: [PATCH] Marked add_rule() and remove_rule() as critical sections. Mutual exclusion is imperative here since other modules/extenions may be simultaneously adding/removing rules, thus putting the value of @next_id at risk of becoming inconsistent. --- extensions/dns/dns.rb | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/extensions/dns/dns.rb b/extensions/dns/dns.rb index 9e3f4e9db..ac646189a 100644 --- a/extensions/dns/dns.rb +++ b/extensions/dns/dns.rb @@ -14,7 +14,8 @@ module DNS # @!method instance # Returns the singleton instance. def initialize - @server = nil + @lock = Mutex.new + @server = nil @next_id = 0 end @@ -59,9 +60,11 @@ module DNS # @see #remove_rule # @see http://rubydoc.info/gems/rubydns/RubyDNS/Transaction def add_rule(pattern, type, &block) - @next_id += 1 - @server.match(@next_id, pattern, type, block) - @next_id + @lock.synchronize do + @next_id += 1 + @server.match(@next_id, pattern, type, block) + @next_id + end end # Removes the given DNS rule. Any future queries for it will be passed through. @@ -69,8 +72,10 @@ module DNS # @param id [Integer] id returned from {#add_rule} # @see #add_rule def remove_rule(id) - @server.remove_rule(id) - @next_id -= 1 + @lock.synchronize do + @server.remove_rule(id) + @next_id -= 1 + end end # Loads all rules from the database at server startup