diff --git a/datasette/database.py b/datasette/database.py index ef516e9ed9..f2c980d705 100644 --- a/datasette/database.py +++ b/datasette/database.py @@ -169,7 +169,11 @@ async def execute_isolated_fn(self, fn): result = fn(isolated_connection) finally: isolated_connection.close() - self._all_file_connections.remove(isolated_connection) + try: + self._all_file_connections.remove(isolated_connection) + except ValueError: + # Was probably a memory connection + pass return result else: # Threaded mode - send to write thread @@ -230,7 +234,11 @@ def _execute_writes(self): result = e finally: isolated_connection.close() - self._all_file_connections.remove(isolated_connection) + try: + self._all_file_connections.remove(isolated_connection) + except ValueError: + # Was probably a memory connection + pass else: try: result = task.fn(conn) diff --git a/tests/test_internals_database.py b/tests/test_internals_database.py index eb842c968e..e05111007a 100644 --- a/tests/test_internals_database.py +++ b/tests/test_internals_database.py @@ -1,6 +1,7 @@ """ Tests for the datasette.database.Database class """ +from datasette.app import Datasette from datasette.database import Database, Results, MultipleValues from datasette.utils.sqlite import sqlite3 from datasette.utils import Column @@ -543,7 +544,12 @@ def inner(conn): @pytest.mark.asyncio -async def test_execute_isolated(db): +@pytest.mark.parametrize("disable_threads", (False, True)) +async def test_execute_isolated(db, disable_threads): + if disable_threads: + ds = Datasette(memory=True, settings={"num_sql_threads": 0}) + db = ds.add_database(Database(ds, memory_name="test_num_sql_threads_zero")) + # Create temporary table in write await db.execute_write( "create temporary table created_by_write (id integer primary key)" @@ -577,6 +583,7 @@ def create_shared_table(conn): # ... and a second call to isolated should not see that connection either assert not await db.execute_isolated_fn(table_exists_checker("created_by_isolated")) + @pytest.mark.asyncio async def test_mtime_ns(db): assert isinstance(db.mtime_ns, int)