Skip to content

160-tool MCP server for real-time global intelligence from free public APIs: markets, liquidity, prediction markets, SEC filings, conflict, military, aircraft-cohort early warning, migration and border flows, cyber, climate, news and more. Dated observation history, 29 research prompts, cited briefs, geofences, SSE dashboard. MIT, no paid API keys.

History: close the store's connection when its file is not a database

Phoenix Agent (mac-studio)6d9d6c98e18e
diff --git a/src/world_intel_mcp/history.py b/src/world_intel_mcp/history.py
index d3dc5a3..4041392 100644
--- a/src/world_intel_mcp/history.py
+++ b/src/world_intel_mcp/history.py
@@ -871,6 +871,15 @@ class HistoryStore:
# The connect timeout is SQLite's busy timeout: how long a write
# waits on another writer's lock before "database is locked".
self._conn = sqlite3.connect(str(self.db_path), timeout=BUSY_TIMEOUT_S)
+ try:
+ self._create_schema()
+ except BaseException:
+ # A file that is not a database (or a failed migration) raises
+ # here; the caller never gets a store to close, so close it now.
+ self._conn.close()
+ raise
+
+ def _create_schema(self) -> None:
self._conn.execute("PRAGMA journal_mode=WAL")
self._conn.execute("PRAGMA synchronous=NORMAL")
self._conn.executescript(
diff --git a/src/world_intel_mcp/tests/test_history_honesty.py b/src/world_intel_mcp/tests/test_history_honesty.py
index 07dd8f6..8b3462e 100644
--- a/src/world_intel_mcp/tests/test_history_honesty.py
+++ b/src/world_intel_mcp/tests/test_history_honesty.py
@@ -11,6 +11,7 @@ the collector attaches, at fixed times.
"""
import asyncio
+import gc
import json
import os
import sqlite3
@@ -19,6 +20,7 @@ import tempfile
import threading
import time
import types
+import warnings
from urllib.parse import unquote
import httpx
@@ -996,3 +998,26 @@ def test_migration_tolerates_a_column_another_process_just_added(tmp_path) -> No
finally:
conn.close()
assert _capture_columns(db).count("stale_age_s") == 1
+
+
+def test_a_file_that_is_not_a_database_raises_and_leaks_no_connection(tmp_path, monkeypatch):
+ db = tmp_path / "corrupt.db"
+ db.write_bytes(b"this is not an SQLite database\n" * 64)
+ opened = []
+ real_connect = sqlite3.connect
+
+ def _spy(*args, **kwargs):
+ opened.append(real_connect(*args, **kwargs))
+ return opened[-1]
+
+ monkeypatch.setattr(history.sqlite3, "connect", _spy)
+ gc.collect() # connections other tests leaked warn now, not inside the block
+ with warnings.catch_warnings(record=True) as caught:
+ warnings.simplefilter("always")
+ with pytest.raises(sqlite3.DatabaseError, match="not a database"):
+ HistoryStore(db)
+ gc.collect()
+ (conn,) = opened
+ with pytest.raises(sqlite3.ProgrammingError, match="closed"):
+ conn.execute("SELECT 1")
+ assert [w for w in caught if issubclass(w.category, ResourceWarning)] == []