Skip to content

Commit

Permalink
Protect against connection reuse across forks
Browse files Browse the repository at this point in the history
  • Loading branch information
pietern committed May 4, 2012
1 parent 2a102c3 commit 1bd4334
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/redis/client.rb
Expand Up @@ -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

This comment has been minimized.

Copy link
@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."

This comment has been minimized.

Copy link
@pietern

pietern May 7, 2012

Author Collaborator

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

This comment has been minimized.

Copy link
@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 ;)

This comment has been minimized.

Copy link
@pietern

pietern May 7, 2012

Author Collaborator

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
Expand Down
4 changes: 4 additions & 0 deletions lib/redis/errors.rb
Expand Up @@ -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.