#!/usr/bin/env python3
# Synthetic stall for #23372: hold an outside read transaction on a synthetic
# statev2.sqlite, then grow the WAL to ~48 MB behind that snapshot so the
# backend's startup writes/checkpoint stall. Released by killing this process.
import sqlite3, sys, time, os
db, flag = sys.argv[1], sys.argv[2]
reader = sqlite3.connect(db, isolation_level=None)
reader.execute("PRAGMA journal_mode=WAL")
reader.execute("BEGIN")
print("reader snapshot rows in sqlite_master:", reader.execute("SELECT count(*) FROM sqlite_master").fetchone()[0], flush=True)
w = sqlite3.connect(db, isolation_level=None)
w.execute("PRAGMA wal_autocheckpoint=0")
w.execute("CREATE TABLE IF NOT EXISTS ev_synthetic_pad (id INTEGER PRIMARY KEY, b BLOB)")
for i in range(48):
    w.execute("BEGIN")
    w.execute("INSERT INTO ev_synthetic_pad (b) VALUES (randomblob(1048576))")
    w.execute("COMMIT")
w.close()
print("wal bytes:", os.path.getsize(db + "-wal"), flush=True)
open(flag, "w").close()
while True:
    time.sleep(3600)
