From 3b6245536cf55da9e8bfcdb03c845fe9ef931d7f Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Fri, 3 Apr 2026 16:26:47 +0900 Subject: [PATCH] LeakChecker: skip CLOSE_WAIT sockets Those FDs still live in the kernel space but have been closed in the user space. --- tool/lib/leakchecker.rb | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/tool/lib/leakchecker.rb b/tool/lib/leakchecker.rb index 513ffd23eb5f7c..36662e9a46761d 100644 --- a/tool/lib/leakchecker.rb +++ b/tool/lib/leakchecker.rb @@ -77,6 +77,7 @@ def check_fd_leak(test_name) end (h[fd] ||= []) << [io, autoclose, inspect] } + inspect = {} fd_leaked.select! {|fd| str = ''.dup pos = nil @@ -98,6 +99,7 @@ def check_fd_leak(test_name) s = io.stat rescue Errno::EBADF # something un-stat-able + live2.delete(fd) next else next if /darwin/ =~ RUBY_PLATFORM and [0, -1].include?(s.dev) @@ -106,15 +108,35 @@ def check_fd_leak(test_name) io&.close end end - puts "Leaked file descriptor: #{test_name}: #{fd}#{str}" - puts " The IO was created at #{pos}" if pos + inspect[fd] = [str, pos] true } unless fd_leaked.empty? unless @@try_lsof == false - @@try_lsof |= system(*%W[lsof -w -a -d #{fd_leaked.minmax.uniq.join("-")} -p #$$], out: Test::Unit::Runner.output) + open_list = IO.popen(%W[lsof -w -a -d #{fd_leaked.minmax.uniq.join("-")} -p #$$], &:readlines) + if @@try_lsof |= $?.success? + columns = (header = open_list.shift).split + fd_index, node_index = columns.index('FD'), columns.index('NODE') + open_list.reject! do |of| + of = of.chomp.split(' ', node_index + 2) + if of[node_index] == 'TCP' and of.last.end_with?('(CLOSE_WAIT)') + fd = of[fd_index].to_i + inspect.delete(fd) + h.delete(fd) + live2.delete(fd) + true + else + false + end + end + end + puts(header, open_list) unless open_list.empty? end end + inspect.each {|fd, (str, pos)| + puts "Leaked file descriptor: #{test_name}: #{fd}#{str}" + puts " The IO was created at #{pos}" if pos + } h.each {|fd, list| next if list.length <= 1 if 1 < list.count {|io, autoclose, inspect| autoclose }