From 93920f3555633935b2d3743061deb7c37e92fed3 Mon Sep 17 00:00:00 2001 From: Kentaro Hayashi Date: Tue, 15 Feb 2022 15:29:47 +0900 Subject: [PATCH] in_exec: fix a file descriptor leaks Every calling command from in_exec plugin, it causes a file descriptor leak. If interval is short enough or running Fluentd in long term, it will reaches the limit of file descriptor - "too many open files". In this commit, ensure to close IO object when it reaches EOF. Signed-off-by: Kentaro Hayashi --- lib/fluent/plugin/in_exec.rb | 1 + test/plugin/test_in_exec.rb | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/lib/fluent/plugin/in_exec.rb b/lib/fluent/plugin/in_exec.rb index c285136604..e435b8f337 100644 --- a/lib/fluent/plugin/in_exec.rb +++ b/lib/fluent/plugin/in_exec.rb @@ -95,6 +95,7 @@ def run(io) else @parser.parse(io.read, &method(:on_record)) end + io.close if io.eof? end def on_record(time, record) diff --git a/test/plugin/test_in_exec.rb b/test/plugin/test_in_exec.rb index cbce44313b..c52d39b347 100644 --- a/test/plugin/test_in_exec.rb +++ b/test/plugin/test_in_exec.rb @@ -258,4 +258,14 @@ def create_driver(conf) assert_match(/LoadError/, event[2]['message']) end end + + test 'ensure not leaking file descriptor' do + omit "/proc/PID is not available on Windows" if Fluent.windows? + d = create_driver JSON_CONFIG_COMPAT + before_fd_count = Dir.glob("/proc/#{$$}/fd/*").length + d.run(expect_records: 10, timeout: 10) + after_fd_count = Dir.glob("/proc/#{$$}/fd/*").length + + assert{ before_fd_count == after_fd_count } + end end