Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
fix(subscription): stop leaking Event.wait tasks from poll loops
Adryan Eka Vandra548c69a1f9f8
diff --git a/headroom/subscription/copilot_quota.py b/headroom/subscription/copilot_quota.pyindex 782431f0a..73d433f8f 100644--- a/headroom/subscription/copilot_quota.py+++ b/headroom/subscription/copilot_quota.py@@ -274,8 +274,12 @@ class _CopilotQuotaTracker(QuotaTracker):except Exception as exc:logger.warning("Copilot quota poll error: %s", exc)try:+ # NOTE: do NOT wrap in asyncio.shield() — shield prevents the+ # inner Event.wait() from being cancelled when wait_for times+ # out, leaking one Task per poll interval. See the matching+ # note in headroom/subscription/tracker.py:_poll_loop.await asyncio.wait_for(- asyncio.shield(self._stop_event.wait()),+ self._stop_event.wait(),timeout=self._poll_interval_s,)break # stop event was setdiff --git a/headroom/subscription/tracker.py b/headroom/subscription/tracker.pyindex ce0bf9237..602cee0dc 100644--- a/headroom/subscription/tracker.py+++ b/headroom/subscription/tracker.py@@ -219,8 +219,13 @@ class SubscriptionTracker(QuotaTracker):except Exception as exc:logger.warning("Subscription tracker poll error: %s", exc)try:+ # NOTE: do NOT wrap in asyncio.shield() — shield prevents the+ # inner Event.wait() from being cancelled when wait_for times+ # out, leaking one Task per poll interval. Over hours the+ # accumulated idle waiters bog down the event loop scheduler+ # (observed as the "aged proxy degradation" in 2026-04-17).await asyncio.wait_for(- asyncio.shield(self._stop_event.wait()),+ self._stop_event.wait(),timeout=self._poll_interval_s,)break # stop event was setdiff --git a/tests/test_copilot_quota.py b/tests/test_copilot_quota.pyindex 1b58b309e..7ec51ab8a 100644--- a/tests/test_copilot_quota.py+++ b/tests/test_copilot_quota.py@@ -278,3 +278,54 @@ class TestCopilotQuotaSnapshot:assert d["copilot_plan"] == "business"assert "chat" in d["categories"]assert d["categories"]["chat"]["entitlement"] == 50+++# ---------------------------------------------------------------------------+# Poll-loop task-leak regression+# ---------------------------------------------------------------------------+++class TestCopilotQuotaPollLoopLeak:+ @pytest.mark.asyncio+ async def test_poll_loop_does_not_leak_event_wait_tasks(self, monkeypatch):+ """Regression for the ``asyncio.shield(event.wait())`` pattern.++ Matches the equivalent guard in ``tests/test_subscription_tracker.py``:+ every poll interval the loop previously leaked one Event.wait+ waiter because ``asyncio.shield`` prevented ``wait_for`` from+ cancelling the inner wait on timeout.+ """+ import asyncio++ from headroom.subscription.copilot_quota import _CopilotQuotaTracker++ # No token configured → _maybe_poll returns immediately each cycle.+ for var in ("GITHUB_COPILOT_GITHUB_TOKEN", "GITHUB_TOKEN"):+ monkeypatch.delenv(var, raising=False)++ tracker = _CopilotQuotaTracker(poll_interval_s=0.05)++ def _count_event_wait() -> int:+ return sum(+ 1+ for t in asyncio.all_tasks()+ if (t.get_coro().__qualname__ if t.get_coro() else "") == "Event.wait"+ )++ baseline = _count_event_wait()+ await tracker.start()+ try:+ await asyncio.sleep(0.3) # ~6 poll cycles+ peak = _count_event_wait()+ finally:+ await tracker.stop()++ await asyncio.sleep(0.05)+ residual = _count_event_wait()++ assert peak - baseline <= 1, (+ f"CopilotQuotaTracker leaked Event.wait: baseline={baseline} peak={peak}"+ )+ assert residual <= baseline, (+ f"CopilotQuotaTracker left residual Event.wait: baseline={baseline} residual={residual}"+ )diff --git a/tests/test_subscription_tracker.py b/tests/test_subscription_tracker.pyindex 72a5a2c8d..be58eb9b7 100644--- a/tests/test_subscription_tracker.py+++ b/tests/test_subscription_tracker.py@@ -339,6 +339,63 @@ class TestSubscriptionTrackerPollLoop:mock_client.fetch.assert_not_called()+ @pytest.mark.asyncio+ async def test_poll_loop_does_not_leak_event_wait_tasks(self, tmp_path):+ """The poll loop must not accumulate idle Event.wait waiters.++ Regression for the ``asyncio.shield(event.wait())`` pattern:+ ``shield`` prevents the inner wait from being cancelled when+ ``wait_for`` times out, leaking one Task per poll interval.+ Observed as the "aged proxy degradation" in 2026-04-17 runtime+ analysis — over hours the accumulated idle waiters bog down the+ event-loop scheduler.+ """+ import asyncio++ from headroom.subscription.tracker import SubscriptionTracker++ mock_client = MagicMock()+ mock_client.fetch = AsyncMock(return_value=None)++ tracker = SubscriptionTracker(+ poll_interval_s=0.05, # short so many iterations fit in the test window+ active_window_s=60,+ persist_path=tmp_path / "state.json",+ client=mock_client,+ )++ def _count_event_wait_tasks() -> int:+ return sum(+ 1+ for t in asyncio.all_tasks()+ if (t.get_coro().__qualname__ if t.get_coro() else "") == "Event.wait"+ )++ baseline = _count_event_wait_tasks()+ await tracker.start()+ try:+ # Let the loop run through ~6 iterations. Each iteration would+ # previously leak one Event.wait waiter.+ await asyncio.sleep(0.3)+ peak = _count_event_wait_tasks()+ finally:+ await tracker.stop()++ # After stop the loop task has exited; anything above baseline + 1+ # (the single live wait that was in-flight before stop set the event)+ # indicates leaked waiters.+ await asyncio.sleep(0.05) # let stop()'s awaited tasks settle+ residual = _count_event_wait_tasks()++ # Peak growth during polling should be at most one (the one currently+ # awaiting inside the loop); anything more is a leak.+ assert peak - baseline <= 1, (+ f"poll loop leaked Event.wait waiters: baseline={baseline} peak={peak}"+ )+ assert residual <= baseline, (+ f"poll loop left residual Event.wait waiters: baseline={baseline} residual={residual}"+ )+# ---------------------------------------------------------------------------# Anomaly detection