Created
November 21, 2012 23:56
-
-
Save mkdynamic/4128620 to your computer and use it in GitHub Desktop.
Utility class to help monitor open file descriptors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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