Skip to content

Commit

Permalink
Merge pull request #1145 from casperisfine/fork-disconnect
Browse files Browse the repository at this point in the history
Fix `Redis#close` to properly reset the fork protection check
  • Loading branch information
byroot authored Aug 31, 2022
2 parents ed0d03f + 28c65bc commit 7a867b6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Unreleased

- Fix `Redis#close` to properly reset the fork protection check.

# 5.0.1

- Added a fake `Redis::Connections.drivers` method to be compatible with older sidekiq versions.
Expand Down
9 changes: 7 additions & 2 deletions lib/redis/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def sentinel(**kwargs)
def initialize(*)
super
@inherit_socket = false
@pid = Process.pid
@pid = nil
end
ruby2_keywords :initialize if respond_to?(:ruby2_keywords, true)

Expand Down Expand Up @@ -114,10 +114,15 @@ def inherit_socket!
@inherit_socket = true
end

def close
super
@pid = nil
end

private

def ensure_connected(retryable: true)
unless @inherit_socket || Process.pid == @pid
unless @inherit_socket || (@pid ||= Process.pid) == Process.pid
raise InheritedError,
"Tried to use a connection from a child process without reconnecting. " \
"You need to reconnect to Redis after forking " \
Expand Down
13 changes: 13 additions & 0 deletions test/redis/client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,17 @@ def test_mixed_encoding
r.call("SET", "\x00\xFF", "fée")
assert_equal "fée", r.call("GET", "\x00\xFF".b)
end

def test_close_clear_pid
assert_equal "PONG", r.ping
fake_pid = Process.pid + 1
Process.stubs(:pid).returns(fake_pid)

assert_raises Redis::InheritedError do
r.ping
end

r.close
assert_equal "PONG", r.ping
end
end

0 comments on commit 7a867b6

Please sign in to comment.