Skip to content

Stop a scheduled run from overlapping itself - #11

Merged
Gabriel-Lewis merged 1 commit into
devfrom
fix/cron-overlap
Aug 30, 2026
Merged

Gabriel-Lewis merged 1 commit into
devfrom
fix/cron-overlap

Conversation

@Gabriel-Lewis

Copy link
Copy Markdown
Owner

Audit finding 6.

The bug

crond starts a job on schedule whether or not the previous one finished, and a run can outlast its own interval — downloads are slow, and #9 caps monitoring alone at two hours. Two copies of the same job then race on the same playlist.

This is the mirror image of finding 5: the web UI has an "already running" guard, but cron never touched it, since docker/start.sh execs the binary directly.

The fix

Scheduled runs go through docker/explo-run.sh, which holds a lock for the duration. A second start stands down with a log line and exits 0 — a skipped schedule isn't an error, and cron would otherwise complain every time.

The lock is per job, not global. A long weekly-exploration must not cancel weekly-jams: silently skipping a different playlist for the week is worse than letting two runs overlap, and what the guard exists to prevent is a job racing itself. Where flock is missing it runs unlocked and says so, for the same reason.

The lock is held on a file descriptor rather than passed as flock FILE -c PROG, which keeps the exit status unambiguous — flock can then only fail because the lock is held, never because the job did. That matters because explo itself exits 1 on a failed run.

Testing

docker/explo-run_test.sh covers: running when free, a second copy of the same job skipping, a skip exiting 0, a different job not being blocked, the lock releasing when the run ends, and the command's exit status passing through. All six pass.

Caveat on how I ran them. There's no shell harness in CI (the pipeline is golangci-lint only), and this machine has neither flock nor a Docker daemon, so I couldn't execute against busybox's real binary. I ran the suite against a stand-in implementing busybox's flock [-n] FD form via fcntl.flock, so the contention is genuine OS advisory locking — what's untested is busybox's exact CLI parsing of that form, not the script's logic. The fallback branch was separately confirmed by running with no flock on PATH at all.

Both scripts pass sh -n, and I checked the generated crontab line renders as intended.

🤖 Generated with Claude Code

crond starts a job on schedule whether or not the previous one finished,
and a run can easily outlast its own interval -- downloads are slow.
Two copies of the same job then race on the same playlist.

Scheduled runs go through a wrapper that holds a lock for the duration,
so a second start stands down and says so. It exits 0, since a skipped
schedule is not an error and cron would otherwise complain every time.

The lock is per job rather than global. A long weekly-exploration must
not cancel weekly-jams: skipping a different playlist entirely is worse
than letting the two overlap, and the runs the guard exists to prevent
are a job racing itself.

Where flock is missing the wrapper runs the job unlocked and says so,
because an overlapping run is a lesser problem than a playlist that
never gets built.

The web UI's own guard is unaffected; it never applied to cron, which
execs the binary directly.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@Gabriel-Lewis
Gabriel-Lewis merged commit 138e331 into dev Aug 30, 2026
4 checks passed
@Gabriel-Lewis
Gabriel-Lewis deleted the fix/cron-overlap branch October 19, 2026 21:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant