Skip to content

Commit

Permalink
bcc: add method to close file descriptors
Browse files Browse the repository at this point in the history
I have a long running process that attaches BPF programs to containers,
and I want those programs to stay attached after the BPF object goes out
of scope. After some time, the open file descriptors pile up and my
process starts failing since it cannot open new files.

This close() method allows me to create a BPF object, attach my
function, then close the associated file descriptors without detaching
the program.

Signed-off-by: Antonio Terceiro <[email protected]>
  • Loading branch information
terceiro committed Mar 22, 2022
1 parent 14dacd8 commit 63103fa
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 63103fa

Please sign in to comment.