From 4d33ef3b5baef601e294b4ca95997a64e856b190 Mon Sep 17 00:00:00 2001 From: suguanYang Date: Fri, 4 Dec 2026 03:53:55 -0700 Subject: [PATCH] Time only the cancellation in the indexing execution-timeout test The test bounded the whole IndexingWorker.run() at budget + 0.32 s. Most of that run is blocking psycopg2 work before and after the native wait, which the cooperative gevent timer cannot interrupt and is not meant to. Locally the run takes 0.13-0.21 s; on CI runners it reached 0.55-0.72 s on unrelated branches, with the whole stall landing before the failure transition, so the bound failed without saying anything about cancellation. Record when the blocked native wait starts and ends, and assert that it ends within 0.1 s of the deadline (or of entering the wait, when the SQL before it already used the budget). Settlement after cancellation must finish within its own settlement budget, and the native call must not be replayed. Assertion messages report each phase so a future failure shows where the time went. With the deadline's Timeout disabled the test still fails: the native wait ends at +1.63 s. Co-Authored-By: Claude Opus 5.5 --- .../test_indexing_execution_contract.py | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/apps/api/tests/contract/test_indexing_execution_contract.py b/apps/api/tests/contract/test_indexing_execution_contract.py index f18896e4e..e0e57d9ed 100644 --- a/apps/api/tests/contract/test_indexing_execution_contract.py +++ b/apps/api/tests/contract/test_indexing_execution_contract.py @@ -211,24 +211,43 @@ def test_execution_timeout_stops_native_wait_and_returns_without_replay( # check measures cancellation of the blocked native wait itself. monkeypatch.setattr(OpenSearchRuntime, "resolve", lambda endpoint: SimpleNamespace(client=None, release=lambda: None)) + nativeWaitSeconds: float = executionBudget * 20 + nativeWait: dict[str, float] = {} + def blockedNative(self: OpenSearchLexicalIndex, *, isExact: bool, timeoutSeconds: float) -> NativeLifecycleSnapshot: calls.append("native") - gevent.sleep(executionBudget * 20) + nativeWait["startedAt"] = time.monotonic() + try: + gevent.sleep(nativeWaitSeconds) + finally: + nativeWait["endedAt"] = time.monotonic() raise LexicalIndexError("Native wait reached its deliberately long end") monkeypatch.setattr(settings, "KNOWHERE_INDEXING_TIMEOUT_SECONDS", executionBudget) monkeypatch.setattr(OpenSearchLexicalIndex, "getLifecycleSnapshot", blockedNative) # The failure transition's audit metadata goes through make_json_safe, which imports numpy and - # pandas on first use. Load them here so the timed region measures cancellation, not a cold import. + # pandas on first use. Load them here so a cold import does not eat into the settlement bound. from shared.utils.json_utils import make_json_safe make_json_safe({}) startedAt: float = time.monotonic() worker: IndexingWorker = IndexingWorker(sessionFactory=sessionmaker(engine, expire_on_commit=False)) assert worker.run(jobId) == "failed" - assert time.monotonic() - startedAt < executionBudget + 0.32 + ( - settings.KNOWHERE_INDEXING_SETTLEMENT_TIMEOUT_SECONDS if executionBudget > 0.08 else 0) + returnedAt: float = time.monotonic() assert calls == ["native"] + nativeStartedAt: float = nativeWait["startedAt"] + nativeEndedAt: float = nativeWait["endedAt"] + phases: str = (f"budget {executionBudget:.3f}s; native wait started +{nativeStartedAt - startedAt:.3f}s, " + f"ended +{nativeEndedAt - startedAt:.3f}s; run returned +{returnedAt - startedAt:.3f}s") + # Time only the cancellation. The SQL before and after the native wait is blocking psycopg2 + # work that the cooperative timer cannot interrupt. It takes about 0.15 s locally, yet whole + # runs reached 0.55-0.72 s on CI runners, so a bound on the whole run flaked without saying + # anything about cancellation. If that SQL already used up the budget, the wait must end as + # soon as it starts. + cancellationToleranceSeconds: float = 0.1 + assert nativeEndedAt - max(nativeStartedAt, startedAt + executionBudget) < cancellationToleranceSeconds, phases + # After cancellation, failure settlement runs under its own budget and does not replay the wait. + assert returnedAt - nativeEndedAt < settings.KNOWHERE_INDEXING_SETTLEMENT_TIMEOUT_SECONDS, phases with Session(engine) as session: attempt: IndexingAttempt = session.scalars(select(IndexingAttempt)).one() job: Job = session.scalars(select(Job)).one()