fix(tests): the rule-history fixture must not delete a book the embedder is still writing (#3241)
CI & Build / Python lint (push) Successful in 5s
CI & Build / Plugin hooks (push) Successful in 13s
CI & Build / integration (push) Successful in 31s
CI & Build / TypeScript typecheck (push) Successful in 34s
CI & Build / Python tests (push) Successful in 1m17s
CI & Build / Build & push image (push) Successful in 16s

The integration lane failed on a deadlock, not an assertion — 103 passed, and
the one error was in teardown: `DELETE FROM rulebooks` blocked against
another process holding a lock on a rule row.

`update_rule` fires a detached asyncio.create_task(upsert_rule_embedding(...))
that opens its OWN connection and UPDATEs the rule it just saved. The
teardown's rulebook delete cascade-locks that same row, and Postgres resolves
the cycle by killing one of them. The sibling test_integration_rule_verification
never hit this because it calls update_rule but never deletes its rulebook.

Cleanup moves to setup, which runs on a fresh loop after the previous test's
loop has closed and cancelled whatever it left in flight. That also keeps the
#3240 constraint intact: no database call after a yield.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-29 23:28:03 -04:00
co-authored by Claude Opus 5
parent 6fa66f202b
commit 255c43a8fe
+8 -11
View File
@@ -35,10 +35,13 @@ OWNER_USERNAME = "rule_history_owner"
async def constraint(): async def constraint():
"""One rule carrying a check, with no history yet. """One rule carrying a check, with no history yet.
Cleanup is in this fixture's own teardown, never in an autouse one: CLEANED UP AT SETUP, NOT TEARDOWN, and that is forced. `update_rule` fires
`_dispose_engine` arrives through usefixtures, so it tears down BEFORE an a detached `asyncio.create_task(upsert_rule_embedding(...))` that opens
autouse fixture would, and a database call after that point orphans a its own connection and UPDATEs the rule row. A teardown that deleted the
pooled connection and breaks the NEXT test to touch Postgres (#3240). rulebook would race it: the delete cascade-locks the rule the embedding
task is writing, and Postgres kills one of them with a deadlock. Purging
at setup instead runs on a fresh loop, after the previous test's loop
closed and cancelled whatever it left in flight.
""" """
async with async_session() as s: async with async_session() as s:
owner = await ensure_user(s, OWNER_USERNAME) owner = await ensure_user(s, OWNER_USERNAME)
@@ -60,13 +63,7 @@ async def constraint():
verify_with="read the workflow's shell setting", verify_with="read the workflow's shell setting",
) )
yield {"uid": uid, "rule_id": rule.id} return {"uid": uid, "rule_id": rule.id}
async with async_session() as s:
row = await s.get(Rulebook, book.id)
if row is not None:
await s.delete(row)
await s.commit()
async def _versions(rule_id: int) -> list[RuleVersion]: async def _versions(rule_id: int) -> list[RuleVersion]: