From acce761917f1ab7b1b7c47e90a7e8d397e36e018 Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Mon, 20 Jul 2026 21:57:21 +0530 Subject: [PATCH] chore: incorporate review comments --- api/services/telephony/providers/ari/strategies.py | 7 +++++++ .../telephony/providers/ari/test_external_pbx.py | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/api/services/telephony/providers/ari/strategies.py b/api/services/telephony/providers/ari/strategies.py index 8e053b9b..cfbddeb0 100644 --- a/api/services/telephony/providers/ari/strategies.py +++ b/api/services/telephony/providers/ari/strategies.py @@ -261,6 +261,7 @@ class ARIHangupStrategy(HangupStrategy): (Redis ``ari:channel:{id}`` -> run_id -> ``initial_context``). Best-effort: never blocks dograh's own hangup if the upstream call fails. """ + redis = None try: import redis.asyncio as aioredis @@ -319,3 +320,9 @@ class ARIHangupStrategy(HangupStrategy): ) except Exception as e: logger.error(f"[ARI Hangup] external PBX terminate check failed: {e}") + finally: + if redis is not None: + try: + await redis.aclose() + except Exception as e: + logger.warning(f"[ARI Hangup] failed to close Redis client: {e}") diff --git a/api/tests/telephony/providers/ari/test_external_pbx.py b/api/tests/telephony/providers/ari/test_external_pbx.py index 2e08d7ac..019b0a34 100644 --- a/api/tests/telephony/providers/ari/test_external_pbx.py +++ b/api/tests/telephony/providers/ari/test_external_pbx.py @@ -204,3 +204,15 @@ async def test_hangup_strategy_updates_lead_before_customer_leg(monkeypatch): run.initial_context["external_pbx_call"], {"address3": "yes"} ) adapter.hangup.assert_awaited_once_with(run.initial_context["external_pbx_call"]) + redis.aclose.assert_awaited_once_with() + + +@pytest.mark.asyncio +async def test_hangup_strategy_closes_redis_when_channel_has_no_run(monkeypatch): + redis = AsyncMock() + redis.get.return_value = None + monkeypatch.setattr(aioredis, "from_url", lambda *args, **kwargs: redis) + + await ARIHangupStrategy()._terminate_external_pbx_if_any("missing-channel") + + redis.aclose.assert_awaited_once_with()