Merge pull request #111 from pyopower/fix/obp-stream-state-leak

fix(obp): free per-stream state when OpenBridge streams end (memory grew ~2 KB per call)
pull/117/head
ce5rpy 2 months ago committed by GitHub
commit 345e81dad1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -133,6 +133,15 @@ class HbpForwardMixin:
self._silent_activation_log_key(system_name, peer_id, dst_id),
)
def _forget_ingress_drop_logs(self, system_name: str, stream_ids: list[bytes]) -> None:
"""Drop log-once keys of streams the trimmer removed (OBP has no per-slot VTERM hook)."""
cache = getattr(self, "_ingress_drop_logged", None)
if not cache:
return
gone = set(stream_ids)
for key in [k for k in cache if len(k) > 4 and k[1] == system_name and k[4] in gone]:
cache.discard(key)
def _clear_ingress_drop_log(
self,
system_name: str,

@ -283,8 +283,9 @@ class RoutingTimerMixin:
# Forward legs (H_LC): no END,RX and no cross-leg END,TX cascade.
if "_to" not in st and "_fin" not in st and last < now - 5:
if _obp_status_is_forward_leg(st):
if st.get("_end_tx_sent") or st.get("_bcsq_quenched"):
st["_to"] = True
# Idle forward leg: mark for removal even if VTERM never arrived,
# otherwise one row per forwarded call stays here forever.
st["_to"] = True
continue
rfs = st.get("RFS", b"\x00\x00\x00")
peer = st.get("RX_PEER", b"\x00\x00\x00\x00")
@ -321,6 +322,13 @@ class RoutingTimerMixin:
elif isinstance(st_rem, dict) and _obp_status_is_ingress(st_rem):
self._obp_emit_end_tx_for_forward_legs(stream_id, system_name, now)
obp_status.pop(stream_id, None)
# Per-stream TA state (DMRA/embedded buffers, relay dedupe) is only
# released on HBP VTERM; OBP streams must be freed here.
self.clear_talker_alias_stream(system_name, stream_id)
if to_remove:
self._forget_ingress_drop_logs(system_name, to_remove)
if hasattr(protocol, "trim_dmra_streams"):
protocol.trim_dmra_streams()
continue
for slot in (1, 2):
_slot = protocol.STATUS.get(slot)

@ -661,6 +661,8 @@ class RoutingUseCases(
entry["SYSTEM"], int_id(stream_id), _tx_report_peer, int_id(rf_src), entry.get("TS", 1), int_id(target_tgid), call_duration, int(synthetic_announcement)
)
)
# Leg is over: let the stream trimmer age it out (and not re-emit END,TX).
_target_status[stream_id]["_end_tx_sent"] = True
elif dtype_vseq in (1, 2, 3, 4):
self._rewrite_embed_lc(
dmrbits, _target_status[stream_id], dtype_vseq, "EMB_LC",

@ -252,3 +252,61 @@ def test_bcsq_end_tx_not_duplicated() -> None:
routing.on_obp_bcsq_received("OBP-ES", tgid, stream_id)
assert factory.events == []
def test_idle_forward_leg_without_end_tx_is_trimmed() -> None:
"""A forward leg whose VTERM/BCSQ never flagged it must still age out (was leaking one row per call)."""
routing, factory, clock, protocols = _obp_stack(("OBP-ES",))
stream_id = bytes_4(0x01020304)
tgid = bytes_3(213)
now = clock.time()
protocols["OBP-ES"].STATUS[stream_id] = _forward_leg(
peer_id=bytes_4(71411),
rf_src=bytes_3(2130035),
tgid=tgid,
start=now - 6,
last=now - 6,
)
with _patch_wall_time(clock):
routing.stream_trimmer_loop()
assert protocols["OBP-ES"].STATUS[stream_id].get("_to") is True
assert _end_tx_for(factory.events, "OBP-ES") == []
clock.advance(181)
routing.stream_trimmer_loop()
assert stream_id not in protocols["OBP-ES"].STATUS
assert len(_end_tx_for(factory.events, "OBP-ES")) == 1
def test_trimmed_obp_stream_releases_talker_alias_state() -> None:
"""OBP streams have no HBP VTERM hook: per-stream TA and log-once state must go with the row."""
routing, _factory, clock, protocols = _obp_stack(("OBP-ES",))
proto = protocols["OBP-ES"]
cleared: list[bytes] = []
trims: list[bool] = []
proto.clear_ta_stream_buffer = cleared.append
proto.trim_dmra_streams = lambda: trims.append(True)
stream_id = bytes_4(0x0A0B0C0D)
other_id = bytes_4(0x0A0B0C0E)
tgid = bytes_3(213)
now = clock.time()
proto.STATUS[stream_id] = _ingress_leg(
peer_id=bytes_4(71411), rf_src=bytes_3(2130035), tgid=tgid, start=now - 10, last=now - 6,
)
routing._passthrough_relayed.add(routing._both_ta_key("OBP-ES", stream_id))
busy_key = routing._ingress_drop_key("tg_busy", "OBP-ES", bytes_4(71411), tgid, stream_id)
other_key = routing._ingress_drop_key("tg_busy", "OBP-ES", bytes_4(71411), tgid, other_id)
routing._ingress_drop_log_cache().update({busy_key, other_key})
with _patch_wall_time(clock):
routing.stream_trimmer_loop()
clock.advance(181)
routing.stream_trimmer_loop()
assert stream_id not in proto.STATUS
assert cleared == [stream_id]
assert trims
assert routing._both_ta_key("OBP-ES", stream_id) not in routing._passthrough_relayed
assert busy_key not in routing._ingress_drop_log_cache()
assert other_key in routing._ingress_drop_log_cache()

Loading…
Cancel
Save

Powered by TurnKey Linux.