From 82d86afbe6651c23160c9754b3c17b2dd78d5aa0 Mon Sep 17 00:00:00 2001 From: danielsdeleo Date: Wed, 26 Jun 2013 14:54:48 -0700 Subject: Reap processes and close fds when exec fails Fixes OHAI-455: http://tickets.opscode.com/browse/OHAI-455 --- lib/ohai/mixin/command.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/ohai/mixin/command.rb b/lib/ohai/mixin/command.rb index 4487e3bf..a719e274 100644 --- a/lib/ohai/mixin/command.rb +++ b/lib/ohai/mixin/command.rb @@ -224,6 +224,10 @@ module Ohai begin e = Marshal.load ps.first + pw.last.close + pr.first.close + pe.first.close + Process.wait(cid) raise(Exception === e ? e : "unknown failure!") rescue EOFError # If we get an EOF error, then the exec was successful 42 -- cgit v1.2.1 From 96cc792d77b5d03808a65ca7e94501a3d6c3d980 Mon Sep 17 00:00:00 2001 From: danielsdeleo Date: Wed, 26 Jun 2013 17:33:07 -0700 Subject: Add regression test for popen4 zombie leak --- spec/unit/mixin/command_spec.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/spec/unit/mixin/command_spec.rb b/spec/unit/mixin/command_spec.rb index 0faa1df3..9e454117 100644 --- a/spec/unit/mixin/command_spec.rb +++ b/spec/unit/mixin/command_spec.rb @@ -44,4 +44,26 @@ describe Ohai::Mixin::Command, "popen4" do end end + it "reaps zombie processes after exec fails [OHAI-455]" do + # NOTE: depending on ulimit settings, GC, etc., before the OHAI-455 patch, + # ohai could also exhaust the available file descriptors when creating this + # many zombie processes. A regression _could_ cause Errno::EMFILE but this + # probably won't be consistent on different environments. + created_procs = 0 + 100.times do + begin + Ohai::Mixin::Command.popen4("/bin/this-is-not-a-real-command") {|p,i,o,e| nil } + rescue Ohai::Exceptions::Exec + created_procs += 1 + end + end + created_procs.should == 100 + reaped_procs = 0 + begin + loop { Process.wait(-1); reaped_procs += 1 } + rescue Errno::ECHILD + end + reaped_procs.should == 0 + end + end -- cgit v1.2.1