diff --git a/src/bridge/transport.py b/src/bridge/transport.py index a2127b0..3d2e03d 100644 --- a/src/bridge/transport.py +++ b/src/bridge/transport.py @@ -358,8 +358,15 @@ class BridgeTransport(QuicConnectionProtocol): hello = build_hello(self._settings.service, self._capabilities, self._settings.token, self._settings.max_concurrent) self._send_frame(sid, hello, end=False) - greeting = await asyncio.wait_for(stream.read_frame(), - timeout=GREETING_TIMEOUT_SECS) + # `wait_for` DEĞİL `asyncio.timeout`: 3.11'in `wait_for`ı, dışarıdan + # gelen iptal (CancelledError) tam içteki okuma BİTTİĞİ anda gelirse + # iptali YUTAR ve sonucu döndürür (`except CancelledError: if + # fut.done(): return fut.result()`, bkz. 3.11 `asyncio/tasks.py`; + # 3.12+ `wait_for`u bu dalı taşımaz). Yutulan iptal, iptal edilmiş + # sanılan `run_forever`ı `wait_closed()`ta asılı bırakır: bağlantı + # kapanmaz, görev hiç bitmez. `asyncio.timeout` iptali aynen geçirir. + async with asyncio.timeout(GREETING_TIMEOUT_SECS): + greeting = await stream.read_frame() self.worker_id = parse_greeting(greeting) # Blob okuma yolu BURADA takılır: `rag.index` executor thread'inde koşar # (PING'ler durmasın) ve o thread'den QUIC akışı açmak bu köprüyü @@ -387,7 +394,11 @@ class BridgeTransport(QuicConnectionProtocol): self._streams[sid] = stream try: self._send_frame(sid, request, end=True) - header = await asyncio.wait_for(stream.read_frame(), timeout=header_timeout) + # `asyncio.timeout`, `wait_for` DEĞİL — gerekçe `register()`te: + # 3.11'in `wait_for`ı, içteki okuma bittiği anda gelen iptali + # yutar; bu yol da aynı sınıftandır. + async with asyncio.timeout(header_timeout): + header = await stream.read_frame() if not isinstance(header, dict): raise BlobReadRefused("bad_header", "blob başlığı nesne değil") if header.get("status") != "ok": @@ -397,8 +408,8 @@ class BridgeTransport(QuicConnectionProtocol): if size < 0 or size > max_bytes: raise BlobReadRefused( "too_large", f"blob {size} bayt: sınır {max_bytes}") - body = await asyncio.wait_for(stream.read_exact(size), - timeout=body_timeout) + async with asyncio.timeout(body_timeout): + body = await stream.read_exact(size) return header, body except BlobReadRefused: raise diff --git a/src/service/http_app.py b/src/service/http_app.py index e712962..0baa712 100644 --- a/src/service/http_app.py +++ b/src/service/http_app.py @@ -297,7 +297,12 @@ def create_app(service, *, service_token: str | None = None, rid = request.headers.get("X-Request-Id") or uuid.uuid4().hex[:16] try: if deadline > 0: - resp = await asyncio.wait_for(call_next(request), timeout=deadline) + # `wait_for` DEĞİL `asyncio.timeout` — gerekçesi köprüde + # (`transport.register`): 3.11'in `wait_for`ı, içteki çağrı + # bittiği anda gelen DIŞ iptali (istemci koptu / sunucu + # kapanıyor) yutar. Burada da iptal aynen geçmeli. + async with asyncio.timeout(deadline): + resp = await call_next(request) else: resp = await call_next(request) except (asyncio.TimeoutError, TimeoutError): diff --git a/tests/unit/test_bridge_transport.py b/tests/unit/test_bridge_transport.py index 787c313..2f367d2 100644 --- a/tests/unit/test_bridge_transport.py +++ b/tests/unit/test_bridge_transport.py @@ -81,6 +81,35 @@ def _ayarlar(bridge: FakeBridge, *, port: int | None = None, **ustune) -> Settin return Settings(**deger) +#: İptalden sonra görevin bitmesi için üst sınır. Taşıma iptali YUTARSA +#: görev `wait_closed()`ta asılı kalır (3.11 `asyncio.wait_for`ının iptal +#: yutması — `transport.register`); o durumda test ASILI KALMAZ, ADIYLA düşer. +KOPRU_IPTAL_TIMEOUT_SECS = 15.0 + + +async def _iptal_et_ve_bekle(gorev) -> None: + """`cancel()` + iptalin GERÇEKTEN işlemesini SINIRLI bekle (YOKLAMA). + + Sınırsız `await gorev` bir kilitlenmeyi CI'da yalnız faulthandler'ın + zaman aşımıyla görünür kılar. Doğrudan/`asyncio.timeout` içinde beklemek + de YETMEZ: zaman aşımının iptali görevi de çözer, yani YUTULAN iptal + görünmez kalır (ölçüldü: 15 sn bekleyip test yine PASS etti). Bu yüzden + görev YOKLANIR; süre dolarsa adıyla bir `AssertionError` çıkar — CI'ın + zaman aşımı katmanı yerinde kalır, bu yalnız iptalin işlediğini ölçer.""" + gorev.cancel() + loop = asyncio.get_running_loop() + deadline = loop.time() + KOPRU_IPTAL_TIMEOUT_SECS + while not gorev.done(): + if loop.time() >= deadline: + raise AssertionError("köprü görevi iptalden sonra bitmedi: " + f"taşıma iptali YUTMUŞ (done={gorev.done()})") + await asyncio.sleep(0.05) + if not gorev.cancelled(): + # İptal dışındaki sonuç GİZLENMEZ: teardown'da bir hata varsa burada + # yükselir (eski `await gorev` yalnız CancelledError'ı yutardı). + gorev.result() + + @contextlib.asynccontextmanager async def _calisan_kopru(bridge: FakeBridge, servis: _SahteServis, *, indexer=None, **ustune): @@ -90,9 +119,7 @@ async def _calisan_kopru(bridge: FakeBridge, servis: _SahteServis, *, try: yield gorev finally: - gorev.cancel() - with contextlib.suppress(asyncio.CancelledError): - await gorev + await _iptal_et_ve_bekle(gorev) assert gorev.done() @@ -375,9 +402,7 @@ class KopruTasimaTestleri(unittest.TestCase): await asyncio.sleep(0.3) self.assertFalse(ilk.done(), "erişilemeyen backend'de süreç ÇIKTI") finally: - ilk.cancel() - with contextlib.suppress(asyncio.CancelledError): - await ilk + await _iptal_et_ve_bekle(ilk) # (b) Sertifika çekilebiliyor, QUIC YOK (gerçek bir yeniden # başlatmanın ilk anı) → bekle, backend gelince katıl. @@ -397,9 +422,7 @@ class KopruTasimaTestleri(unittest.TestCase): {"message": "x", "asker_role": "student"}) self.assertEqual(cevap["status"], "ok") finally: - gorev.cancel() - with contextlib.suppress(asyncio.CancelledError): - await gorev + await _iptal_et_ve_bekle(gorev) await bridge.stop() asyncio.run(senaryo()) @@ -427,9 +450,7 @@ class KopruTasimaTestleri(unittest.TestCase): "yanlış parmak iziyle BAĞLANDI") self.assertFalse(gorev.done(), "pin uyuşmazlığında süreç ÇIKTI") finally: - gorev.cancel() - with contextlib.suppress(asyncio.CancelledError): - await gorev + await _iptal_et_ve_bekle(gorev) async with _calisan_kopru( bridge, _SahteServis(), tls_fingerprint=bridge.certificate.fingerprint):