Skip to content

Commit eb4f482

Browse files
committed
server: close all connections correctly at shutdown
Signed-off-by: Shizuo Fujita <fujita@clear-code.com>
1 parent 41d5102 commit eb4f482

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

lib/fluent/plugin_helper/server.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,10 @@ def stop
356356
end
357357

358358
def shutdown
359-
@_server_connections.each do |conn|
359+
# When it invokes conn.cose, it reduces elements in @_server_connections by close_callback,
360+
# and it reduces the number of loops. This prevents the connection closing.
361+
# So, it requires invoking #dup to avoid the problem.
362+
@_server_connections.dup.each do |conn|
360363
conn.close rescue nil
361364
end
362365

test/plugin_helper/test_server.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,24 @@ class Dummy < Fluent::Plugin::TestBase
381381
d2.stop; d2.before_shutdown; d2.shutdown; d2.after_shutdown; d2.close; d2.terminate
382382
end
383383
end
384+
385+
test 'close all connections by shutdown' do
386+
@d.server_create_tcp(:s, @port) do |data, conn|
387+
end
388+
389+
client_sockets = []
390+
5.times do
391+
client_sockets << TCPSocket.open("127.0.0.1", @port)
392+
end
393+
waiting(4){ sleep 0.1 until @d.instance_variable_get(:@_server_connections).size == 5 }
394+
395+
@d.before_shutdown
396+
@d.shutdown
397+
398+
assert_true @d.instance_variable_get(:@_server_connections).empty?
399+
400+
client_sockets.each(&:close)
401+
end
384402
end
385403

386404
sub_test_case '#server_create' do

0 commit comments

Comments
 (0)