fix(engine): count overflow and shutdown drops in packets, not queue copies (audit F6) - #54
Merged
Conversation
…copies `_enqueue()` runs once per element of `dec.releases`, and pipeline step 12 adds a second element for a duplicate, so `drop_overflow` and `drop_shutdown` charged for the copy as well as for the packet. Measured: seen=1000, duplicated=1000 -> drop_overflow=1900, drop_shutdown=100, which let the "Buffer overflow" tile read higher than the "Packets" tile beside it - while both tooltips promise "Packets". Heap entries carry a `copy` flag as a fourth element (ordering unaffected: the unique counter means comparison never reaches it), `_enqueue` takes `copy=False`, and `stop()` counts entries that are not copies. A refused copy no longer warns either: `log.queue_overflow` interpolates the counter, so warning on a copy would print "(0 so far)". A queue that refuses only copies costs the user nothing. Hot path measured before and after against a worktree of master (150k 1500 B packets, median of 5): 153.8k -> 156.0k pkt/s without duplication, 100.9k -> 102.4k with. The ranges overlap, so this reads as no measurable regression. Both new guards verified by mutation: reverting each half turns them red with exactly the pre-fix numbers (390 and 400 against seen=200). Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Audit finding F6.
drop_overflowanddrop_shutdowncounted queue ENTRIES while bothtooltips promise "Packets".
_enqueue()runs once per element ofdec.releases, and pipeline step 12 adds a second elementfor a duplicate, so a duplicating session charged for the copy as well as for the packet.
Measured before the fix:
seen=1000,duplicated=1000->drop_overflow=1900,drop_shutdown=100. The "Buffer overflow" tile could read higher than the "Packets" tile besideit in the same session.
What changed (
beantester/engine.pyonly)_enqueue(release, packet, copy=False); heap entries carry the flag as a fourth element(release, counter, packet, copy). Ordering is unaffected:counteris unique, so tuplecomparison never reaches index 2 or 3.
stop()counts entries that are not copies instead oflen(self._heap).releases[0]directly and only branches into a loop for duplicates,so the single-release path (every session without duplication) does not pay for an
enumerate.log.queue_overflowinterpolates the counter, sowarning on a copy would print "the TOOL is now dropping packets you did not ask to lose
(0 so far)". Counter, log line and GUI banner now tell the same story, and a queue that
refuses only copies is costing the user nothing.
Known, deliberate gap, recorded in the docstring rather than engineered away: if the original is
refused and the injector then frees a slot so the duplicate fits, the packet is delivered up to
20 ms late yet counted once as lost.
Verification
python -m pytest tests-> 679 passed, 0 failed on an elevated shell (so zero failures isthe full bar, not the two known non-admin ones).
python smoke_gui.py-> OK.Two new guards in
tests/test_engine.py, both verified by mutation: reverting each half ofthe fix turns them red with exactly the pre-fix numbers,
drop_overflow=390anddrop_shutdown=400againstseen=200. Deterministic, not statistical:dup=100%fires onevery packet and the 60 s latency releases nothing before STOP.
Hot path measured before and after, back to back against a
git worktreeof master (Win11AMD64, CPython 3.14.6, 150k pre-built 1500 B packets through
FakeDivert, median of 5):Medians moved about 1.4% in this branch's favour, but the per-run ranges overlap, so the honest
reading is no measurable regression, not a speedup.
Not verified: behaviour under a real WinDivert overflow. The queue arithmetic is engine-side and
identical on either driver, so nothing here depends on the real path.
🤖 Generated with Claude Code