fix(jobs): stop JobQueueService leaking unhandled rejections on DB errors#83
Merged
Merged
Conversation
…rors JobQueueService invokes executeJob and pollWorker fire-and-forget from three sites; only pollAll isolated rejections (via allSettled). executeJob caught the job handler's errors but not repo.complete/repo.fail — and pollWorker rejects if repo.claimJob throws. A transient DB failure while finalizing or claiming a job therefore surfaced as an unhandled promise rejection: the gateway's global handler only logs (no crash), but it emitted a misleading "Unhandled Promise Rejection" with no job context. - executeJob now wraps repo.fail in its own try/catch so it never rejects; the caller's finally still frees the slot. - New safePoll() wrapper catches pollWorker rejections at the immediate start poll and the per-job re-poll (pollAll already used allSettled). 4 regression tests: executeJob resolves when complete/fail throw, the slot is freed via finally on persistence failure, and startWorker's immediate poll emits no unhandledRejection when claimJob throws. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This was referenced Jun 5, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Found during a proactive concurrency audit of
JobQueueService.The service invokes
executeJobandpollWorkerfire-and-forget from three callsites, but onlypollAllisolated rejections (viaPromise.allSettled). The gaps:executeJobcaught the job handler's errors butawaitedrepo.complete/repo.failunguarded — both are DB writes that can throw on a transient connection loss.pollWorkerrejects ifrepo.claimJobthrows, and the immediate-start poll (startWorker) and per-job re-poll (.finally) didn't.catchit.So a transient DB failure while finalizing or claiming a job surfaced as an unhandled promise rejection. The gateway's global handler only logs (no crash), but it emitted a misleading
Unhandled Promise Rejectionwith no job/worker context — a false crash signal.That
pollAllalready usedallSettledwhile the sibling callsites didn't is the tell this was an oversight, not intent.Fix
executeJobwrapsrepo.failin its owntry/catch→ it never rejects; the caller'sfinallystill frees the slot.safePoll()wrapper catchespollWorkerrejections at the two bare callsites (immediate start poll + per-job re-poll).pollAllis unchanged.Behavior is otherwise identical — the worker frees the slot and re-polls in every path; this only stops the leaked rejection.
Test plan
4 new regression tests in
job-queue-service.test.ts:executeJobresolves (not rejects) whenrepo.completethrows, and still callsrepo.failfor retry.executeJobresolves whenrepo.completeandrepo.failboth throw.finallywhen the job's persistence throws.startWorker's immediate poll emits nounhandledRejectionwhenclaimJobthrows (process listener assertion).typecheck+eslintclean🤖 Generated with Claude Code