Compare commits

..

9 commits

Author SHA1 Message Date
Alex Garcia
ec4866bea4 Apply ruff 0.16 and black fixes
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012U7coQfVu8nK2R4q2mCULA
2026-08-31 13:18:18 -07:00
Alex Garcia
90e9809810 Remove reference to untracked local plans/ directory
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012U7coQfVu8nK2R4q2mCULA
2026-08-31 13:17:50 -07:00
Alex Garcia
49e58f797f Ensure immutable table counts still precompute when startup ran first
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-31 13:17:50 -07:00
Alex Garcia
96cba70e09 Run startup via ASGI lifespan instead of waiting for the first request
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-31 13:17:50 -07:00
Alex Garcia
c748683f2e Apply ruff 0.16 and black fixes
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012U7coQfVu8nK2R4q2mCULA
2026-08-31 13:17:44 -07:00
Alex Garcia
55b995e5a3 Explain why serve_with_plugins needs a subprocess and plugin files
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012U7coQfVu8nK2R4q2mCULA
2026-08-31 13:13:03 -07:00
Alex Garcia
d4b6d6cf4e Fix datasette-litestream URL and trim marker-task test comments
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012U7coQfVu8nK2R4q2mCULA
2026-08-31 13:13:03 -07:00
Alex Garcia
ba3756eb54 Move the serve-subprocess test plumbing into a conftest fixture
The two new tests span a datasette serve subprocess by hand: each found a
free port with a copy of test_playwright.py's find_free_port, built its own
subprocess.Popen call, polled with its own 15 second deadline loop, and
tore the process down in its own finally block. That is the third and
fourth hand-rolled copy of plumbing conftest.py already owns for
ds_localhost_http_server and ds_unix_domain_socket_server.

Move find_free_port into conftest.py and add a serve_with_plugins factory
fixture that writes plugin sources to a temporary --plugins-dir, takes a
free port, waits for the server to answer, and terminates every process it
started when the test ends. wait_until_responds() grows an optional
process argument so a server that dies during startup fails immediately
with its captured output instead of waiting out the timeout, and now
catches httpx.TransportError rather than only httpx.ConnectError - a
superclass, so existing callers are unaffected.

Two fixes beyond the deduplication:

The marker test polls until its flag flips, which meant it would also have
passed if the startup hook were re-run on the serving loop by the
first-request fallback - the exact bug it exists to catch. That cannot
happen while invoke_startup() is idempotent, but nothing said so. The
plugin now counts startup calls and the test asserts it ran exactly once,
so removing that guard fails the test loudly instead of quietly turning it
into a no-op.

test_startup_error_fails_fast_before_port_binds passes on unmodified main,
where startup already ran ahead of uvicorn.run(), so it is a
characterization test rather than a regression test for this commit; its
docstring now says so. Its loop re-checking that nothing was listening ran
about one iteration before the process exited, and could not distinguish a
pre-bind failure from a port nothing ever touched, so it is replaced by a
single check with a comment about what it does and does not prove.

Verified the red side is preserved: the marker test still fails on
unmodified main, now in 3.7s rather than 15.2s.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-31 13:13:03 -07:00
Alex Garcia
cd9daa266c Run datasette serve startup and uvicorn on a single event loop
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-31 13:13:03 -07:00
3 changed files with 12 additions and 5 deletions

View file

@ -11,17 +11,16 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.10", "3.11", "3.12", "3.13", "3.14", "3.15"]
python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
steps:
- uses: actions/checkout@v7
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v7
uses: actions/setup-python@v6
with:
python-version: ${{ matrix.python-version }}
allow-prereleases: true
cache: pip
cache-dependency-path: pyproject.toml
check-latest: true
- name: Build extension for --load-extension test
run: |-
(cd tests && gcc ext.c -fPIC -shared -o ext.so)

View file

@ -670,9 +670,11 @@ def serve(
raise click.ClickException("--token can only be used with --get")
if get:
# --get means we don't run Uvicorn at all
# Run async soundness checks before startup hooks, since invoke_startup
# now populates internal tables which requires querying each database
run_sync(lambda: check_databases(ds))
# Run the "startup" plugin hooks
try:
run_sync(ds.invoke_startup)
except StartupError as e:
@ -707,7 +709,8 @@ def serve(
# on the loop (asyncio.create_task, Lock/Queue/Event objects, ...) is
# still alive when the server starts handling requests.
async def _serve_async():
# Populate internal catalog tables before invoke_startup
# Run async soundness checks before startup hooks, since invoke_startup
# now populates internal tables which requires querying each database
await check_databases(ds)
# Run the full startup sequence (immutable-database table-count

View file

@ -127,6 +127,11 @@ def test_startup_error_fails_fast_before_port_binds(serve_with_plugins):
A "startup" plugin hook that raises StartupError must fail fast: print
the message, exit non-zero, and never accept a connection on the port -
the failure must happen before uvicorn.Server binds the socket.
Note this is a characterization test, not a regression test: it also
passes on unmodified main, where startup already ran ahead of
uvicorn.run(). It earns its keep once startup moves into the ASGI
lifespan, where fail-fast is genuinely at risk.
"""
proc, port = serve_with_plugins(
{"startup_error_plugin": STARTUP_ERROR_PLUGIN}, wait_for_startup=False