Skip to content

Commit f4553dc

Browse files
test(web): issue 21397 seeder closes SQLite before failure cleanup
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 07892a7 commit f4553dc

2 files changed

Lines changed: 27 additions & 6 deletions

File tree

‎scripts/seed-issue-21397.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
(current time) or --now ISO to shift every timestamp so the failure happens then.
1515
"""
1616
import argparse
17+
import contextlib
1718
import datetime
1819
import json
1920
import pathlib
@@ -74,7 +75,8 @@ def shift(value, delta):
7475
if wal.is_file() and not wal.is_symlink():
7576
shutil.copyfile(wal, path.with_name(path.name + '-wal'))
7677
fixture = json.loads((root / 'scripts/fixtures/issue-21397.json').read_text())
77-
with sqlite3.connect(path) as db:
78+
# closing() outlives the transaction so cleanup below never deletes an open database.
79+
with contextlib.closing(sqlite3.connect(path)) as db, db:
7880
# Drop copied conversations, including V1 tables the server could re-import.
7981
tables = [r[0] for r in db.execute("SELECT name FROM sqlite_master WHERE type='table'")]
8082
for table in tables:
@@ -100,7 +102,6 @@ def shift(value, delta):
100102
columns = ','.join(f'"{k}"' for k in row)
101103
placeholders = ','.join('?' for _ in row)
102104
db.execute(f'INSERT INTO "{table}" ({columns}) VALUES ({placeholders})', list(row.values()))
103-
db.close()
104105
settings = dict(snoozeLimitedThreads=False, autoResumeLimitedThreads=False)
105106
(userdata / 'settings.json').write_text(json.dumps(settings, indent=2) + '\n')
106107
except BaseException:

‎scripts/seed-issue-21397.test.py‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,25 @@
1313

1414
SCRIPT = pathlib.Path(__file__).resolve().with_name('seed-issue-21397.py')
1515
FIXTURE = json.loads(SCRIPT.with_name('fixtures').joinpath('issue-21397.json').read_text())
16+
# Windows cannot delete a file SQLite still holds open, so cleanup must close connections first.
17+
OPEN_DB_PROBE = '''
18+
import runpy, shutil, sqlite3, sys
19+
opened, connect, rmtree = [], sqlite3.connect, shutil.rmtree
20+
def tracked(*args, **kwargs):
21+
opened.append(connect(*args, **kwargs))
22+
return opened[-1]
23+
def checked(*args, **kwargs):
24+
for db in opened:
25+
try:
26+
db.total_changes
27+
except sqlite3.ProgrammingError:
28+
continue
29+
sys.exit('rmtree ran while a SQLite connection was open')
30+
return rmtree(*args, **kwargs)
31+
sqlite3.connect, shutil.rmtree = tracked, checked
32+
sys.argv = sys.argv[1:]
33+
runpy.run_path(sys.argv[0], run_name='__main__')
34+
'''
1635

1736

1837
def snapshot(directory):
@@ -49,9 +68,9 @@ def setUp(self):
4968
(self.source_home / 'settings.json').write_text('{"sentinel": true}\n')
5069
self.before = snapshot(self.source_home)
5170

52-
def run_seeder(self, *args):
71+
def run_seeder(self, *args, probe=()):
5372
env = {**os.environ, 'HOME': str(self.fake_home)}
54-
return subprocess.run([sys.executable, '-I', str(SCRIPT), '--source', str(self.source), *args],
73+
return subprocess.run([sys.executable, '-I', *probe, str(SCRIPT), '--source', str(self.source), *args],
5574
env=env, capture_output=True, text=True)
5675

5776
def assertSourceUnchanged(self):
@@ -151,9 +170,10 @@ def test_failed_seed_removes_only_its_new_home(self):
151170
db.close()
152171
self.before = snapshot(self.source_home)
153172
home = self.tmp / 'broken'
154-
result = self.run_seeder('--home', str(home))
173+
result = self.run_seeder('--home', str(home), probe=('-c', OPEN_DB_PROBE))
155174
self.assertNotEqual(result.returncode, 0)
156-
self.assertFalse(home.exists())
175+
self.assertIn('orchestration_v2_effect_outbox', result.stderr)
176+
self.assertFalse(home.exists(), result.stderr)
157177
self.assertSourceUnchanged()
158178

159179

0 commit comments

Comments
 (0)