From deffec5b89922f25cf1eb34a2475b1664384f07f Mon Sep 17 00:00:00 2001 From: voidborne-d Date: Tue, 31 Mar 2026 22:13:26 +0000 Subject: [PATCH] fix: colon-safe endpoint ID extraction + dedup stale cleanup Address review feedback from greptile-apps[bot]: - Use regex to anchor on :exact: / :subpath: delimiters instead of naive split(':', 1)[0], so endpoint IDs containing colons (e.g. 'svc:v2') are extracted correctly. - Deduplicate remove_endpoint_routes() calls when an endpoint has both exact and subpath routes (prevents redundant dict scans). - Add test for colon-safe ID extraction. --- .../pass_through_endpoints.py | 20 +++++++++-- .../test_passthrough_registry_leak.py | 36 +++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/litellm/proxy/pass_through_endpoints/pass_through_endpoints.py b/litellm/proxy/pass_through_endpoints/pass_through_endpoints.py index ed9a3d820fd..4de1e7e6de0 100644 --- a/litellm/proxy/pass_through_endpoints/pass_through_endpoints.py +++ b/litellm/proxy/pass_through_endpoints/pass_through_endpoints.py @@ -2301,6 +2301,9 @@ async def initialize_pass_through_endpoints( ) # remove the ones that are not visited from the list + import re + + removed_endpoint_ids: set = set() for endpoint_key in registered_pass_through_endpoints: if endpoint_key not in visited_endpoints: # Route keys are formatted as "{endpoint_id}:exact:{path}:{methods}" @@ -2309,8 +2312,21 @@ async def initialize_pass_through_endpoints( # we must split it out here. Previously the full key was passed # verbatim, which never matched any stored endpoint_id and silently # left stale routes in the registry forever. - stale_endpoint_id = endpoint_key.split(":", 1)[0] - InitPassThroughEndpointHelpers.remove_endpoint_routes(stale_endpoint_id) + # + # Use regex to anchor on `:exact:` or `:subpath:` delimiters so + # endpoint IDs that contain colons (e.g. "svc:v2") are handled + # correctly instead of being truncated at the first colon. + _match = re.match(r"^(.+?):(?:exact|subpath):", endpoint_key) + stale_endpoint_id = ( + _match.group(1) if _match else endpoint_key.split(":", 1)[0] + ) + # Deduplicate: an endpoint with include_subpath=True produces two + # registry keys (exact + subpath). Only call remove once. + if stale_endpoint_id not in removed_endpoint_ids: + InitPassThroughEndpointHelpers.remove_endpoint_routes( + stale_endpoint_id + ) + removed_endpoint_ids.add(stale_endpoint_id) def _get_pass_through_endpoints_from_config() -> List[PassThroughGenericEndpoint]: diff --git a/tests/pass_through_unit_tests/test_passthrough_registry_leak.py b/tests/pass_through_unit_tests/test_passthrough_registry_leak.py index a299a56e6e1..0e89d09c02e 100644 --- a/tests/pass_through_unit_tests/test_passthrough_registry_leak.py +++ b/tests/pass_through_unit_tests/test_passthrough_registry_leak.py @@ -253,3 +253,39 @@ def test_explicit_id_is_preserved(): del _registered_pass_through_routes[k] asyncio.run(_run()) + + +# --------------------------------------------------------------------------- +# Test 6 – stale endpoint cleanup: colon-safe ID extraction +# --------------------------------------------------------------------------- + +def test_stale_endpoint_cleanup_colon_safe_split(): + """ + Verify that the cleanup loop correctly extracts the endpoint_id from + a registry key even when the ID contains a colon (e.g. 'svc:v2'), + and that deduplication prevents redundant remove calls. + """ + import re + + # Simulate registry keys with a colon-containing ID + colon_id = "svc:v2" + route_key_exact = f"{colon_id}:exact:/some/path:GET,POST" + route_key_subpath = f"{colon_id}:subpath:/some/path:GET,POST" + + # Verify regex-based extraction works correctly for both key types + for key in [route_key_exact, route_key_subpath]: + _match = re.match(r"^(.+?):(?:exact|subpath):", key) + extracted = _match.group(1) if _match else key.split(":", 1)[0] + assert extracted == colon_id, ( + f"Expected {colon_id!r} but got {extracted!r} from key {key!r}. " + "The colon-safe split is broken." + ) + + # Also verify standard auto-generated IDs still work + auto_id = "auto-abc123def456" + auto_key = f"{auto_id}:exact:/test:GET" + _match = re.match(r"^(.+?):(?:exact|subpath):", auto_key) + extracted = _match.group(1) if _match else auto_key.split(":", 1)[0] + assert extracted == auto_id, ( + f"Expected {auto_id!r} but got {extracted!r} for auto-generated ID" + )