#5166·vendure

preventOverlap cannot protect a task that runs longer than its own schedule interval

Author: juangadielCreated Aug 23, 2026Updated Sep 23, 2026
Labelstype: bug 🐛

Describe the bug

Two independent mechanisms both fail to prevent a ScheduledTask from running concurrently with itself once a single execution takes longer than the task's own schedule interval.

Mechanism 1 — preventOverlap's in-process guard never sees an in-flight run.

SchedulerService.createCronJob (packages/core/src/scheduler/scheduler.service.ts) passes croner a non-async callback:

javascript
const job = new Cron(schedule, {
    name: task.id,
    protect: task.options.preventOverlap ? protectCallback : undefined,
}, () => {
    if (this.shouldRunTasks) {
        this.runningTasks++;
        try {
            schedulerStrategy.executeTask(task)(job);   // not awaited
        } catch (e) { ... }
        finally {
            this.runningTasks--;                        // decremented before executeTask resolves
        }
    }
});

schedulerStrategy.executeTask(task)(job) returns a promise (it calls runTask, an async method) but that promise is never awaited here. Because the callback itself is synchronous and returns almost immediately, croner's own currentRun() — the thing protect's callback (protectCallback) checks to decide whether to warn about/block an overlapping invocation — is already back to null/finished by the time the next tick fires, regardless of whether the task's actual async work is still running. preventOverlap: true therefore does not prevent overlapping executions at the croner level at all.

Mechanism 2 — the DB lock's staleness threshold is shorter than a legitimately long task.

The remaining protection is the DB row lock acquired in DefaultSchedulerStrategy.runTask (tryAcquireLock) and released on completion. StaleTaskService.isStale (packages/core/src/plugin/default-scheduler-plugin/stale-task.service.ts) exists to reclaim a lock abandoned by a crashed worker:

javascript
isStale(task, now, intervalMs) {
    if (!task.lockedAt) {
        return false;
    }
    return now.getTime() - task.lockedAt.getTime() > intervalMs;
}

intervalMs is getScheduleIntervalMs(task) — the time between one scheduled run and the next, derived from the task's own cron schedule, not from its timeout. Any task that legitimately runs longer than its own schedule interval (e.g. a 5-minute-interval task whose single execution can take up to 16 minutes) has its lock treated as stale and cleared while it is still genuinely running, and the very next tick acquires it — a second, fully concurrent execution starts on top of the first.

Both mechanisms fail for the same class of task: one whose single run can legitimately exceed its own schedule interval. preventOverlap: true — the documented, opt-in protection for exactly this — provides no actual protection in that case.

To Reproduce

  1. Register a ScheduledTask with preventOverlap: true and a schedule (e.g. every 5 minutes) shorter than its worst-case execution time (e.g. up to 16 minutes).
  2. Trigger a run that takes longer than the schedule interval.
  3. Observe: protectCallback never logs its "blocked because an existing task is still running" warning (mechanism 1), and once intervalMs has elapsed, StaleTaskService clears the DB lock and the next tick starts a second concurrent execution (mechanism 2).

Expected behavior

preventOverlap: true should prevent a second concurrent execution of the same task under all circumstances, including a run that exceeds the task's own schedule interval. If a DB lock is the backing mechanism, its staleness threshold should be derived from the task's timeout (the longest a legitimate run is expected to take), not from the schedule interval (how often it is supposed to start).

Actual behavior

Neither mechanism prevents the overlap once a run exceeds the schedule interval; the task can end up running many overlapping instances at once, each holding its own resources (DB connections, in-memory polling loops, etc.).

Screenshots/Videos

N/A — this is a background scheduled-task bug, not a UI issue.

Error logs

No error is logged for the overlap itself — that is the bug. The only visible symptom is the downstream cost of the overlapping work (see "Additional context").

Environment (please complete the following information):

  • @vendure/core version: 3.7.2
  • Nodejs version: 24 (node:24-alpine); engines requires >=22
  • Database (mysql/postgres etc): PostgreSQL 17
  • Operating System (Windows/macOS/Linux): Linux (Alpine, Docker) in production; Windows 11 for development
  • Browser (if applicable): N/A — worker process, no browser involved
  • Package manager (npm/yarn/pnpm): npm (npm workspaces monorepo)

Configuration

A custom ScheduledTask (application code, not core) registered via config.schedulerOptions.tasks, scheduled every 5 minutes, whose execution can take up to ~16 minutes in the worst case. preventOverlap was not set on this particular task (its absence is itself notable: even setting it would not have helped, per mechanism 1/2 above).

Minimal reproduction

Not attached as a runnable repo — reproducible directly from the excerpted source: any ScheduledTask whose real execution time can exceed its schedule interval demonstrates both mechanisms failing, with or without preventOverlap: true set.

Workaround

Add an in-process re-entrancy guard inside the task's own service — hold the in-flight promise in a field and have concurrent callers join it rather than starting a new run — since neither of core's own overlap-prevention mechanisms can be relied on for a task whose legitimate runtime can exceed its schedule interval. See apps/shop-api/src/plugins/search-index-autoflush/services/search-index-autoflush.service.ts in this repository for the exact implementation.

Additional context

Found alongside "Scheduled task timeout releases the lock but leaves the task running, allowing unbounded overlapping executions" while investigating a production incident: a task that both (a) timed out and kept running in the background, and (b) had no effective overlap protection, ended up with multiple overlapping executions polling the same expensive collection-filter queries against a memory-capped database, which contributed to a separate, more severe outage — see "A failed job_record update permanently wedges a JobQueue with concurrency 1".