Permalink
Browse files

Protect against connection reuse across forks

  • Loading branch information...
1 parent 2a102c3 commit 1bd43346cdf71ba69176612714616289a5a70ed4 @pietern pietern committed May 4, 2012
Showing with 16 additions and 1 deletion.
  1. +12 −1 lib/redis/client.rb
  2. +4 −0 lib/redis/errors.rb
View
@@ -272,7 +272,18 @@ def ensure_connected
tries = 0
begin
- connect unless connected?
+ if connected?
+ if Process.pid != @pid
+ raise InheritedError, <<-EOS.gsub(/(?:^|\n)\s*/, " ").strip
@wjessop

wjessop May 7, 2012

Seems like this would be better than the gsub (simpler, easier to read):

raise InheritedError, "Tried to use a connection from a child process without reconnecting. You need to reconnect to Redis after forking."

@pietern

pietern May 7, 2012

Owner

I don't like long lines that much...

@wjessop

wjessop May 7, 2012

How about:

raise InheritedError,
"Tried to use a connection from a child process without reconnecting. " +
"You need to reconnect to Redis after forking."

Now all the people on fixed-width terminals can read the code too ;)

@pietern

pietern May 7, 2012

Owner

The force is strong with you ;-)

+ Tried to use a connection from a child process without reconnecting.
+ You need to reconnect to Redis after forking.
+ EOS
+ end
+ else
+ @pid = Process.pid
+ connect
+ end
+
tries += 1
yield
View
@@ -33,4 +33,8 @@ class ConnectionError < BaseConnectionError
# Raised when performing I/O times out.
class TimeoutError < BaseConnectionError
end
+
+ # Raised when the connection was inherited by a child process.
+ class InheritedError < BaseConnectionError
+ end
end

0 comments on commit 1bd4334

Please sign in to comment.