|
1 | 1 | """Instrumented listener entrypoint for durable finalization scenarios. |
2 | 2 |
|
3 | | -The production FastAPI application and task construction remain real. Only the |
4 | | -Cloud Tasks transport client is replaced by a strict loopback boundary. |
| 3 | +The production FastAPI application and task construction remain real. Cloud |
| 4 | +Tasks is the only external transport replaced; an opt-in gate merely |
| 5 | +coordinates real route lookups for a deterministic concurrency scenario. |
5 | 6 | """ |
6 | 7 |
|
| 8 | +import os |
| 9 | +from threading import Barrier, BrokenBarrierError, Lock |
| 10 | + |
7 | 11 | from testing.listen_pusher_stack.cloud_tasks import install_loopback_tasks_client |
8 | 12 |
|
9 | 13 | install_loopback_tasks_client() |
10 | 14 |
|
11 | 15 | from main import app # noqa: E402 |
| 16 | + |
| 17 | + |
| 18 | +def _install_rest_finalization_race_barrier() -> None: |
| 19 | + """Force a bounded stale-read race before the real finalization transaction. |
| 20 | +
|
| 21 | + The live gauntlet sends concurrent public REST calls, but scheduler timing |
| 22 | + alone cannot guarantee every handler reads ``in_progress`` before the |
| 23 | + winning Firestore transaction changes it to ``processing``. This opt-in |
| 24 | + harness seam gates only the first N target reads after they have used the |
| 25 | + production lookup. The route, auth, lifecycle transaction, and Cloud Tasks |
| 26 | + call therefore stay real while the intended named-task race is repeatable. |
| 27 | + """ |
| 28 | + uid = os.getenv('OMI_STACK_FINALIZATION_RACE_UID', '') |
| 29 | + conversation_id = os.getenv('OMI_STACK_FINALIZATION_RACE_CONVERSATION_ID', '') |
| 30 | + raw_parties = os.getenv('OMI_STACK_FINALIZATION_RACE_PARTIES', '') |
| 31 | + if not any((uid, conversation_id, raw_parties)): |
| 32 | + return |
| 33 | + if not all((uid, conversation_id, raw_parties)): |
| 34 | + raise RuntimeError('REST finalization race barrier requires uid, conversation ID, and party count') |
| 35 | + try: |
| 36 | + parties = int(raw_parties) |
| 37 | + except ValueError as error: |
| 38 | + raise RuntimeError('REST finalization race barrier party count must be an integer') from error |
| 39 | + if parties < 2: |
| 40 | + raise RuntimeError('REST finalization race barrier needs at least two parties') |
| 41 | + |
| 42 | + from routers import conversations as conversations_router |
| 43 | + |
| 44 | + original_lookup = conversations_router._get_valid_conversation_by_id |
| 45 | + barrier = Barrier(parties) |
| 46 | + lock = Lock() |
| 47 | + remaining = parties |
| 48 | + |
| 49 | + def lookup_with_race_gate(request_uid: str, request_conversation_id: str): |
| 50 | + nonlocal remaining |
| 51 | + snapshot = original_lookup(request_uid, request_conversation_id) |
| 52 | + if request_uid != uid or request_conversation_id != conversation_id: |
| 53 | + return snapshot |
| 54 | + with lock: |
| 55 | + should_wait = remaining > 0 |
| 56 | + if should_wait: |
| 57 | + remaining -= 1 |
| 58 | + if not should_wait: |
| 59 | + return snapshot |
| 60 | + try: |
| 61 | + barrier.wait(timeout=10.0) |
| 62 | + except BrokenBarrierError as error: |
| 63 | + raise RuntimeError('REST finalization race barrier did not receive every request') from error |
| 64 | + return snapshot |
| 65 | + |
| 66 | + conversations_router._get_valid_conversation_by_id = lookup_with_race_gate |
| 67 | + |
| 68 | + |
| 69 | +_install_rest_finalization_race_barrier() |
0 commit comments