🧵 3.11'in wait_for'u iptali yutuyordu — köprü kapanmıyordu
Browse files at this commita4b387405f13c3f1c36f75e914bbf382480aedd8Parent: d70189f- fix
- 🧵 3.11'in wait_for'u iptali yutuyordu — köprü kapanmıyordu
3 changed files +55 -18
- src/bridge/transport.py +16 -5
- src/service/http_app.py +6 -1
- tests/unit/test_bridge_transport.py +33 -12
src/bridge/transport.pymodified+16 -5
| @@ -358,8 +358,15 @@ class BridgeTransport(QuicConnectionProtocol): | |||
| 358 | hello = build_hello(self._settings.service, self._capabilities, | 358 | hello = build_hello(self._settings.service, self._capabilities, |
| 359 | self._settings.token, self._settings.max_concurrent) | 359 | self._settings.token, self._settings.max_concurrent) |
| 360 | self._send_frame(sid, hello, end=False) | 360 | self._send_frame(sid, hello, end=False) |
| 361 | - greeting = await asyncio.wait_for(stream.read_frame(), | 361 | + # `wait_for` DEĞİL `asyncio.timeout`: 3.11'in `wait_for`ı, dışarıdan |
| 362 | - timeout=GREETING_TIMEOUT_SECS) | 362 | + # gelen iptal (CancelledError) tam içteki okuma BİTTİĞİ anda gelirse |
| 363 | + # iptali YUTAR ve sonucu döndürür (`except CancelledError: if | ||
| 364 | + # fut.done(): return fut.result()`, bkz. 3.11 `asyncio/tasks.py`; | ||
| 365 | + # 3.12+ `wait_for`u bu dalı taşımaz). Yutulan iptal, iptal edilmiş | ||
| 366 | + # sanılan `run_forever`ı `wait_closed()`ta asılı bırakır: bağlantı | ||
| 367 | + # kapanmaz, görev hiç bitmez. `asyncio.timeout` iptali aynen geçirir. | ||
| 368 | + async with asyncio.timeout(GREETING_TIMEOUT_SECS): | ||
| 369 | + greeting = await stream.read_frame() | ||
| 363 | self.worker_id = parse_greeting(greeting) | 370 | self.worker_id = parse_greeting(greeting) |
| 364 | # Blob okuma yolu BURADA takılır: `rag.index` executor thread'inde koşar | 371 | # Blob okuma yolu BURADA takılır: `rag.index` executor thread'inde koşar |
| 365 | # (PING'ler durmasın) ve o thread'den QUIC akışı açmak bu köprüyü | 372 | # (PING'ler durmasın) ve o thread'den QUIC akışı açmak bu köprüyü |
| @@ -387,7 +394,11 @@ class BridgeTransport(QuicConnectionProtocol): | |||
| 387 | self._streams[sid] = stream | 394 | self._streams[sid] = stream |
| 388 | try: | 395 | try: |
| 389 | self._send_frame(sid, request, end=True) | 396 | self._send_frame(sid, request, end=True) |
| 390 | - header = await asyncio.wait_for(stream.read_frame(), timeout=header_timeout) | 397 | + # `asyncio.timeout`, `wait_for` DEĞİL — gerekçe `register()`te: |
| 398 | + # 3.11'in `wait_for`ı, içteki okuma bittiği anda gelen iptali | ||
| 399 | + # yutar; bu yol da aynı sınıftandır. | ||
| 400 | + async with asyncio.timeout(header_timeout): | ||
| 401 | + header = await stream.read_frame() | ||
| 391 | if not isinstance(header, dict): | 402 | if not isinstance(header, dict): |
| 392 | raise BlobReadRefused("bad_header", "blob başlığı nesne değil") | 403 | raise BlobReadRefused("bad_header", "blob başlığı nesne değil") |
| 393 | if header.get("status") != "ok": | 404 | if header.get("status") != "ok": |
| @@ -397,8 +408,8 @@ class BridgeTransport(QuicConnectionProtocol): | |||
| 397 | if size < 0 or size > max_bytes: | 408 | if size < 0 or size > max_bytes: |
| 398 | raise BlobReadRefused( | 409 | raise BlobReadRefused( |
| 399 | "too_large", f"blob {size} bayt: sınır {max_bytes}") | 410 | "too_large", f"blob {size} bayt: sınır {max_bytes}") |
| 400 | - body = await asyncio.wait_for(stream.read_exact(size), | 411 | + async with asyncio.timeout(body_timeout): |
| 401 | - timeout=body_timeout) | 412 | + body = await stream.read_exact(size) |
| 402 | return header, body | 413 | return header, body |
| 403 | except BlobReadRefused: | 414 | except BlobReadRefused: |
| 404 | raise | 415 | raise |
src/service/http_app.pymodified+6 -1
| @@ -297,7 +297,12 @@ def create_app(service, *, service_token: str | None = None, | |||
| 297 | rid = request.headers.get("X-Request-Id") or uuid.uuid4().hex[:16] | 297 | rid = request.headers.get("X-Request-Id") or uuid.uuid4().hex[:16] |
| 298 | try: | 298 | try: |
| 299 | if deadline > 0: | 299 | if deadline > 0: |
| 300 | - resp = await asyncio.wait_for(call_next(request), timeout=deadline) | 300 | + # `wait_for` DEĞİL `asyncio.timeout` — gerekçesi köprüde |
| 301 | + # (`transport.register`): 3.11'in `wait_for`ı, içteki çağrı | ||
| 302 | + # bittiği anda gelen DIŞ iptali (istemci koptu / sunucu | ||
| 303 | + # kapanıyor) yutar. Burada da iptal aynen geçmeli. | ||
| 304 | + async with asyncio.timeout(deadline): | ||
| 305 | + resp = await call_next(request) | ||
| 301 | else: | 306 | else: |
| 302 | resp = await call_next(request) | 307 | resp = await call_next(request) |
| 303 | except (asyncio.TimeoutError, TimeoutError): | 308 | except (asyncio.TimeoutError, TimeoutError): |
tests/unit/test_bridge_transport.pymodified+33 -12
| @@ -81,6 +81,35 @@ def _ayarlar(bridge: FakeBridge, *, port: int | None = None, **ustune) -> Settin | |||
| 81 | return Settings(**deger) | 81 | return Settings(**deger) |
| 82 | 82 | ||
| 83 | 83 | ||
| 84 | +#: İptalden sonra görevin bitmesi için üst sınır. Taşıma iptali YUTARSA | ||
| 85 | +#: görev `wait_closed()`ta asılı kalır (3.11 `asyncio.wait_for`ının iptal | ||
| 86 | +#: yutması — `transport.register`); o durumda test ASILI KALMAZ, ADIYLA düşer. | ||
| 87 | +KOPRU_IPTAL_TIMEOUT_SECS = 15.0 | ||
| 88 | + | ||
| 89 | + | ||
| 90 | +async def _iptal_et_ve_bekle(gorev) -> None: | ||
| 91 | + """`cancel()` + iptalin GERÇEKTEN işlemesini SINIRLI bekle (YOKLAMA). | ||
| 92 | + | ||
| 93 | + Sınırsız `await gorev` bir kilitlenmeyi CI'da yalnız faulthandler'ın | ||
| 94 | + zaman aşımıyla görünür kılar. Doğrudan/`asyncio.timeout` içinde beklemek | ||
| 95 | + de YETMEZ: zaman aşımının iptali görevi de çözer, yani YUTULAN iptal | ||
| 96 | + görünmez kalır (ölçüldü: 15 sn bekleyip test yine PASS etti). Bu yüzden | ||
| 97 | + görev YOKLANIR; süre dolarsa adıyla bir `AssertionError` çıkar — CI'ın | ||
| 98 | + zaman aşımı katmanı yerinde kalır, bu yalnız iptalin işlediğini ölçer.""" | ||
| 99 | + gorev.cancel() | ||
| 100 | + loop = asyncio.get_running_loop() | ||
| 101 | + deadline = loop.time() + KOPRU_IPTAL_TIMEOUT_SECS | ||
| 102 | + while not gorev.done(): | ||
| 103 | + if loop.time() >= deadline: | ||
| 104 | + raise AssertionError("köprü görevi iptalden sonra bitmedi: " | ||
| 105 | + f"taşıma iptali YUTMUŞ (done={gorev.done()})") | ||
| 106 | + await asyncio.sleep(0.05) | ||
| 107 | + if not gorev.cancelled(): | ||
| 108 | + # İptal dışındaki sonuç GİZLENMEZ: teardown'da bir hata varsa burada | ||
| 109 | + # yükselir (eski `await gorev` yalnız CancelledError'ı yutardı). | ||
| 110 | + gorev.result() | ||
| 111 | + | ||
| 112 | + | ||
| 84 | @contextlib.asynccontextmanager | 113 | @contextlib.asynccontextmanager |
| 85 | async def _calisan_kopru(bridge: FakeBridge, servis: _SahteServis, *, | 114 | async def _calisan_kopru(bridge: FakeBridge, servis: _SahteServis, *, |
| 86 | indexer=None, **ustune): | 115 | indexer=None, **ustune): |
| @@ -90,9 +119,7 @@ async def _calisan_kopru(bridge: FakeBridge, servis: _SahteServis, *, | |||
| 90 | try: | 119 | try: |
| 91 | yield gorev | 120 | yield gorev |
| 92 | finally: | 121 | finally: |
| 93 | - gorev.cancel() | 122 | + await _iptal_et_ve_bekle(gorev) |
| 94 | - with contextlib.suppress(asyncio.CancelledError): | ||
| 95 | - await gorev | ||
| 96 | assert gorev.done() | 123 | assert gorev.done() |
| 97 | 124 | ||
| 98 | 125 | ||
| @@ -375,9 +402,7 @@ class KopruTasimaTestleri(unittest.TestCase): | |||
| 375 | await asyncio.sleep(0.3) | 402 | await asyncio.sleep(0.3) |
| 376 | self.assertFalse(ilk.done(), "erişilemeyen backend'de süreç ÇIKTI") | 403 | self.assertFalse(ilk.done(), "erişilemeyen backend'de süreç ÇIKTI") |
| 377 | finally: | 404 | finally: |
| 378 | - ilk.cancel() | 405 | + await _iptal_et_ve_bekle(ilk) |
| 379 | - with contextlib.suppress(asyncio.CancelledError): | ||
| 380 | - await ilk | ||
| 381 | 406 | ||
| 382 | # (b) Sertifika çekilebiliyor, QUIC YOK (gerçek bir yeniden | 407 | # (b) Sertifika çekilebiliyor, QUIC YOK (gerçek bir yeniden |
| 383 | # başlatmanın ilk anı) → bekle, backend gelince katıl. | 408 | # başlatmanın ilk anı) → bekle, backend gelince katıl. |
| @@ -397,9 +422,7 @@ class KopruTasimaTestleri(unittest.TestCase): | |||
| 397 | {"message": "x", "asker_role": "student"}) | 422 | {"message": "x", "asker_role": "student"}) |
| 398 | self.assertEqual(cevap["status"], "ok") | 423 | self.assertEqual(cevap["status"], "ok") |
| 399 | finally: | 424 | finally: |
| 400 | - gorev.cancel() | 425 | + await _iptal_et_ve_bekle(gorev) |
| 401 | - with contextlib.suppress(asyncio.CancelledError): | ||
| 402 | - await gorev | ||
| 403 | await bridge.stop() | 426 | await bridge.stop() |
| 404 | 427 | ||
| 405 | asyncio.run(senaryo()) | 428 | asyncio.run(senaryo()) |
| @@ -427,9 +450,7 @@ class KopruTasimaTestleri(unittest.TestCase): | |||
| 427 | "yanlış parmak iziyle BAĞLANDI") | 450 | "yanlış parmak iziyle BAĞLANDI") |
| 428 | self.assertFalse(gorev.done(), "pin uyuşmazlığında süreç ÇIKTI") | 451 | self.assertFalse(gorev.done(), "pin uyuşmazlığında süreç ÇIKTI") |
| 429 | finally: | 452 | finally: |
| 430 | - gorev.cancel() | 453 | + await _iptal_et_ve_bekle(gorev) |
| 431 | - with contextlib.suppress(asyncio.CancelledError): | ||
| 432 | - await gorev | ||
| 433 | async with _calisan_kopru( | 454 | async with _calisan_kopru( |
| 434 | bridge, _SahteServis(), | 455 | bridge, _SahteServis(), |
| 435 | tls_fingerprint=bridge.certificate.fingerprint): | 456 | tls_fingerprint=bridge.certificate.fingerprint): |