Skip to content

fix(protocols): drop the warnings of a trial parse that is kept as Raw (#2308) - #2339

Merged
JarryShaw merged 1 commit into
mainfrom
fix/2308-trial-parse-warnings
Oct 27, 2026
Merged

JarryShaw merged 1 commit into
mainfrom
fix/2308-trial-parse-warnings

Conversation

@JarryShaw

Copy link
Copy Markdown
Owner

What is the purpose of your pull request?

  • fix — bug fix

Description

Closes #2308, per the ruling (take the recommendation).

  • Root cause: hold_warnings.__exit__ replayed held warnings on any Exception exit and ignored discard. So a short trial parse in _parse_next_layer that raised reported its warnings before beholder replaced the layer with Raw.
  • Fix:
    • hold_warnings honours discard on an Exception exit too.
    • beholder sets a _beholding context variable, and a trial that raises discards its warnings only while a beholder will replace the layer. With no beholder on the stack, the error and its warnings still reach the caller.
  • Tests: test_trial_parse_raises_2308_unit.py covers the repro on both the warnings and the logger channels, the error filter, a cut Hop-by-Hop header, and two controls (a kept Shim6 header still warns, and no beholder still warns). The docs(logging,pcap): per-frame logging note and Frame.read suite claim are false #2236 and fix(protocols): a trial next-layer parse that is replaced by Raw still emits its SchemaWarnings #1580 tests that pinned the leak are updated.
  • Fail-without-fix: on origin/main, 5 failed. On the head, 23 passed.
  • Left out: a layer captured at least as long as its hint is parsed without holding. If it then raises and becomes Raw, its warnings still show. That is pinned in test_layer_that_cannot_be_replaced_holds_nothing.

#2308)

Problem: a held trial parse in _parse_next_layer that raised had its
warnings replayed by hold_warnings.__exit__ whatever discard said, and
beholder then kept the layer as Raw, so an Ethernet frame cut inside its
IPv4 header warned "packet length < 0" for a layer it does not have.

Fix: hold_warnings honours discard on an Exception exit too; beholder
marks its call in a context variable, and _parse_next_layer discards a
raising trial's warnings under it. Outside beholder the error reaches the
caller and the warnings are still reported. The Raw's error names the cut.

Test: tests/protocols/test_trial_parse_raises_2308_unit.py; the #2236
test pins the cap_len argument directly and asserts the Raw's error; the
#1580 tests that pinned the leak are retargeted to kept layers.
@JarryShaw JarryShaw added this to the PyPCAPKit v1.5 milestone Oct 26, 2026
@JarryShaw JarryShaw added bug Issues reporting a defect (set by the bug report template; a default, not an assessment) fix Pull requests that fix a defect (fix: subject prefix) review: running A cross-review is in flight against the current head - no verdict yet labels Oct 26, 2026
@JarryShaw

Copy link
Copy Markdown
Owner Author

Review: GOOD TO GO at 9970fa305 (Sonnet).

  • hold_warnings: it drops on discard for any exit. A successful trial is unchanged, and _parse_next_layer is the only caller.
  • _beholding: set and reset in try/finally, and the token handles nesting. It behaves correctly in a fresh thread and under copy_context().run. With no beholder, ProtocolError is raised and its SchemaWarnings are still reported. pypcapfile's direct call keeps reporting.
  • Repro: a 10-octet IPv4 header gives Raw with the header-length error, and no SchemaWarnings on either channel.
  • Edited tests: a mutation of cap_len to len fails the docs(logging,pcap): per-frame logging note and Frame.read suite claim are false #2236 spy (154 != 24), so it pins cap_len. The fix(protocols): a trial next-layer parse that is replaced by Raw still emits its SchemaWarnings #1580 kept-layer guard moved to a Shim6 header that is genuinely kept.
  • Fail-without-fix: 5 failed on origin/main across the changed files (3 in the new file; its 2 controls pass). On the head, 581 passed.

@JarryShaw JarryShaw added review: good-to-go Cross-review at the current head says ready; CI state is separate and removed review: running A cross-review is in flight against the current head - no verdict yet labels Oct 26, 2026
@JarryShaw JarryShaw moved this to In review in PyPCAPKit Oct 26, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Coverage: 91.56% (unit tier, Python 3.15, 9970fa305, Unit Tests run success)

Package Statements Missed Branches Partial Cover
pcapkit (top level) 104 4 20 2 95.16%
pcapkit/const 19599 881 2618 867 92.11%
pcapkit/corekit 2594 74 816 37 96.51%
pcapkit/dumpkit 282 3 102 3 98.44%
pcapkit/foundation 3023 23 1044 25 98.82%
pcapkit/interface 138 4 52 3 96.32%
pcapkit/protocols 17441 218 4586 185 98.15%
pcapkit/toolkit 869 36 298 10 95.20%
pcapkit/utilities 502 7 144 5 98.14%
pcapkit/vendor 4432 1988 1018 167 51.87%

Per-file detail: the coverage-html artifact of this run.

@JarryShaw

Copy link
Copy Markdown
Owner Author

Should we promote _beholding to beholding to match house convention since it's cross referenced in the library - does not have to become a public API tho.

@JarryShaw

Copy link
Copy Markdown
Owner Author

Recommendation: keep _beholding private.

pcapkit/protocols/protocol.py already has the same shape — a module-level, private ContextVar (_rebuilding) that internet.py imports and reads across module boundaries, with no promotion and no Sphinx entry. _beholding is the same kind of thing: an implementation-internal coordination flag, not part of the constructed-object contract that the "document the contract, hide the recipe" convention surfaces. I'd leave it private and undocumented, matching _rebuilding.

@JarryShaw
JarryShaw merged commit 4615c31 into main Oct 27, 2026
68 checks passed
@JarryShaw
JarryShaw deleted the fix/2308-trial-parse-warnings branch October 27, 2026 00:12
@github-project-automation github-project-automation Bot moved this from In review to Done in PyPCAPKit Oct 27, 2026
@JarryShaw JarryShaw removed the review: good-to-go Cross-review at the current head says ready; CI state is separate label Oct 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Issues reporting a defect (set by the bug report template; a default, not an assessment) fix Pull requests that fix a defect (fix: subject prefix)

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

fix(protocols): a trial parse that raises and is kept as Raw still emits SchemaWarnings

1 participant