diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b8916f8f..a3621341f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/lib/redis/client.rb b/lib/redis/client.rb index 884b4dae3..55d3b3f42 100644 --- a/lib/redis/client.rb +++ b/lib/redis/client.rb @@ -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) @@ -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 " \ diff --git a/test/redis/client_test.rb b/test/redis/client_test.rb index 68668593a..0e0628cdd 100644 --- a/test/redis/client_test.rb +++ b/test/redis/client_test.rb @@ -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