Skip to content

Fix for Database.close()/Datasette.close() order#2710

Merged
simonw merged 1 commit into
mainfrom
issue-2709
May 12, 2026
Merged

Fix for Database.close()/Datasette.close() order#2710
simonw merged 1 commit into
mainfrom
issue-2709

Conversation

@simonw
Copy link
Copy Markdown
Owner

@simonw simonw commented May 12, 2026

@simonw
Copy link
Copy Markdown
Owner Author

simonw commented May 12, 2026

I shipped this as a wheel file to https://static.simonwillison.net/static/2026/datasette-1.0a28.post1-py3-none-any.whl and then used this Dockerfile to confirm that the segfault bug is fixed:

# syntax=docker/dockerfile:1.7

FROM python:3.12-slim-bookworm

ENV PYTHONFAULTHANDLER=1

RUN pip install --no-cache-dir \
    'https://static.simonwillison.net/static/2026/datasette-1.0a28.post1-py3-none-any.whl' \
    pytest==9.0.2 \
    sqlite-utils==3.39

WORKDIR /repro

COPY <<'PY' test_datasette_autoclose_crash.py
import asyncio
import time

from datasette.app import Datasette


def test_datasette_pytest_autoclose_closes_connection_during_execute(tmp_path):
    async def run():
        db_path = tmp_path / "data.db"
        db_path.touch()

        ds = Datasette([str(db_path)], internal=str(tmp_path / "internal.db"))
        loop = asyncio.get_running_loop()
        sql_started = asyncio.Event()
        original_prepare_connection = ds._prepare_connection

        def prepare_connection(conn, name):
            original_prepare_connection(conn, name)

            def sleep_ms(ms):
                loop.call_soon_threadsafe(sql_started.set)
                time.sleep(ms / 1000)
                return ms

            conn.create_function("sleep_ms", 1, sleep_ms)

        ds._prepare_connection = prepare_connection
        asyncio.create_task(
            ds.get_database().execute("select sleep_ms(2000)", custom_time_limit=5000)
        )
        await asyncio.wait_for(sql_started.wait(), timeout=5)

    asyncio.run(run())
PY

CMD ["pytest", "-s", "-vv", "/repro/test_datasette_autoclose_crash.py"]

Then:

docker build -t recreate-issue-2709 .
docker run --rm recreate-issue-2709

This passed. With the current Datasette 1.0a28 it crashes.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 12, 2026

Codecov Report

❌ Patch coverage is 0% with 22 lines in your changes missing coverage. Please review.
✅ Project coverage is 0.00%. Comparing base (aa84fe0) to head (21b2b54).

Files with missing lines Patch % Lines
datasette/database.py 0.00% 21 Missing ⚠️
datasette/version.py 0.00% 1 Missing ⚠️
Additional details and impacted files
@@          Coverage Diff          @@
##            main   #2710   +/-   ##
=====================================
  Coverage   0.00%   0.00%           
=====================================
  Files         57      57           
  Lines       8744    8763   +19     
=====================================
- Misses      8744    8763   +19     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@simonw simonw merged commit 345f910 into main May 12, 2026
40 checks passed
@simonw simonw deleted the issue-2709 branch May 12, 2026 23:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant