Skip to content

Call NFPlugin.cleanup() at the end of the meter workflow - #243

Open
drnpkr wants to merge 1 commit into
nfstream:masterfrom
drnpkr:fix/nfplugin-cleanup
Open

Call NFPlugin.cleanup() at the end of the meter workflow#243
drnpkr wants to merge 1 commit into
nfstream:masterfrom
drnpkr:fix/nfplugin-cleanup

Conversation

@drnpkr

@drnpkr drnpkr commented Jun 8, 2026

Copy link
Copy Markdown
Member

Problem

NFPlugin.cleanup() is documented ("Method called for plugin cleanup") but is never actually invoked anywhere in the package. Plugins that buffer state across flows — time-window/bin aggregators, file writers, batched exporters — have no hook to flush their final buffered state, so that data is silently lost at the end of a capture.

Fix

Invoke udp.cleanup() for each plugin in meter_workflow, right after meter_cleanup() has expired the remaining cached flows (so every on_expire hook has already run) and before the meter signals completion via channel.put(None).

Test

Adds test_plugin_cleanup: a plugin counts its on_expire calls and writes the total from cleanup(). The test asserts the marker file exists (i.e. cleanup() was actually called) and that the count equals the number of flows (i.e. it ran after all flows expired).

Scope note — live captures (follow-up needed)

This fix covers the graceful end-of-capture path, which today is only reached when capture_next hits end-of-file — i.e. file sources. On live interfaces the meter loop has no EOF: stopping via Ctrl-C (or reaching max_nflows, which internally raises KeyboardInterrupt) makes the streamer call meters[i].terminate() — a hard kill. In that path neither meter_cleanup() (so no on_expire for still-cached flows — a pre-existing loss unrelated to this PR) nor the new cleanup() hook runs.

A proper live-source fix is a larger coordination change (meters ignoring SIGINT, a shared stop event checked in the capture loop, and the streamer draining the channel until each meter sends its None sentinel instead of terminating). Deliberately kept out of this PR to keep it a minimal, reviewable fix; noted here so it isn't forgotten — follow-up PR planned.

NFPlugin.cleanup() is documented ('Method called for plugin cleanup') but
was never actually invoked anywhere in the package. Plugins that buffer
state across flows (e.g. time-window aggregators) therefore had no hook to
flush their final entries, silently losing them at end of capture.

Invoke udp.cleanup() for each plugin in meter_workflow, right after
meter_cleanup() expires the remaining cached flows (so all on_expire hooks
have already run) and before signalling completion.

Add test_plugin_cleanup: asserts cleanup() runs after all flows expire.
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.

1 participant