Skip to content

Instantly share code, notes, and snippets.

@samuelharmer
Last active August 6, 2018 06:30
Show Gist options
  • Select an option

  • Save samuelharmer/6e9b19cd46876a1f5519db776e92ae88 to your computer and use it in GitHub Desktop.

Select an option

Save samuelharmer/6e9b19cd46876a1f5519db776e92ae88 to your computer and use it in GitHub Desktop.
Isolating file handle leaks
#!/usr/bin/env python
# Based on https://stackoverflow.com/a/23762447/594137
# Snippet prints not only what handles are open but where
# the REG (regular) handles are on disk, helping to narrow
# down what's causing them in your code. Won't work on
# Windows due to using `/proc`.
def fd_stats():
import stat
from collections import defaultdict
_fd_types = (
('REG', stat.S_ISREG),
('FIFO', stat.S_ISFIFO),
('DIR', stat.S_ISDIR),
('CHR', stat.S_ISCHR),
('BLK', stat.S_ISBLK),
('LNK', stat.S_ISLNK),
('SOCK', stat.S_ISSOCK)
)
results = defaultdict(list)
for fd in range(100):
try:
s = os.fstat(fd)
except:
continue
for fd_type, func in _fd_types:
if func(s.st_mode):
break
else:
fd_type = str(s.st_mode)
results[fd_type] += [os.readlink('/proc/self/fd/%d' % fd)]
return results
def print_fd_stats():
import json
print(json.dumps(fd_stats(), indent=4, sort_keys=True))
Open file handles: 0: CHR, 1: REG, 2: REG, 3: CHR, 4: FIFO, 5: CHR, 6: FIFO, 7: FIFO, 8: REG, 10: REG
{
"CHR": [
"/dev/urandom"
],
"FIFO": [
"pipe:[121906]",
"pipe:[121907]",
"pipe:[121908]"
],
"REG": [
"/home/username/foo/temp_files/1c03831504974cefbdf7c4d18270892c/tmp5cch44z2 (deleted)",
"/home/username/foo/temp_files/968cdb7273f2403bbc6d670fc5b5fe48/tmp061vtabb (deleted)",
"/home/username/foo/temp_files/1c03831504974cefbdf7c4d18270892c/tmpc_qbhlhy (deleted)",
"/home/username/foo/temp_files/1c03831504974cefbdf7c4d18270892c/tmppy2rzafm (deleted)",
"/home/username/foo/temp_files/1c03831504974cefbdf7c4d18270892c/tmpzje91eeo (deleted)",
"/home/username/foo/temp_files/1c03831504974cefbdf7c4d18270892c/tmp4js5u1l3 (deleted)",
"/home/username/foo/temp_files/e0efcd40973143cc95e46f73179e0445/tmp7v7in0vf",
"/home/username/foo/temp_files/e0efcd40973143cc95e46f73179e0445/tmppzsfs428",
"/home/username/foo/temp_files/e0efcd40973143cc95e46f73179e0445/tmp4tcjzkq0",
"/home/username/foo/temp_files/e0efcd40973143cc95e46f73179e0445/tmpabua5a8v",
"/home/username/foo/temp_files/e0efcd40973143cc95e46f73179e0445/tmp4v2wn7jq"
],
"SOCK": [
"socket:[120783]",
"socket:[120783]",
"socket:[122713]",
"socket:[126049]"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment