fix(agenda): correct off-by-one error in returnNextConcurrencyFreeJob#39830
fix(agenda): correct off-by-one error in returnNextConcurrencyFreeJob#39830Jashk120 wants to merge 2 commits into
Conversation
|
Looks like this PR is not ready to merge, because of the following issues:
Please fix the issues and try again If you have any trouble, please check the PR guidelines |
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (1)
📜 Recent review details🧰 Additional context used📓 Path-based instructions (1)**/*.{ts,tsx,js}📄 CodeRabbit inference engine (.cursor/rules/playwright.mdc)
Files:
🧠 Learnings (3)📓 Common learnings📚 Learning: 2026-02-26T19:25:44.063ZApplied to files:
📚 Learning: 2026-02-26T19:25:44.063ZApplied to files:
🔇 Additional comments (1)
WalkthroughChanged Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Suggested labels
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
09b06e5 to
ed6ca9a
Compare
There was a problem hiding this comment.
1 issue found across 1 file
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="packages/agenda/src/JobProcessingQueue.ts">
<violation number="1" location="packages/agenda/src/JobProcessingQueue.ts:63">
P1: `returnNextConcurrencyFreeJob` can return `undefined` (when all jobs are concurrency-blocked) but is typed as `Job`, and callers dereference it unguarded.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/agenda/src/JobProcessingQueue.ts (1)
61-71:⚠️ Potential issue | 🔴 CriticalReturn type mismatch and missing null check cause NPE at call site.
The method's return type
Jobdoesn't account for the scenario where no job with available concurrency is found. When the loop completes without breaking (all jobs at concurrency limit),nextbecomes-1, andthis._queue[-1]returnsundefined. The call site atAgenda.ts:909accessesjob.attrs.nextRunAtwithout checking ifjobis defined, resulting in a crash.
- Update return type to
Job | undefined- Add guard:
if (next < 0) return undefined;- Update call site to handle
undefined:if (!job || !job.attrs.nextRunAt) return;🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/agenda/src/JobProcessingQueue.ts` around lines 61 - 71, The method returnNextConcurrencyFreeJob currently assumes a Job is always found; change its signature to return Job | undefined, add a guard after the loop (if next < 0) return undefined to avoid indexing this._queue[-1], and keep locating the method by name returnNextConcurrencyFreeJob and the _queue/agendaDefinitions usage; then update the call site that uses its result (the variable named job which later reads job.attrs.nextRunAt) to handle undefined (e.g., if (!job || !job.attrs.nextRunAt) return) so callers won’t dereference undefined.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In `@packages/agenda/src/JobProcessingQueue.ts`:
- Around line 61-71: The method returnNextConcurrencyFreeJob currently assumes a
Job is always found; change its signature to return Job | undefined, add a guard
after the loop (if next < 0) return undefined to avoid indexing this._queue[-1],
and keep locating the method by name returnNextConcurrencyFreeJob and the
_queue/agendaDefinitions usage; then update the call site that uses its result
(the variable named job which later reads job.attrs.nextRunAt) to handle
undefined (e.g., if (!job || !job.attrs.nextRunAt) return) so callers won’t
dereference undefined.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 597bc627-d2d8-48a0-970b-c40d43c96ee0
📒 Files selected for processing (1)
packages/agenda/src/JobProcessingQueue.ts
📜 Review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: cubic · AI code reviewer
🧰 Additional context used
📓 Path-based instructions (1)
**/*.{ts,tsx,js}
📄 CodeRabbit inference engine (.cursor/rules/playwright.mdc)
**/*.{ts,tsx,js}: Write concise, technical TypeScript/JavaScript with accurate typing in Playwright tests
Avoid code comments in the implementation
Files:
packages/agenda/src/JobProcessingQueue.ts
🧠 Learnings (3)
📓 Common learnings
Learnt from: smirk-dev
Repo: RocketChat/Rocket.Chat PR: 39625
File: apps/meteor/app/api/server/v1/push.ts:85-97
Timestamp: 2026-03-14T14:58:58.834Z
Learning: In RocketChat/Rocket.Chat, the `push.token` POST/DELETE endpoints in `apps/meteor/app/api/server/v1/push.ts` were already migrated to the chained router API pattern on `develop` prior to PR `#39625`. `cleanTokenResult` (which strips `authToken` and returns `PushTokenResult`) and `isPushTokenPOSTProps`/`isPushTokenDELETEProps` validators already exist on `develop`. PR `#39625` only migrates `push.get` and `push.info` to the chained pattern. Do not flag `cleanTokenResult` or `PushTokenResult` as newly introduced behavior-breaking changes when reviewing this PR.
📚 Learning: 2026-02-26T19:25:44.063Z
Learnt from: gabriellsh
Repo: RocketChat/Rocket.Chat PR: 38778
File: packages/ui-voip/src/providers/useMediaSession.ts:192-192
Timestamp: 2026-02-26T19:25:44.063Z
Learning: In the Rocket.Chat repository, do not reference Biome lint rules in code review feedback. Biome is not used even if biome.json exists; only reference Biome rules if there is explicit, project-wide usage documented. For TypeScript files, review lint implications without Biome guidance unless the project enables Biome rules.
Applied to files:
packages/agenda/src/JobProcessingQueue.ts
📚 Learning: 2026-02-26T19:25:44.063Z
Learnt from: gabriellsh
Repo: RocketChat/Rocket.Chat PR: 38778
File: packages/ui-voip/src/providers/useMediaSession.ts:192-192
Timestamp: 2026-02-26T19:25:44.063Z
Learning: In this repository (RocketChat/Rocket.Chat), Biome lint rules are not used even if a biome.json exists. When reviewing TypeScript files (e.g., packages/ui-voip/src/providers/useMediaSession.ts), ensure lint suggestions do not reference Biome-specific rules. Rely on general ESLint/TypeScript lint rules and project conventions instead.
Applied to files:
packages/agenda/src/JobProcessingQueue.ts
|
Just let me know how I can assist! 😊 🧠 Learnings used✅ Actions performedReview triggered.
|
|
@Jashk120 I have started the AI code review. It will take a few minutes to complete. |
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
Proposed changes
returnNextConcurrencyFreeJobinJobProcessingQueue.tsusednext > 0as the loop condition, which caused index 0 to never be evaluated. If the only concurrency-free job happened to be at position 0 in the queue, the loop would exit without finding it. In a scenario where all jobs except the one at index 0 are at their concurrency limit, the wrong job would be returned.Fixed by changing
next > 0tonext >= 0so all queue positions are evaluated.Issue(s)
No existing issue — bug found by code review.
Steps to test or reproduce
returnNextConcurrencyFreeJob— before fix it skips index 0 and returns the wrong jobSummary by CodeRabbit