Stop a stalled Prefect test server from dropping a `@flow(retries=1)` retry
Why we make these changes
Prefect @flow(retries=1) tests sometimes end Failed without their retry, 62 s after the first attempt raises, on test durable-exec on 3.11 (locked):
- #13707:
test_flow_retry_preserves_incoming_conversation_identity[explicit_conversation] - #13767:
test_prefect_concurrent_workspace_runs_keep_their_environments_on_retry - unfiled,
mainpush:test_default_id_static_metadata_precedence_and_factory_lifecycle
Root cause. The Prefect client's default request timeout is 60 s. A state change the test server has read can wait that long for the SQLite write lock (PRAGMA busy_timeout = 60000), and longer on a stalled runner. The server then commits its reply to the Failed proposal, AwaitingRetry, after the client has given up, and the client's ReadTimeout retry resends the same Failed 2 s later. AwaitingRetry carries no transition_id, so PreventDuplicateTransitions misses the resend, and RetryFailedFlows only applies from RUNNING: the server accepts the resent Failed and the run ends. A 60 s timeout plus a 2 s backoff is the 62 s in all three logs.
The resend gap is in Prefect, and is reported upstream as PrefectHQ/prefect#23975.
Fix. prefect_test_server sets PREFECT_API_REQUEST_TIMEOUT to READINESS_WAIT_TIMEOUT (300 s), so the client waits out the server's 60 s lock wait and any runner stall short of a hang. A stalled request then gets either its committed state or a 503 that the client retries safely, because nothing was committed. A runner stall still costs the test that time, but no longer changes its outcome, as in #12933.
New public surface
None.
User-visible behavior
None. Test-only.
Verification
Forced repro, not committed: as a @flow(retries=1) attempt raises, a scratch test takes the test server's SQLite write lock, so the server reads the Failed proposal and waits in BEGIN IMMEDIATE. Two seconds in, it SIGSTOPs the server, releases the lock, and SIGCONTs the server 61 s after taking it, so the server commits AwaitingRetry after the client's timeout and before its resend.
- Without the fix (client timeout 60 s): the run ends
Failed61.8 s after the raise, after one attempt, with no retry line. This is the CI signature. - With the fix (client timeout 300 s): the client gets
AwaitingRetryat 61.0 s, and the second attempt runs and passes.
The three failing tests pass unforced with the fix.
Scratch repro (drop into tests/durable_exec/)
"""Scratch repro, not committed: a server stall that outlives the client's request timeout loses the flow retry.
As the flow's first attempt raises, this holds the Prefect test server's SQLite write lock, so the server reads the
`Failed` proposal and waits in `BEGIN IMMEDIATE`. Two seconds in, it freezes the server (SIGSTOP), releases the lock,
and resumes the server 61 s after the lock was taken: past the client's default 60 s request timeout, before the
client's resend. The server then commits `AwaitingRetry` for a request the client gave up on.
"""
from __future__ import annotations
import os
import signal
import sqlite3
import threading
import time
import pytest
from ..conftest import try_import
with try_import() as imports_successful:
from prefect import flow
from prefect.server.api.server import SubprocessASGIServer
from prefect.settings import PREFECT_API_REQUEST_TIMEOUT, PREFECT_SERVER_DATABASE_CONNECTION_URL
pytestmark = [
pytest.mark.skipif(not imports_successful(), reason='prefect not installed'),
pytest.mark.usefixtures('prefect_test_server'),
pytest.mark.xdist_group(name='prefect'),
]
def stall_server(db_path: str, pid: int, held: threading.Event, log: list[str]) -> None:
conn = sqlite3.connect(db_path, isolation_level=None)
conn.execute('BEGIN IMMEDIATE')
start = time.monotonic()
held.set()
time.sleep(2)
os.kill(pid, signal.SIGSTOP)
conn.execute('COMMIT')
conn.close()
time.sleep(59)
os.kill(pid, signal.SIGCONT)
log.append(f'server resumed {time.monotonic() - start:.2f}s after the lock was taken')
async def test_retry_survives_server_stall() -> None:
print(f'client request timeout: {PREFECT_API_REQUEST_TIMEOUT.value()}s')
db_path = PREFECT_SERVER_DATABASE_CONNECTION_URL.value().removeprefix('sqlite+aiosqlite:///')
server = SubprocessASGIServer._instances[None].server_process
assert server is not None
pid = server.pid
attempts: list[int] = []
log: list[str] = []
stallers: list[threading.Thread] = []
@flow(retries=1)
async def retried() -> None:
attempts.append(1)
if len(attempts) == 1:
held = threading.Event()
t = threading.Thread(target=stall_server, args=(db_path, pid, held, log))
t.start()
stallers.append(t)
held.wait()
raise RuntimeError('retry')
try:
await retried()
finally:
for t in stallers:
t.join()
os.kill(pid, signal.SIGCONT)
print(*log, f'attempts: {len(attempts)}')
assert len(attempts) == 2
What changes for existing users
Nothing.
PR decisions
2026-11-18 · root cause of the lost Prefect flow retry · iter -
- Decision: The Prefect client's 60s request timeout is no longer than the test server's own SQLite lock wait (busy_timeout 60000, from BEGIN IMMEDIATE after the body is read); a stalled set_state commits AwaitingRetry after the client gave up, the client's ReadTimeout retry resends the same Failed, and the server accepts it (AwaitingRetry has no transition_id; RetryFailedFlows is FROM RUNNING only)
- Why: 62 s in all three CI logs = 60 s timeout + 2 s backoff; forced repro reproduces the exact signature; the cause of the server stall itself is a runner stall, not found in test code
- Source: https://github.com/pydantic/pydantic-ai/issues/13707
- Session: 97f0eab9-7b44-47c8-89c8-8a63b6316764
2026-11-18 · forced repro for the lost retry · iter -
- Decision: Scratch test holds the server's SQLite write lock so set_state(Failed) waits mid-request, SIGSTOPs the server 2 s in, releases the lock, and SIGCONTs it 61 s after the lock was taken; on main the run ends Failed after 61.8 s with one attempt (CI signature)
- Why: SIGSTOP before the server reads the body does not reproduce: uvicorn drops a request whose client disconnected before the body was read; a plain 61 s lock hold also passes because the server's 503 beats the client's timeout
- Source: https://github.com/pydantic/pydantic-ai/issues/13707
- Session: 97f0eab9-7b44-47c8-89c8-8a63b6316764
2026-11-18 · fix: client request timeout above the server's lock wait · iter -
- Decision: prefect_test_server sets PREFECT_API_REQUEST_TIMEOUT to READINESS_WAIT_TIMEOUT (300 s); a stalled request then gets its committed state or a 503 the client retries safely
- Why: The timeout pairing is the defect: it turns a tolerated server stall into a lost retry; precedent #12933 set product timeouts to READINESS_WAIT_TIMEOUT so a runner stall no longer changes a test's outcome; touches tests/conftest.py, so not a flake fix under the push skill
- Source: https://github.com/pydantic/pydantic-ai/pull/12933
- Session: 97f0eab9-7b44-47c8-89c8-8a63b6316764
2026-11-18 · pre-push review 1 of 3 at 23bbbb5d0256a9edf8d38ec76e0c587ec46fed53 · iter -
- Decision: reserved; dispatching (light tier: Haiku)
- Why: tree 4a2cd1d6ef1b9aa06a80009ed6a820a203881fb9; scope e3cb4783ee..23bbbb5d02 (full branch, first review)
- Source: https://github.com/pydantic/pydantic-ai/tree/fix-flaky-prefect-flow-retry-13707
- Session: 97f0eab9-7b44-47c8-89c8-8a63b6316764
2026-11-18 · pre-push review 1 of 3 at 23bbbb5d0256a9edf8d38ec76e0c587ec46fed53 · iter -
- Decision: remediated: request-timeout comment moved beside its setting (do); local 30s readiness_wait_timeout in the #13767 test deferred to the manager as an unverified sibling, tracked by PR comment (defer); production PrefectAgent timeout skipped (Prefect-owned setting and resend gap)
- Why: reviewed tree 4a2cd1d6ef1b9aa06a80009ed6a820a203881fb9; scope e3cb4783ee..23bbbb5d02; remediation dff714c0c0984003ba4dbd27d262ef6152037263 is comment-only (exempt delta: no non-comment line in git diff 23bbbb5d02..dff714c0c0984003ba4dbd27d262ef6152037263), chains from reviewed 23bbbb5d02
- Source: https://github.com/pydantic/pydantic-ai/tree/fix-flaky-prefect-flow-retry-13707
- Session: 97f0eab9-7b44-47c8-89c8-8a63b6316764
2026-11-18 · sibling 30s readiness wait in the #13767 test: skip · iter -
- Decision: Skip: no fix, no issue; the PR comment says so
- Why: Unreproduced, so it does not clear the reproduce-and-confirm bar; manager's call
- Source: https://github.com/pydantic/pydantic-ai/pull/13866#issuecomment-6982387855
- Session: 97f0eab9-7b44-47c8-89c8-8a63b6316764
- Supersedes: pre-push review 1 of 3 at 23bbbb5d0256a9edf8d38ec76e0c587ec46fed53 (defer part)
2026-11-18 · final metadata check at dff714c0c0984003ba4dbd27d262ef6152037263 · iter -
- Decision: current; the checker's flake-label conflict kept as its option (a): Closes lines and flake label stay, PR handed to the manager/maintainer rather than self-enqueued
- Why: No rule ties the flake label to the Flake-fixes self-merge exception; precedent #12933 carried flake while changing files outside the failing modules
- Source: https://github.com/pydantic/pydantic-ai/pull/12933
- Session: 97f0eab9-7b44-47c8-89c8-8a63b6316764
2026-11-18 · step 7 second opinion: none · iter -
- Decision: No additional review surface for PR 13866
- Why: Overmanager's pick via manager: test infrastructure only, the forced repro backs the timeout change (dispatch local-notes/dispatch-20261118T161948Z.md, from manager dispatch-20261118T161935Z.md)
- Source: https://github.com/pydantic/pydantic-ai/pull/13866
- Session: 97f0eab9-7b44-47c8-89c8-8a63b6316764
2026-11-18 · report the Prefect duplicate-transition gap upstream · iter -
- Decision: Filed https://github.com/PrefectHQ/prefect/issues/23975 with a standalone prefect-only repro (5 s client timeout, 6 s SQLite lock hold; history Pending -> Running -> AwaitingRetry -> Failed on prefect 3.9.0)
- Why: David's pick, relayed in local-notes/dispatch-20261118T161948Z.md; no existing PrefectHQ/prefect issue matched
- Source: https://github.com/pydantic/pydantic-ai/pull/13866
- Session: 97f0eab9-7b44-47c8-89c8-8a63b6316764
- Supersedes: Out-of-scope note in issue-brief: report the gap to the manager, do not file upstream
2026-11-18 · final metadata check (rerun) at dff714c0c0984003ba4dbd27d262ef6152037263 · iter -
- Decision: Removed area:prefect and area:durable-exec; kept decision-entry paths and the Verification section as they are
- Why: Label rule: repository infrastructure (CI, tests) takes no area label; decision paths are not confidential reasoning and the log is authoritative; the Files-changed link rule is for feature or behavior changes
- Source: https://github.com/pydantic/pydantic-ai/pull/13866
- Session: 97f0eab9-7b44-47c8-89c8-8a63b6316764
Checklist
- No breaking changes in accordance with the version policy.
- Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
- PR title is fit for the release changelog.