Vincent Chéry
04/10/2026, 10:07 AMTimed out while attempting to connect to ephemeral Prefect API server.Marvin
04/10/2026, 10:07 AMMarvin
04/10/2026, 10:11 AMfrom prefect.testing.utilities import prefect_test_harness
def test_something():
with prefect_test_harness(server_startup_timeout=90):
...
- Or via env var when running tests:
PREFECT_SERVER_EPHEMERAL_STARTUP_TIMEOUT_SECONDS=90 pytest
- If you’re starting the server directly, pass a longer timeout:
from prefect.server.api.server import SubprocessASGIServer
s = SubprocessASGIServer()
s.start(timeout=90)
print(s.api_url)
s.stop()
- If you’re running tests in parallel (e.g., pytest-xdist), ensure each worker has its own harness instance and avoids sharing a SQLite file. A session-scoped, autouse fixture is the usual pattern:
import pytest
from prefect.testing.utilities import prefect_test_harness
@pytest.fixture(autouse=True, scope="session")
def _prefect_test_env():
with prefect_test_harness(server_startup_timeout=90):
yield
- Check for port conflicts (the ephemeral server probes ports in the 8000–9000 range):
lsof -nP -iTCP -sTCP:LISTEN | grep '8[0-9][0-9][0-9]'
- Ensure Python has SQLite support (the ephemeral server uses SQLite by default):
python -c "import sqlite3; print(sqlite3.sqlite_version)"
- Turn up logging to see what’s happening during startup:
PREFECT_LOGGING_LEVEL=DEBUG pytest
You can also start a server manually to verify your environment:
prefect server start --log-level DEBUG
# then hit <http://127.0.0.1:4200/api/health>
Why this helps:
- On macOS ARM, process spawn + import time (uvicorn/fastapi/sqlalchemy, test coverage, etc.) can push startup past the default 20–30s check, so bumping to 60–90s is often enough.
- Parallel test runs can collide on the same SQLite file or saturate the port probe window, causing health checks to time out.
If it still times out after increasing the timeout:
- Let me know your Prefect version, Python version/arch (arm64 vs x86_64/Rosetta), and whether you’re running tests in parallel.
- If you can share the first 50–100 lines of DEBUG logs around server startup, I can pinpoint whether it’s port contention, DB init/migrations taking too long, or something else.
References:
- Testing workflows guide: Testing workflows (Prefect 3)
- Harness API: prefect_test_harness API
- Ephemeral server settings: Server ephemeral settings