Scheduled task timeout releases the lock but leaves the task running, allowing unbounded overlapping executions
Describe the bug
DefaultSchedulerStrategy.runTask (packages/core/src/plugin/default-scheduler-plugin/default-scheduler-strategy.ts)
races a scheduled task's execute() against a timeout with Promise.race:
const timeoutPromise = new Promise((_, reject) => {
timeoutTimer = setTimeout(() => {
Logger.warn(`Scheduled task ${task.id} timed out after ${timeoutMs}ms`);
reject(new Error('Task timed out'));
}, timeoutMs);
});
const result = await Promise.race([task.execute(this.injector), timeoutPromise]);On timeout, the catch block writes lockedAt: null and returns — releasing the task's lock as
if it had finished. But task.execute(this.injector)'s promise is never cancelled: it keeps
running in the background, holding whatever DB connections or other resources it acquired. Because
the lock was already released, the next scheduled tick can acquire it and start a second,
fully concurrent execution of the same task while the first is still running.
To Reproduce
- Register a
ScheduledTaskwhoseexecute()can legitimately take longer thanDefaultSchedulerPlugin'sdefaultTimeout(60s) — no explicittimeoutoverride on the task. - Have
execute()do real, slow work — in the reproduction below, poll something for up to 15 minutes. - Let the task run past the 60s default timeout.
- Observe the warn/error log pair (
timed out after 60000ms/failed with error: Task timed out), the lock released, and — once the next cron tick fires — a second execution of the same task starting while the first is still running.
Expected behavior
Either the task's own execution is genuinely cancelled when it times out, or the lock is held until the underlying promise actually settles, so a timeout can never produce two concurrent executions of the same task.
Actual behavior
The lock is released immediately on timeout while the real execution continues detached. Nothing tracks or bounds how many of these detached executions can pile up.
Real-world trigger
Core's own SearchJobBufferService.runPendingSearchUpdates()
(packages/core/src/plugin/default-search-plugin/search-job-buffer/search-job-buffer.service.ts)
polls just-released apply-collection-filters jobs to completion with:
await forkJoin(...subscribableCollectionJobs.map(sj => sj.updates({ pollInterval: 500, timeoutMs: 15 * 60 * 1000 })))— i.e. up to 15 minutes. Any ScheduledTask that calls runPendingSearchUpdates() without setting
its own timeout above 15 minutes is therefore guaranteed to hit DEFAULT_TIMEOUT (60s) on any
run that takes longer than a minute, and is guaranteed to eventually overlap itself.
Screenshots/Videos
N/A — this is a background scheduled-task bug, not a UI issue.
Error logs
Scheduled task flush-search-index-buffer timed out after 60000ms
Scheduled task "flush-search-index-buffer" failed with error: Task timed outThese two lines repeated every ~5 minutes in production, each one a fresh overlapping execution piling 500ms-interval polling queries on top of whatever the previous (still-running) execution was already doing against a memory-capped database.
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
DefaultSchedulerPlugin.init(), // defaultTimeout left at its 60_000ms default
DefaultSearchPlugin.init({ bufferUpdates: true, indexStockStatus: true }),A custom ScheduledTask (application code, not core) with no timeout option set, whose
execute() calls into SearchJobBufferService.runPendingSearchUpdates() (indirectly, via an
application service).
Minimal reproduction
Not attached as a runnable repo — reproducible directly from the excerpted source: register any
ScheduledTask whose execute() awaits a promise that resolves after longer than
defaultTimeout, and observe the lock-release/still-running split described above.
Workaround
Set an explicit timeout on the task, above the real worst-case duration of the work it waits on,
so the timeout essentially never fires under normal conditions. Additionally guard re-entrancy
inside the task's own service with an in-process in-flight-promise field, since the DB-level lock
(StaleTaskService) is a separate, independently insufficient mechanism for a task that legitimately
runs longer than its own schedule interval — see the companion issue "preventOverlap cannot protect
a task that runs longer than its own schedule interval". See
apps/shop-api/src/plugins/search-index-autoflush/config/search-index-flush-task.ts and
apps/shop-api/src/plugins/search-index-autoflush/services/search-index-autoflush.service.ts in
this repository for the exact implementation.
Additional context
Found while investigating a production incident where a search-index-flush scheduled task
overlapped itself repeatedly over more than an hour, each overlapping execution adding its own
500ms-interval polling load against a database that has previously OOMed on the exact queries
being polled (collection-filter application). The resulting DB congestion is what caused an
unrelated job-queue UPDATE to exceed its query_timeout, which in turn triggered a separate,
more severe bug — see "A failed job_record update permanently wedges a JobQueue with concurrency 1".
Source: vendurehq/vendure