From b4278bcbc28126d66405910dbab19ea8d70508be Mon Sep 17 00:00:00 2001 From: repo-worker-tmp-code-b Date: Wed, 11 Nov 2026 00:33:15 -0600 Subject: [PATCH 1/3] fix(cold-store): bound export-turso scratch to one named dir, deleted per database (darwin-nix#373) --- .../cold-store-reaper/scripts/export-turso.sh | 46 ++++++++++++++++++- .../tests/test_export_turso.py | 19 ++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/pkgs/cold-store-reaper/scripts/export-turso.sh b/pkgs/cold-store-reaper/scripts/export-turso.sh index 9d1ef247bc..9dcbfc9d3b 100755 --- a/pkgs/cold-store-reaper/scripts/export-turso.sh +++ b/pkgs/cold-store-reaper/scripts/export-turso.sh @@ -59,6 +59,12 @@ # COLD_STORE_BIN optional; the cold-store executable # (default: ../cold-store next to this # script). +# EXPORT_TURSO_WORK_DIR optional; the scratch directory for +# dumps and rebuilds (default: +# ${TMPDIR:-/tmp}/cold-store-export-turso). +# One named directory, wiped at startup +# and on exit - never a fresh anonymous +# mktemp dir (darwin-nix#373). # # A database that fails any stage prints a loud ERROR line naming it; # the run continues with the remaining databases and exits non-zero @@ -90,7 +96,27 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" COLD_STORE="${COLD_STORE_BIN:-${SCRIPT_DIR}/../cold-store}" SQLITE3="${SQLITE3_BIN:-sqlite3}" -WORK="$(mktemp -d)" +# Scratch lives in ONE named directory, never a fresh anonymous +# mktemp dir (darwin-nix#373): the 2026-11-08/09/10 cold-store-nightly +# runs were killed by the service's 30-minute start timeout with +# ~30 GB of accumulated exports in an anonymous /tmp/tmp.* dir; the +# EXIT trap does not run under SIGKILL, so each killed run leaked +# the whole directory and the next run started a new one. A fixed +# location bounds the damage: a killed run leaves at most this one +# directory, and the next run wipes it before starting. The flock +# keeps two concurrent runs (timer + manual) from wiping each +# other's scratch mid-flight. Per-database files are deleted as +# soon as each database is filed (see export_one), so a live run's +# footprint is its largest single database, not the sum of all of +# them. The .lock file itself is 0 bytes and stays. +WORK="${EXPORT_TURSO_WORK_DIR:-${TMPDIR:-/tmp}/cold-store-export-turso}" +exec 9>"${WORK}.lock" +if ! flock -n 9; then + echo "turso export: ERROR: another export-turso run holds ${WORK}.lock" >&2 + exit 1 +fi +rm -rf "${WORK}" +mkdir -p "${WORK}" trap 'rm -rf "${WORK}"' EXIT # curl with an Authorization header that never touches argv (#1027): @@ -136,7 +162,7 @@ if [[ ${#DB_ROWS[@]} -eq 0 ]]; then fi # ------------------------------------------------------------ one database -export_one() { +export_one_run() { local name="$1" hostname="$2" local dump_sql="${WORK}/${name}.sql" db_file="${WORK}/${name}.db" local filtered_sql="${WORK}/${name}.filtered.sql" @@ -253,6 +279,22 @@ PY echo "turso export: filed ${name} (${gz_size} bytes gzipped)" } +# export_one wraps export_one_run with per-database scratch cleanup: +# a database's dump, filtered dump, rebuilt file, gzipped export and +# minted-token response are deleted as soon as its export finishes, +# pass or fail, instead of accumulating in WORK until the run ends. +# Only the run-level files (databases.json / databases.tsv) live for +# the whole run, and WORK itself is wiped at the next run's start. +export_one() { + local name="$1" hostname="$2" + local rc=0 + export_one_run "${name}" "${hostname}" || rc=$? + rm -f "${WORK}/${name}.sql" "${WORK}/${name}.filtered.sql" \ + "${WORK}/${name}.db" "${WORK}/${name}.db.gz" \ + "${WORK}/${name}.token.json" + return "${rc}" +} + FAILURES=0 for row in "${DB_ROWS[@]}"; do name="${row%%$'\t'*}" diff --git a/pkgs/cold-store-reaper/tests/test_export_turso.py b/pkgs/cold-store-reaper/tests/test_export_turso.py index 58bb6be651..50b72e6918 100644 --- a/pkgs/cold-store-reaper/tests/test_export_turso.py +++ b/pkgs/cold-store-reaper/tests/test_export_turso.py @@ -192,6 +192,25 @@ def test_skip_when_token_unset(self): self.assertIn("skipped (TURSO_PLATFORM_API_TOKEN not set)", result.stdout) self.assertEqual(list(self.capture.glob("argv-*.log")), []) + def test_work_dir_is_named_wiped_at_start_and_removed_on_exit(self): + # darwin-nix#373 regression: scratch must live in ONE named + # directory. A previous run killed mid-flight (SIGKILL defeats + # the EXIT trap) leaves its files there; the next run wipes + # them at startup instead of starting a second anonymous + # mktemp dir, and a clean exit removes the directory. + work = Path(self.tmp.name) / "named-work" + work.mkdir() + (work / "stale-from-a-killed-run.sql").write_text("leftover") + env = {**self.env, "EXPORT_TURSO_WORK_DIR": str(work)} + result = self.run_script(env=env) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertIn("all 1 database(s) filed", result.stdout) + # the named location was used (its lock file exists) ... + self.assertTrue(Path(f"{work}.lock").exists()) + # ... and the directory itself is gone after a clean exit, + # stale leftovers included. + self.assertFalse(work.exists()) + BROKEN_SCHEMA_INSERT = ( b"INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)" From 22d5baa828c844d961a654eb2e310c4fcfc6e9ec Mon Sep 17 00:00:00 2001 From: repo-worker-tmp-code-b Date: Wed, 11 Nov 2026 00:33:16 -0600 Subject: [PATCH 2/3] fix(laptop-backups): prune stale atlas-backup staging dirs left by killed runs (darwin-nix#373) --- nixos/home/atlas-backup.sh | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/nixos/home/atlas-backup.sh b/nixos/home/atlas-backup.sh index 8175b751a7..8c4a676b4f 100755 --- a/nixos/home/atlas-backup.sh +++ b/nixos/home/atlas-backup.sh @@ -167,6 +167,16 @@ FREE="$(python3 -c 'import shutil,sys; print(shutil.disk_usage(sys.argv[1]).free STAGE="$HOME/atlas/turso-atlas/data/.backup-staging/$DATE" rm -rf "$STAGE" mkdir -p "$STAGE" +# Bound the staging tree (darwin-nix#373): the EXIT trap removes +# today's STAGE on every handled exit, but a SIGKILLed run (OOM, +# service timeout) leaves its date dir behind and later runs only +# ever touch their own date. This run holds the instance lock, so +# remove every OTHER date's staging dir here: a killed run leaves +# at most one stale dir, reclaimed by the next run. +for stale in "$HOME/atlas/turso-atlas/data/.backup-staging"/*; do + [ -e "$stale" ] || continue + [ "$stale" = "$STAGE" ] || rm -rf "$stale" +done SNAP="$STAGE/atlas-$DATE.db" sqlite3 "$DB" ".backup '$SNAP'" || early_fail "sqlite3 .backup failed" From d3fa454ccc165bb945ebb8af8a65d2da772314e9 Mon Sep 17 00:00:00 2001 From: repo-worker-tmp-code-b Date: Wed, 11 Nov 2026 00:33:17 -0600 Subject: [PATCH 3/3] fix(laptop-backups): cold-store-nightly start timeout 30m -> 120m (darwin-nix#373) --- nixos/home/laptop-backups.nix | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/nixos/home/laptop-backups.nix b/nixos/home/laptop-backups.nix index 07f3978e5d..466ca8610f 100644 --- a/nixos/home/laptop-backups.nix +++ b/nixos/home/laptop-backups.nix @@ -304,7 +304,11 @@ in Service = { Type = "oneshot"; ExecStart = cfg.coldStoreNightly.script; - TimeoutStartSec = "30m"; + # 120m, not 30m: the Turso export leg alone ran past + # 30m on 2026-11-08/09/10, and systemd's timeout kill + # leaked its scratch directory each night + # (darwin-nix#373). + TimeoutStartSec = "120m"; }; }; })