Description
Both network_tracker.py and process_tracker.py contain similar boilerplate for attaching all programs and managing their links:
self._links = []
for name, prog in self.obj.programs.items():
print(f" Attaching {name} ({prog.section})...")
try:
link = prog.attach()
self._links.append(link)
except tinybpf.BpfError as e:
print(f" Warning: Could not attach {name}: {e}")
# Later, in cleanup:
for link in self._links:
link.destroy()
This pattern is verbose and easy to get wrong (forgetting to destroy links, not handling errors consistently).
Proposed API
1. BpfObject.attach_all()
# Basic usage - raises on first error
links = obj.attach_all()
# With error handling - returns list of (name, link_or_exception) tuples
results = obj.attach_all(ignore_errors=True)
for name, result in results:
if isinstance(result, Exception):
print(f"Warning: {name} failed: {result}")
else:
print(f"Attached {name}")
# Get only successful links
links = [r for name, r in results if not isinstance(r, Exception)]
2. LinkCollection Context Manager
class LinkCollection:
"""Manages multiple BpfLinks with automatic cleanup."""
def __init__(self, links: list[BpfLink]):
self._links = list(links)
def __enter__(self):
return self
def __exit__(self, *args):
for link in self._links:
link.destroy()
def __iter__(self):
return iter(self._links)
def __len__(self):
return len(self._links)
Usage:
with LinkCollection(obj.attach_all()) as links:
print(f"Attached {len(links)} programs")
# ... run event loop ...
# All links automatically destroyed
3. Combined: attach_all() returns LinkCollection
with obj.attach_all() as links: # returns LinkCollection
rb.poll()
# Auto-cleanup
Benefits
- Less boilerplate - common pattern becomes one line
- Correct resource cleanup - context manager ensures links are destroyed
- Consistent error handling -
ignore_errors flag for partial attachment
- Discoverable - users find it via
obj. autocomplete
Implementation Notes
attach_all() iterates obj.programs.values() and calls prog.attach()
LinkCollection is a simple wrapper with __enter__/__exit__
- Could also add
links.destroy_all() for manual cleanup
- Consider making it iterable for
for link in links: patterns
Related
This would simplify the examples and reduce copy-paste errors when users write their own trackers.
Description
Both
network_tracker.pyandprocess_tracker.pycontain similar boilerplate for attaching all programs and managing their links:This pattern is verbose and easy to get wrong (forgetting to destroy links, not handling errors consistently).
Proposed API
1. BpfObject.attach_all()
2. LinkCollection Context Manager
Usage:
3. Combined: attach_all() returns LinkCollection
Benefits
ignore_errorsflag for partial attachmentobj.autocompleteImplementation Notes
attach_all()iteratesobj.programs.values()and callsprog.attach()LinkCollectionis a simple wrapper with__enter__/__exit__links.destroy_all()for manual cleanupfor link in links:patternsRelated
This would simplify the examples and reduce copy-paste errors when users write their own trackers.