Skip to content

Commit

Permalink
Merge pull request iovisor#3919 from terceiro/python-bpf-close
Browse files Browse the repository at this point in the history
bcc: add method to close file descriptors
  • Loading branch information
davemarchevsky committed Jun 13, 2022
2 parents a184f09 + 63103fa commit 70e8a9b
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/python/bcc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1736,6 +1736,20 @@ def add_module(modname):
def donothing(self):
"""the do nothing exit handler"""


def close(self):
"""close(self)
Closes all associated files descriptors. Attached BPF programs are not
detached.
"""
for name, fn in list(self.funcs.items()):
os.close(fn.fd)
del self.funcs[name]
if self.module:
lib.bpf_module_destroy(self.module)
self.module = None

def cleanup(self):
# Clean up opened probes
for k, v in list(self.kprobe_fds.items()):
Expand Down Expand Up @@ -1763,12 +1777,8 @@ def cleanup(self):
if self.tracefile:
self.tracefile.close()
self.tracefile = None
for name, fn in list(self.funcs.items()):
os.close(fn.fd)
del self.funcs[name]
if self.module:
lib.bpf_module_destroy(self.module)
self.module = None

self.close()

# Clean up ringbuf
if self._ringbuf_manager:
Expand Down

0 comments on commit 70e8a9b

Please sign in to comment.