Skip to content

Commit 72b7d71

Browse files
matthewdpitr-ch
authored andcommitted
Use block form of IO.popen to prevent zombies
1 parent f297472 commit 72b7d71

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

lib/concurrent/utility/processor_counter.rb

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,27 +86,25 @@ def compute_processor_count
8686
"select NumberOfLogicalProcessors from Win32_Processor")
8787
result.to_enum.collect(&:NumberOfLogicalProcessors).reduce(:+)
8888
elsif File.executable?("/usr/bin/nproc")
89-
IO.popen("/usr/bin/nproc --all").read.to_i
89+
IO.popen("/usr/bin/nproc --all", &:read).to_i
9090
elsif File.readable?("/proc/cpuinfo")
9191
IO.read("/proc/cpuinfo").scan(/^processor/).size
9292
elsif File.executable?("/usr/bin/hwprefs")
93-
IO.popen("/usr/bin/hwprefs thread_count").read.to_i
93+
IO.popen("/usr/bin/hwprefs thread_count", &:read).to_i
9494
elsif File.executable?("/usr/sbin/psrinfo")
95-
IO.popen("/usr/sbin/psrinfo").read.scan(/^.*on-*line/).size
95+
IO.popen("/usr/sbin/psrinfo", &:read).scan(/^.*on-*line/).size
9696
elsif File.executable?("/usr/sbin/ioscan")
97-
IO.popen("/usr/sbin/ioscan -kC processor") do |out|
98-
out.read.scan(/^.*processor/).size
99-
end
97+
IO.popen("/usr/sbin/ioscan -kC processor", &:read).scan(/^.*processor/).size
10098
elsif File.executable?("/usr/sbin/pmcycles")
101-
IO.popen("/usr/sbin/pmcycles -m").read.count("\n")
99+
IO.popen("/usr/sbin/pmcycles -m", &:read).count("\n")
102100
elsif File.executable?("/usr/sbin/lsdev")
103-
IO.popen("/usr/sbin/lsdev -Cc processor -S 1").read.count("\n")
101+
IO.popen("/usr/sbin/lsdev -Cc processor -S 1", &:read).count("\n")
104102
elsif File.executable?("/usr/sbin/sysconf") and os_name =~ /irix/i
105-
IO.popen("/usr/sbin/sysconf NPROC_ONLN").read.to_i
103+
IO.popen("/usr/sbin/sysconf NPROC_ONLN", &:read).to_i
106104
elsif File.executable?("/usr/sbin/sysctl")
107-
IO.popen("/usr/sbin/sysctl -n hw.ncpu").read.to_i
105+
IO.popen("/usr/sbin/sysctl -n hw.ncpu", &:read).to_i
108106
elsif File.executable?("/sbin/sysctl")
109-
IO.popen("/sbin/sysctl -n hw.ncpu").read.to_i
107+
IO.popen("/sbin/sysctl -n hw.ncpu", &:read).to_i
110108
else
111109
1
112110
end
@@ -118,7 +116,7 @@ def compute_processor_count
118116
def compute_physical_processor_count
119117
ppc = case RbConfig::CONFIG["target_os"]
120118
when /darwin1/
121-
IO.popen("/usr/sbin/sysctl -n hw.physicalcpu").read.to_i
119+
IO.popen("/usr/sbin/sysctl -n hw.physicalcpu", &:read).to_i
122120
when /linux/
123121
cores = {} # unique physical ID / core ID combinations
124122
phy = 0

0 commit comments

Comments
 (0)