Skip to content

Instantly share code, notes, and snippets.

@mkdynamic
Created November 21, 2012 23:56
Show Gist options
  • Save mkdynamic/4128620 to your computer and use it in GitHub Desktop.
Save mkdynamic/4128620 to your computer and use it in GitHub Desktop.
Utility class to help monitor open file descriptors
# Utility class to help monitor open file descriptors
#
class FileDescriptorSentry
class << self
# Return a count of all open file descriptors in the current process.
#
def count_open_fds
ObjectSpace.each_object(IO).count { |o| o.respond_to?(:closed?) && !o.closed? rescue false }
end
# Execute a block and print a warning on STDERR if any file descriptors are left open.
#
def check_open_fds
before = count_open_fds
yield
ensure
leaked = count_open_fds - before
$stderr.puts "You left #{leaked} file descriptors open!" if leaked > 0
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment