Skip to content

Commit 9429205

Browse files
authored
Merge pull request #1158 from casperisfine/fork-handling
Fix automatic disconnection when the process was forked
2 parents fdf61e5 + 389d8b6 commit 9429205

File tree

6 files changed

+10
-76
lines changed

6 files changed

+10
-76
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 automatic disconnection when the process was forked. See #1157.
4+
35
# 5.0.4
46

57
- Cast `ttl` argument to integer in `expire`, `setex` and a few others.

cluster/lib/redis/cluster/client.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ def sentinel(**kwargs)
2424

2525
def initialize(*)
2626
handle_errors { super }
27-
@inherit_socket = false
28-
@pid = Process.pid
2927
end
3028
ruby2_keywords :initialize if respond_to?(:ruby2_keywords, true)
3129

lib/redis/client.rb

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,6 @@ def sentinel(**kwargs)
2828
end
2929
end
3030

31-
def initialize(*)
32-
super
33-
@inherit_socket = false
34-
@pid = nil
35-
end
36-
ruby2_keywords :initialize if respond_to?(:ruby2_keywords, true)
37-
3831
def id
3932
config.id
4033
end
@@ -115,11 +108,6 @@ def inherit_socket!
115108
@inherit_socket = true
116109
end
117110

118-
def close
119-
super
120-
@pid = nil
121-
end
122-
123111
private
124112

125113
def translate_error!(error)
@@ -136,16 +124,5 @@ def translate_error_class(error_class)
136124
raise
137125
end
138126
end
139-
140-
def ensure_connected(retryable: true)
141-
unless @inherit_socket || (@pid ||= Process.pid) == Process.pid
142-
raise InheritedError,
143-
"Tried to use a connection from a child process without reconnecting. " \
144-
"You need to reconnect to Redis after forking " \
145-
"or set :inherit_socket to true."
146-
end
147-
148-
super
149-
end
150127
end
151128
end

redis.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,5 @@ Gem::Specification.new do |s|
4545

4646
s.required_ruby_version = '>= 2.5.0'
4747

48-
s.add_runtime_dependency('redis-client', '>= 0.7.4')
48+
s.add_runtime_dependency('redis-client', '>= 0.9.0')
4949
end

test/redis/client_test.rb

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,4 @@ def test_mixed_encoding
4343
r.call("SET", "\x00\xFF", "fée")
4444
assert_equal "fée", r.call("GET", "\x00\xFF".b)
4545
end
46-
47-
def test_close_clear_pid
48-
assert_equal "PONG", r.ping
49-
fake_pid = Process.pid + 1
50-
Process.stubs(:pid).returns(fake_pid)
51-
52-
assert_raises Redis::InheritedError do
53-
r.ping
54-
end
55-
56-
r.close
57-
assert_equal "PONG", r.ping
58-
end
5946
end

test/redis/fork_safety_test.rb

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,45 +11,15 @@ def setup
1111

1212
def test_fork_safety
1313
redis = Redis.new(OPTIONS)
14-
redis.set "foo", 1
15-
16-
child_pid = fork do
17-
# InheritedError triggers a reconnect,
18-
# so we need to disable reconnects to force
19-
# the exception bubble up
20-
redis.without_reconnect do
21-
redis.set "foo", 2
14+
pid = fork do
15+
1000.times do
16+
assert_equal "OK", redis.set("key", "foo")
2217
end
23-
exit! 0
24-
rescue Redis::InheritedError
25-
exit! 127
2618
end
27-
28-
_, status = Process.wait2(child_pid)
29-
30-
assert_equal 127, status.exitstatus
31-
assert_equal "1", redis.get("foo")
32-
end
33-
34-
def test_fork_safety_with_enabled_inherited_socket
35-
redis = Redis.new(OPTIONS.merge(inherit_socket: true))
36-
redis.set "foo", 1
37-
38-
child_pid = fork do
39-
# InheritedError triggers a reconnect,
40-
# so we need to disable reconnects to force
41-
# the exception bubble up
42-
redis.without_reconnect do
43-
redis.set "foo", 2
44-
end
45-
exit! 0
46-
rescue Redis::InheritedError
47-
exit! 127
19+
1000.times do
20+
assert_equal "PONG", redis.ping
4821
end
49-
50-
_, status = Process.wait2(child_pid)
51-
52-
assert_equal 0, status.exitstatus
53-
assert_equal "2", redis.get("foo")
22+
_, status = Process.wait2(pid)
23+
assert_predicate(status, :success?)
5424
end
5525
end

0 commit comments

Comments
 (0)