Skip to content

fix(protocols): _lookup_next_layer memoises the resolved class on a hit, so a reload leaves it stale #1559

Closed

Description

@JarryShaw

Describe the bug
ProtocolBase._lookup_next_layer writes the resolved class back into the registry on a hit (pcapkit/protocols/protocol.py:2183-2184, registry[proto] = klass). After importlib.reload of the protocol's module, the registry keeps returning the old class. The method's own docstring (:2162-2168) says that memoising the resolved class under proto "would retain a class that importlib.reload then makes stale" (#421, #425, #555).

Reproduction

reg = IPv4.__proto__
k1 = ProtocolBase._lookup_next_layer(reg, TransType.UDP)
importlib.reload(pcapkit.protocols.transport.udp)
k2 = ProtocolBase._lookup_next_layer(reg, TransType.UDP)
k2 is sys.modules['pcapkit.protocols.transport.udp'].UDP   # False

Expected behavior
After a reload, the lookup returns the reloaded class, as the docstring promises. Alternatively, the docstring states that a hit memoises and goes stale after a reload.

System information
Linux, CPython 3.14.7, main at 16fbea054.

Additional context
A parse also mutates the shared registry this way, which is the order dependence #1557 had to isolate in test_dispatch_keys_did_not_move_with_the_modules. ModuleDescriptor.klass already reads sys.modules cheaply (#574), so dropping the write-back may cost little. Measure it before choosing. Found by the #1557 review; I reproduced it myself.

Activity

  1. added
    fixPull requests that fix a defect (fix: subject prefix)
    pendingAccepted and unstarted - nobody is on it and nothing blocks it
    on Oct 10, 2026
  2. JarryShaw commented on Oct 10, 2026

    @JarryShaw
    OwnerAuthor

    A worker is on this. First it will measure what dropping the hit write-back costs. If that cost is negligible, it drops the write-back so the code matches the docstring. If it is material, I will post the numbers here for a decision.

  3. added
    wipWork in flight - a covering PR is open or an agent is actively on it
    and removed
    pendingAccepted and unstarted - nobody is on it and nothing blocks it
    on Oct 10, 2026
  4. removed
    wipWork in flight - a covering PR is open or an agent is actively on it
    on Oct 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    fixPull requests that fix a defect (fix: subject prefix)

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions