Fixed hundreds of database connection closing warnings

From 409 warnings down to 52 warnings.

Claude Code says:

Fixed connection leaks in:
1. datasette/utils/sqlite.py - _sqlite_version() now closes connection
2. datasette/cli.py - --create flag now closes connection
3. datasette/app.py - _versions() now closes connection
4. datasette/utils/__init__.py - detect_json1() now closes connection when created internally
5. tests/conftest.py - pytest_report_header() now closes connection
6. tests/utils.py - has_load_extension() now closes connection
7. tests/fixtures.py - app_client_no_files and CLI fixtures now close connections
8. tests/test_api_write.py - ds_write fixture closes both connections
9. tests/test_cli.py - Multiple test functions now close connections
10. tests/test_config_dir.py - config_dir and config_dir_client fixtures now close connections
11. tests/test_crossdb.py - Loop connections now closed
12. tests/test_internals_database.py - Test setup connections now closed
13. tests/test_plugins.py - view_names_client fixture and test now close connections
14. tests/test_utils.py - Multiple test functions now close connections

Refs #2614
This commit is contained in:
Simon Willison 2025-12-12 22:38:04 -08:00
commit 35ea721469
14 changed files with 75 additions and 28 deletions

View file

@ -442,7 +442,9 @@ def test_serve_duplicate_database_names(tmpdir):
nested.mkdir()
db_2_path = str(tmpdir / "nested" / "db.db")
for path in (db_1_path, db_2_path):
sqlite3.connect(path).execute("vacuum")
conn = sqlite3.connect(path)
conn.execute("vacuum")
conn.close()
result = runner.invoke(cli, [db_1_path, db_2_path, "--get", "/-/databases.json"])
assert result.exit_code == 0, result.output
databases = json.loads(result.output)
@ -456,7 +458,9 @@ def test_weird_database_names(tmpdir, filename):
# https://github.com/simonw/datasette/issues/1181
runner = CliRunner()
db_path = str(tmpdir / filename)
sqlite3.connect(db_path).execute("vacuum")
conn = sqlite3.connect(db_path)
conn.execute("vacuum")
conn.close()
result1 = runner.invoke(cli, [db_path, "--get", "/"])
assert result1.exit_code == 0, result1.output
filename_no_stem = filename.rsplit(".", 1)[0]
@ -493,7 +497,9 @@ def test_duplicate_database_files_error(tmpdir):
"""Test that passing the same database file multiple times raises an error"""
runner = CliRunner()
db_path = str(tmpdir / "test.db")
sqlite3.connect(db_path).execute("vacuum")
conn = sqlite3.connect(db_path)
conn.execute("vacuum")
conn.close()
# Test with exact duplicate
result = runner.invoke(cli, ["serve", db_path, db_path, "--get", "/"])
@ -512,7 +518,9 @@ def test_duplicate_database_files_error(tmpdir):
config_dir = tmpdir / "config"
config_dir.mkdir()
config_db_path = str(config_dir / "data.db")
sqlite3.connect(config_db_path).execute("vacuum")
conn = sqlite3.connect(config_db_path)
conn.execute("vacuum")
conn.close()
result3 = runner.invoke(
cli, ["serve", config_db_path, str(config_dir), "--get", "/"]
@ -523,7 +531,9 @@ def test_duplicate_database_files_error(tmpdir):
# Test that mixing a file NOT in the directory with a directory works fine
other_db_path = str(tmpdir / "other.db")
sqlite3.connect(other_db_path).execute("vacuum")
conn = sqlite3.connect(other_db_path)
conn.execute("vacuum")
conn.close()
result4 = runner.invoke(
cli, ["serve", other_db_path, str(config_dir), "--get", "/-/databases.json"]