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.
This commit is contained in:
voidborne-d 2026-03-31 22:13:26 +00:00
parent 5866c862be
commit deffec5b89
2 changed files with 54 additions and 2 deletions

View file

@ -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]:

View file

@ -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"
)