From c3db5b4c421154f7f2490d37d47bd851c1f63843 Mon Sep 17 00:00:00 2001 From: yo Date: Mon, 28 Sep 2026 19:24:29 +0200 Subject: [PATCH 1/2] fix(obp): trim idle forward legs so STATUS does not grow per call HBP->OBP forward legs (to_target rows with H_LC) only became eligible for removal when flagged _end_tx_sent/_bcsq_quenched. The VTERM path emitted END,TX without setting the flag, so one row per forwarded call and per OBP stayed in STATUS forever (~3 KB/call measured; ~30-40 MB/day on a live master). - VTERM on a forward leg now sets _end_tx_sent. - The stream trimmer marks any idle forward leg _to, so it is removed after 180 s even if VTERM never arrived. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vz1o2mo7r43iNnpD2MQRLA --- src/adn_server/application/routing/timers.py | 5 ++-- .../application/routing_use_cases.py | 2 ++ .../application/test_obp_monitor_voice_end.py | 25 +++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/adn_server/application/routing/timers.py b/src/adn_server/application/routing/timers.py index 533f014..535ad82 100644 --- a/src/adn_server/application/routing/timers.py +++ b/src/adn_server/application/routing/timers.py @@ -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") diff --git a/src/adn_server/application/routing_use_cases.py b/src/adn_server/application/routing_use_cases.py index f5590e6..289b192 100644 --- a/src/adn_server/application/routing_use_cases.py +++ b/src/adn_server/application/routing_use_cases.py @@ -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", diff --git a/tests/application/test_obp_monitor_voice_end.py b/tests/application/test_obp_monitor_voice_end.py index 1a7c26a..f5d1ca1 100644 --- a/tests/application/test_obp_monitor_voice_end.py +++ b/tests/application/test_obp_monitor_voice_end.py @@ -252,3 +252,28 @@ 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 From 59ae137b60484f0db184eda5ffe56bbe9bea96f9 Mon Sep 17 00:00:00 2001 From: yo Date: Wed, 30 Sep 2026 08:24:26 +0200 Subject: [PATCH 2/2] fix(obp): release per-stream talker-alias state when OBP streams are trimmed Talker-alias buffers (_dmra_by_stream, _ta_voice_acc, _ta_complete), the passthrough relay dedupe set and log-once ingress keys are only released on the HBP VTERM hook. OPENBRIDGE streams never hit it, and the OBP branch of stream_trimmer_loop returned before trim_dmra_streams(), so every stream seen on an OBP left one entry per structure behind (~38k after 35 h on a live master, ~2 KB per call). - On stage-2 removal of an OBP stream, call clear_talker_alias_stream() and drop the stream's log-once ingress keys. - Run trim_dmra_streams() for OBP protocols too (catches buffers keyed by streams that never got a STATUS row). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01JnjAx7G27d9q88j9PFLj11 --- .../application/routing/hbp_forward.py | 9 +++++ src/adn_server/application/routing/timers.py | 7 ++++ .../application/test_obp_monitor_voice_end.py | 33 +++++++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/src/adn_server/application/routing/hbp_forward.py b/src/adn_server/application/routing/hbp_forward.py index 65fe3a7..76e4501 100644 --- a/src/adn_server/application/routing/hbp_forward.py +++ b/src/adn_server/application/routing/hbp_forward.py @@ -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, diff --git a/src/adn_server/application/routing/timers.py b/src/adn_server/application/routing/timers.py index 535ad82..011f2aa 100644 --- a/src/adn_server/application/routing/timers.py +++ b/src/adn_server/application/routing/timers.py @@ -322,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) diff --git a/tests/application/test_obp_monitor_voice_end.py b/tests/application/test_obp_monitor_voice_end.py index f5d1ca1..0ef53e8 100644 --- a/tests/application/test_obp_monitor_voice_end.py +++ b/tests/application/test_obp_monitor_voice_end.py @@ -277,3 +277,36 @@ def test_idle_forward_leg_without_end_tx_is_trimmed() -> None: 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()