# node-cron finished, but the work is still running

`execution:finished` describes completion of the callback node-cron can observe. It cannot wait for an asynchronous operation that your callback starts without returning or awaiting its promise.

This local lab compares five cases on **node-cron 4.6.0**. Every case uses real one-second scheduled occurrences and `noOverlap: true`. No monitoring account, database or network service is needed during execution. Dependency installation needs npm access.

## Run the comparison

Use Node.js 24.14.1 to match the recorded runtime. The pinned dependency declares Node.js 20 or later, but that is not a claim that this lab was tested on every supported release.

```sh
npm ci --ignore-scripts --no-audit --no-fund
npm run lab
```

The five scenarios take roughly 35 to 40 seconds in total. The script writes a timestamped JSON file under `results/`, including runtime and dependency versions, source hashes, event traces, scenario summaries and the pass/fail outcome. It exits nonzero when an assertion fails.

## The missing return

This callback returns `undefined` as soon as it launches the work:

```js
cron.schedule('* * * * * *', () => {
  void syncOrders().catch(reportWorkError);
}, { noOverlap: true });
```

The error handler avoids an unhandled rejection if it does not itself throw or reject, but does not connect the work's lifetime to the scheduler. `noOverlap` cannot prevent overlap of work detached in this way. `execution:finished` may arrive before `syncOrders()` resolves. A later work rejection is handled by `reportWorkError`, rather than being a rejection of the scheduled callback.

Return the promise so the scheduler can wait for the work and observe its rejection:

```js
cron.schedule('* * * * * *', () => syncOrders(), {
  noOverlap: true,
});
```

Or use an async callback that awaits all required work and validation:

```js
cron.schedule('* * * * * *', async () => {
  const result = await syncOrders();
  await validateResult(result);
  return result;
}, { noOverlap: true });
```

`syncOrders`, `validateResult` and `reportWorkError` stand for your application functions. The executable lab uses synthetic promises instead. Returning a parent promise is insufficient if that function also detaches required child work.

## What the comparison tests

| Case | Work | Assertion |
| --- | --- | --- |
| Baseline | Returns a short successful promise | Multiple scheduled completions, at most one active workload |
| Reject once | First returned promise rejects; later ones resolve | One execution failure followed by successful scheduled completions |
| Detached | Starts 2.3-second work without returning it | Scheduler completion precedes work completion and active workloads overlap |
| Returned | Returns the same 2.3-second work | At most one active workload; intervening occurrences are skipped for overlap |
| Pending | Returns a deliberately unresolved promise | One started workload, overlap skips and no completion during observation |

Each case has a 6.5-second observation period. Exact event counts depend on alignment with the next second and local scheduling delay. They are not a throughput benchmark. A busy machine or suspended process can miss occurrences or fail the timing assertions. Inspect the raw trace rather than interpreting such a failure as a node-cron defect.

The lab registers event listeners before starting tasks. Events carry scheduler execution IDs; application work records the corresponding ID separately. Every tested execution must have reason `scheduled`. No call to `task.execute()` substitutes for a scheduled occurrence.

At the observation boundary, the lab stops future scheduling, then releases the pending fixture and drains in-flight work. Cleanup events remain in the raw trace but are excluded from the scenario result. **Stopping scheduling does not cancel application work already in progress.** The pending case never resolves naturally during its observation window.

## Apply the distinction to a real job

1. Record both scheduler lifecycle events and the application's validated completion, with a shared execution identifier.
2. Check that the scheduled callback returns the entire required work chain. Look inside helpers for detached promises and asynchronous `forEach` callbacks too.
3. Distinguish a rejected execution from a pending one. In this lab a rejection does not stop later occurrences; a pending returned promise causes overlap skips.
4. Set workload deadlines using cancellation that the underlying operation actually supports. Racing a timer against a promise alone does not cancel its side effects.
5. Treat `execution:finished` as a boundary observation, not proof that business output is correct. Required validation must be part of the returned chain.

A completion heartbeat belongs after the work and its required validation. An external monitor such as [PingCron](https://www.pingcron.io/node-cron-monitoring) can detect a missing expected receipt when the process stops or work does not reach that point. The absence of a receipt does not tell you why it is missing, and receipt does not independently validate your output. This lab works without PingCron.

## Scope and sources

This is a synthetic, single-process promise-lifecycle experiment, not a customer incident or a test of database writes, distributed coordination, durable retries, crash recovery or production performance. It demonstrates callback usage, not a library bug.

- [node-cron events and observability](https://www.nodecron.com/event-listening.html)
- [node-cron scheduling options](https://www.nodecron.com/scheduling-options.html)
- [node-cron 4.6.0 release](https://github.com/node-cron/node-cron/releases/tag/v4.6.0)

Prepared by PingCron with AI assistance. The claims above are intended to be checked against the included fixture and its recorded results.
