Skip to content

refactor(ebpf-extractor): split binary into library crate with graceful shutdown - #380

Merged
0xB10C merged 3 commits into
peer-observer:masterfrom
deadmanoz:refactor/ebpf-testable-run
Apr 2, 2026
Merged

0xB10C merged 3 commits into
peer-observer:masterfrom
deadmanoz:refactor/ebpf-testable-run

Conversation

@deadmanoz

Copy link
Copy Markdown
Collaborator

Summary

The ebpf-extractor was the only extractor without a lib.rs / main.rs split: all logic lived in main.rs, which blocked integration test coverage for issue #26. This PR restructures it to match the rpc, log, and p2p extractors: lib.rs now exposes a public Args struct and pub async fn run(args, shutdown_rx), while main.rs is reduced to argument parsing, logger initialization, shutdown setup, and calling run().

Two implementation details intentionally diverge from the other extractors:

  1. run() is not spawned with tokio::spawn
    The ebpf extractor's run() future is !Send because it holds libbpf state built on raw-pointer-backed FFI types. tokio::spawn requires Send, since spawned tasks may move between runtime worker threads. As a result, run() executes directly on the main task and only the Ctrl+C handler is spawned.

  2. Shutdown uses synchronous has_changed() instead of tokio::select!
    The main extraction loop calls ring_buffers.poll_raw(Duration::from_secs(1)), which is a blocking FFI call rather than an async .await point. A non-blocking shutdown check before each poll iteration is therefore the natural fit, with up to ~1 second shutdown latency. Dropped senders are treated as shutdown, matching the Err(_) => break behavior used by the other extractors.

The work is split into three commits:

  1. pure code movement into lib.rs
  2. shutdown channel support
  3. logger-init alignment with the other extractors

Testing

Verified locally (NixOS x86_64):

  • clippy: zero warnings
  • Release build: clean
  • --help: CLI interface unchanged
  • Bogus PID invocation without required privileges: graceful error through run(), for example:
$ target/release/ebpf-extractor --bitcoind-path /usr/bin/bitcoind --bitcoind-pid 99999
INFO  [ebpf_extractor] Using bitcoind PID=99999 specified via command line option
INFO  [ebpf_extractor] Opening BPF skeleton with debug=false..
INFO  [ebpf_extractor] Loading BPF functions and maps into kernel..
ERROR [ebpf_extractor] Fatal error during extractor runtime: libbpf error: Operation not permitted (os error 1)

No integration tests added yet.. though this PR provides the structural prerequisite for integration tests to follow!

Move all extractor logic from main.rs into lib.rs, matching the pattern
established by the rpc, log, and p2p extractors. The new lib.rs exposes
a public Args struct (with a new() constructor for programmatic use) and
a public async run() function that can be called from integration tests.

main.rs is reduced to arg parsing and a direct call to
ebpf_extractor::run(). No behavioural changes - the extractor runs
identically to before.
Add a shutdown_rx: watch::Receiver<bool> parameter to run(), matching
the shutdown pattern used by the rpc, log, and p2p extractors. The poll
loop checks the channel non-blockingly each iteration.

main.rs spawns a lightweight Ctrl+C handler that sends the shutdown
signal. The run() future itself executes directly on the main task
(not via tokio::spawn) because libbpf types contain raw pointers that
make the future !Send.

This enables integration tests to programmatically start and stop the
extractor via the watch channel.
@0xB10C

0xB10C commented Mar 13, 2026

Copy link
Copy Markdown
Collaborator

Concept ACK on this refactor!


I didn't put this high on my list personally as integration tests for the ebpf-extractor might be a pain, but ultimatly they would be good to have:

I don't want to recommend devs to run them locally due to their privilege requirements (especially if they aren't touching the ebpf-extractor in their code changes). This means introducing another feature flag for it, and not being able to run with --all-features locally. We can and should run them in CI. Maybe we can also implement a "skip", if the user does not have the right priviledges...

@deadmanoz

Copy link
Copy Markdown
Collaborator Author

No worries, on the ever-growing list!!

@0xB10C 0xB10C mentioned this pull request Mar 13, 2026
4 of 6 tasks
@0xB10C

0xB10C commented Apr 2, 2026

Copy link
Copy Markdown
Collaborator

Reviewed and tested!

Thank you @deadmanoz!

@0xB10C
0xB10C merged commit b1b3240 into peer-observer:master Apr 2, 2026
12 checks passed
@deadmanoz
deadmanoz deleted the refactor/ebpf-testable-run branch May 28, 2026 08:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants