test: close all database connections before deleting temp files

Windows refuses to unlink an open SQLite file and executor threads keep
their own connections, so every owned test that creates a PromptDatabase
calls close_all() before cleanup; the diagnostics temp dir ignores
cleanup errors.
This commit is contained in:
Vito Sansevero
2026-10-08 18:01:34 -07:00
parent b127bbeba1
commit 13d1115406
6 changed files with 19 additions and 9 deletions
+3
View File
@@ -38,6 +38,9 @@ class APITestCase(AioHTTPTestCase):
return app
async def tearDownAsync(self):
# Close every thread's connection first: Windows refuses to delete
# an open database file, and executor threads hold connections too.
self.api.db.close_all()
for path in (
self._temp_db.name,
self._temp_db.name + "-wal",
+2
View File
@@ -372,6 +372,7 @@ class CoreRoutesTestCase(AioHTTPTestCase):
return app
async def tearDownAsync(self):
self.api.db.close_all()
shutil.rmtree(self.tmpdir, ignore_errors=True)
def _fake_root(self):
@@ -557,6 +558,7 @@ class TestInstanceHelpers(FolderPathsFixture):
super().setUp()
self.api = PromptManagerAPI()
self.api.db = PromptDatabase(os.path.join(self.tmpdir, "prompts.db"))
self.addCleanup(self.api.db.close_all) # runs before the rmtree cleanup
orig_dirs = list(GalleryConfig.MONITORING_DIRECTORIES)
self.addCleanup(setattr, GalleryConfig, "MONITORING_DIRECTORIES", orig_dirs)
+1 -1
View File
@@ -185,7 +185,7 @@ class TestAdminEndpointsUseConfigPath(unittest.TestCase):
self.assertGreater(len(result.body), 0)
self.assertTrue(result.body.startswith(b"SQLite format 3\x00"))
finally:
real_db.close()
real_db.close_all()
for suffix in ("", "-wal", "-shm"):
if os.path.exists(db_file + suffix):
os.unlink(db_file + suffix)
+1
View File
@@ -51,6 +51,7 @@ class LinkingTestCase(unittest.TestCase):
)
def tearDown(self):
self.db.close_all() # Windows cannot unlink an open database file
for path in (
self.image,
self.db_path,
+1
View File
@@ -182,6 +182,7 @@ class NodeTestCase(unittest.TestCase):
self.node.comfyui_integration = mock.Mock()
def tearDown(self):
self.db.close_all() # Windows cannot unlink an open database file
for suffix in ("", "-wal", "-shm"):
if os.path.exists(self.db_path + suffix):
os.unlink(self.db_path + suffix)
+11 -8
View File
@@ -25,20 +25,23 @@ def same_path(a, b):
class DiagnosticsTestCase(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.tmp = tempfile.TemporaryDirectory(ignore_cleanup_errors=True)
self.addCleanup(self.tmp.cleanup)
self.db_path = os.path.join(self.tmp.name, "prompts.db")
self.diag = GalleryDiagnostics(self.db_path)
def _populate(self):
db = PromptDatabase(self.db_path)
prompt_id = db.save_prompt(
text="a cat", prompt_hash=generate_prompt_hash("a cat")
)
image = os.path.join(self.tmp.name, "cat.png")
with open(image, "wb") as f:
f.write(b"x")
db.link_image_to_prompt(prompt_id=prompt_id, image_path=image, metadata={})
try:
prompt_id = db.save_prompt(
text="a cat", prompt_hash=generate_prompt_hash("a cat")
)
image = os.path.join(self.tmp.name, "cat.png")
with open(image, "wb") as f:
f.write(b"x")
db.link_image_to_prompt(prompt_id=prompt_id, image_path=image, metadata={})
finally:
db.close_all() # Windows cannot delete the temp dir while it is open
return prompt_id
def _chdir(self, path):