mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Don't hang in db.execute_write_fn() if connection fails
Closes #935 Refs https://github.com/simonw/latest-datasette-with-all-plugins/issues/3
This commit is contained in:
parent
13b3b51087
commit
b86f94883b
3 changed files with 32 additions and 6 deletions
|
|
@ -89,14 +89,22 @@ class Database:
|
||||||
def _execute_writes(self):
|
def _execute_writes(self):
|
||||||
# Infinite looping thread that protects the single write connection
|
# Infinite looping thread that protects the single write connection
|
||||||
# to this database
|
# to this database
|
||||||
conn = self.connect(write=True)
|
conn_exception = None
|
||||||
|
conn = None
|
||||||
|
try:
|
||||||
|
conn = self.connect(write=True)
|
||||||
|
except Exception as e:
|
||||||
|
conn_exception = e
|
||||||
while True:
|
while True:
|
||||||
task = self._write_queue.get()
|
task = self._write_queue.get()
|
||||||
try:
|
if conn_exception is not None:
|
||||||
result = task.fn(conn)
|
result = conn_exception
|
||||||
except Exception as e:
|
else:
|
||||||
print(e)
|
try:
|
||||||
result = e
|
result = task.fn(conn)
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
result = e
|
||||||
task.reply_queue.sync_q.put(result)
|
task.reply_queue.sync_q.put(result)
|
||||||
|
|
||||||
async def execute_fn(self, fn):
|
async def execute_fn(self, fn):
|
||||||
|
|
|
||||||
1
setup.py
1
setup.py
|
|
@ -72,6 +72,7 @@ setup(
|
||||||
"pytest-asyncio>=0.10,<0.15",
|
"pytest-asyncio>=0.10,<0.15",
|
||||||
"beautifulsoup4>=4.8.1,<4.10.0",
|
"beautifulsoup4>=4.8.1,<4.10.0",
|
||||||
"black~=19.10b0",
|
"black~=19.10b0",
|
||||||
|
"pytest-timeout>=1.4.2,<1.5",
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
tests_require=["datasette[test]"],
|
tests_require=["datasette[test]"],
|
||||||
|
|
|
||||||
|
|
@ -189,6 +189,23 @@ async def test_execute_write_fn_exception(db):
|
||||||
await db.execute_write_fn(write_fn, block=True)
|
await db.execute_write_fn(write_fn, block=True)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
@pytest.mark.timeout(1)
|
||||||
|
async def test_execute_write_fn_connection_exception(tmpdir, app_client):
|
||||||
|
path = str(tmpdir / "immutable.db")
|
||||||
|
sqlite3.connect(path).execute("vacuum")
|
||||||
|
db = Database(app_client.ds, path=path, is_mutable=False)
|
||||||
|
app_client.ds.add_database("immutable-db", db)
|
||||||
|
|
||||||
|
def write_fn(conn):
|
||||||
|
assert False
|
||||||
|
|
||||||
|
with pytest.raises(AssertionError):
|
||||||
|
await db.execute_write_fn(write_fn, block=True)
|
||||||
|
|
||||||
|
app_client.ds.remove_database("immutable-db")
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_mtime_ns(db):
|
async def test_mtime_ns(db):
|
||||||
assert isinstance(db.mtime_ns, int)
|
assert isinstance(db.mtime_ns, int)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue