You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Fix unhandled promise rejections and optimize array iterations in cron jobs #303
The tickCronJobs and startCronJobs functions in packages/modelence/src/cron/jobs.ts currently use standard Array.prototype.forEach to loop through cron job records and objects.
In tickCronJobs, this is problematic because it uses an async callback inside a synchronous forEach loop. The forEach loop does not wait for the promises to resolve, meaning the asynchronous execution of runCronJob runs detached from the tick cycle. This can potentially lead to unhandled promise rejections crashing the Node.js process and creates a race condition if multiple intervals tick before the previous jobs finish safely.
Additionally, using forEach across the file introduces minor but unnecessary closure allocation overhead in a fast-running interval.
Proposed Solution:
Refactor the forEach(async (job) => { ... }) loop in tickCronJobs to use await Promise.all(Object.values(cronJobs).map(async (job) => { ... })). This allows concurrent execution of the jobs while properly awaiting all of them before the tick cycle completes.
Replace synchronous forEach loops over arrays and object values in startCronJobs with optimized for...of loops to reduce closure allocations and improve readability.
Thanks for looking at this. Closing — runCronJob already wraps await handler() in a try/catch, so the forEach(async ...) here can't produce the rejection described.
You were close to a real one, though: cronJobsCollection.upsertOne runs before that try block, so a failed write leaves state.isRunning stuck at true, and the job gets skipped until the timeout clears it. Tracking in #338.
Activity
omegascorp commentedon Aug 10, 2026
Thanks for looking at this. Closing — runCronJob already wraps await handler() in a try/catch, so the forEach(async ...) here can't produce the rejection described.
You were close to a real one, though: cronJobsCollection.upsertOne runs before that try block, so a failed write leaves state.isRunning stuck at true, and the job gets skipped until the timeout clears it. Tracking in #338.