Skip to content

Commit 28c65bc

Browse files
committed
Fix Redis#close to properly reset the fork protection check
If you properly close the connection in the forked process, you can safely use it.
1 parent ed0d03f commit 28c65bc

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Unreleased
22

3+
- Fix `Redis#close` to properly reset the fork protection check.
4+
35
# 5.0.1
46

57
- Added a fake `Redis::Connections.drivers` method to be compatible with older sidekiq versions.

lib/redis/client.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def sentinel(**kwargs)
3030
def initialize(*)
3131
super
3232
@inherit_socket = false
33-
@pid = Process.pid
33+
@pid = nil
3434
end
3535
ruby2_keywords :initialize if respond_to?(:ruby2_keywords, true)
3636

@@ -114,10 +114,15 @@ def inherit_socket!
114114
@inherit_socket = true
115115
end
116116

117+
def close
118+
super
119+
@pid = nil
120+
end
121+
117122
private
118123

119124
def ensure_connected(retryable: true)
120-
unless @inherit_socket || Process.pid == @pid
125+
unless @inherit_socket || (@pid ||= Process.pid) == Process.pid
121126
raise InheritedError,
122127
"Tried to use a connection from a child process without reconnecting. " \
123128
"You need to reconnect to Redis after forking " \

test/redis/client_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,17 @@ def test_mixed_encoding
3434
r.call("SET", "\x00\xFF", "fée")
3535
assert_equal "fée", r.call("GET", "\x00\xFF".b)
3636
end
37+
38+
def test_close_clear_pid
39+
assert_equal "PONG", r.ping
40+
fake_pid = Process.pid + 1
41+
Process.stubs(:pid).returns(fake_pid)
42+
43+
assert_raises Redis::InheritedError do
44+
r.ping
45+
end
46+
47+
r.close
48+
assert_equal "PONG", r.ping
49+
end
3750
end

0 commit comments

Comments
 (0)