Skip to content

Commit 0202e68

Browse files
fix(lock): fix prune lock in db
1 parent a4ed105 commit 0202e68

4 files changed

Lines changed: 20 additions & 1 deletion

File tree

osmsg/_tick.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ def _rebuild_store_from_pg(db_path: Path, dsn: str) -> None:
107107
conn.close()
108108

109109

110+
_TICK_TIMEOUT_SECONDS = int(os.environ.get("OSMSG_TICK_TIMEOUT_SECONDS", "1200"))
111+
112+
110113
def main() -> int:
111114
extra_args = shlex.split(os.environ.get("OSMSG_EXTRA_ARGS", ""))
112115
bootstrap_days = os.environ.get("OSMSG_BOOTSTRAP_DAYS", "1")
@@ -154,7 +157,11 @@ def main() -> int:
154157
cmd.extend(["--days", bootstrap_days])
155158

156159
print(f"[osmsg-tick] {' '.join(cmd)}", flush=True)
157-
rc = subprocess.call(cmd)
160+
try:
161+
rc = subprocess.call(cmd, timeout=_TICK_TIMEOUT_SECONDS)
162+
except subprocess.TimeoutExpired:
163+
print(f"[osmsg-tick] killed: exceeded {_TICK_TIMEOUT_SECONDS}s", flush=True)
164+
rc = 1
158165
# With a psql push, Postgres is the permanent copy and the store is a per-tick delta buffer: clear
159166
# its data (keeping resume `state`) after a successful push so the next push stays small and fast.
160167
if rc == 0 and psql_dsn:

osmsg/export/psql.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ def to_psql(conn: duckdb.DuckDBPyConnection, dsn: str, *, bulk_load: bool = Fals
177177
conn.execute("INSTALL spatial")
178178
conn.execute("LOAD spatial")
179179
attach_postgres(conn, dsn, alias="pg_target")
180+
pg_execute(conn, "SET lock_timeout = '120s'", alias="pg_target")
180181
try:
181182
# Create the tags composite type as the first PG op (a fresh write txn; a prior read would pin the
182183
# connection read-only). On re-push it already exists (the idempotent case); anything else is real.

osmsg/prune.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ def prune_pg(dsn: str, cutoff: dt.datetime) -> tuple[int, int]:
2929

3030
if cs_n:
3131
writer = connect_postgres(dsn)
32+
pg_execute(writer, "SET lock_timeout = '120s'")
33+
pg_execute(writer, "SET statement_timeout = '1800s'")
3234
pg_execute(
3335
writer,
3436
f"DELETE FROM changeset_stats s USING changesets c WHERE s.changeset_id = c.changeset_id AND c.{older}",

tests/test_tick.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ def test_cold_start_bootstraps_at_day(tmp_path, monkeypatch, captured_cmd, clean
8888
assert cmd[-2:] == ["--days", "1"] # default cold-start window
8989

9090

91+
def test_tick_watchdog_returns_nonzero_when_run_times_out(tmp_path, monkeypatch, clean_env):
92+
def slow_call(cmd, *args, **kwargs):
93+
raise _tick.subprocess.TimeoutExpired(cmd, kwargs.get("timeout", 0))
94+
95+
monkeypatch.setattr(_tick.subprocess, "call", slow_call)
96+
monkeypatch.setenv("OSMSG_EXTRA_ARGS", f"--name stats --output-dir {tmp_path}")
97+
assert _tick.main() == 1
98+
99+
91100
def test_planet_continues_seeded_source(tmp_path, monkeypatch, captured_cmd, clean_env):
92101
"""A `--insert --seed-only` seeds the store's resume source; the planet tick must --update off that
93102
seed, not re-bootstrap."""

0 commit comments

Comments
 (0)