Skip to content
Browse files

Synchronize the ConnectionPool#release method to avoid thread safety …

…issues [#6464]

Fixes #6464

Synchronize the contents of the release method in ConnectionPool due to
errors when running in high concurrency environments.

    Detected invalid hash contents due to unsynchronized modifications
with concurrent users
    org/jruby/RubyHash.java:1356:in `keys'
/usr/local/rvm/gems/jruby-1.6.7@new_import/gems/activerecord-3.2.3/lib/a
ctive_record/connection_adapters/abstract/connection_pool.rb:294:in
`release'

/usr/local/rvm/gems/jruby-1.6.7@new_import/gems/activerecord-3.2.3/lib/a
ctive_record/connection_adapters/abstract/connection_pool.rb:282:in
`checkin'
  • Loading branch information...
1 parent ff3cddf commit e663aa39aa8599f01cce2f61c68e8bea736fc186 Andrew Selder committed May 23, 2012
Showing with 12 additions and 10 deletions.
  1. +12 −10 activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
View
22 activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
@@ -290,17 +290,19 @@ def checkin(conn)
private
def release(conn)
- thread_id = nil
-
- if @reserved_connections[current_connection_id] == conn
- thread_id = current_connection_id
- else
- thread_id = @reserved_connections.keys.find { |k|
- @reserved_connections[k] == conn
- }
- end
+ synchronize do
+ thread_id = nil
+
+ if @reserved_connections[current_connection_id] == conn
+ thread_id = current_connection_id
+ else
+ thread_id = @reserved_connections.keys.find { |k|
+ @reserved_connections[k] == conn
+ }
+ end
- @reserved_connections.delete thread_id if thread_id
+ @reserved_connections.delete thread_id if thread_id
+ end
end
def new_connection

0 comments on commit e663aa3

Please sign in to comment.
Something went wrong with that request. Please try again.