Skip to content

fix: idle restart no migratios#874

Merged
johnyeocx merged 1 commit intomainfrom
fix/idle-restart-no-migratios
Mar 3, 2026
Merged

fix: idle restart no migratios#874
johnyeocx merged 1 commit intomainfrom
fix/idle-restart-no-migratios

Conversation

@johnyeocx
Copy link
Collaborator

@johnyeocx johnyeocx commented Mar 3, 2026

Summary by cubic

Prevent idle self-kill restarts while migration jobs are running to avoid interrupting migrations. We now track active migration jobs and only recycle the worker when idle and no migrations are active.

Written for commit beaa71d. Summary will update on new commits.

Greptile Summary

This PR fixes a race condition where the SQS worker's idle self-kill logic could terminate the process while long-running migration jobs were still executing in the background.

Key Changes:

  • Bug fixes — Introduces an activeMigrationJobs counter that is incremented before dispatching a migration job as fire-and-forget, and decremented in .finally() after it settles. The logStatsAndCheckZeroMessages idle self-kill condition now additionally requires activeMigrationJobs === 0 before calling process.exit(0), preventing premature worker termination during in-flight migrations.

Confidence Score: 5/5

  • The fix is minimal, correct, and directly addresses the described race condition with no side effects.
  • The counter increment/decrement pattern is sound in a single-threaded JS/Bun runtime. The .finally() block guarantees the decrement runs regardless of success or failure. The added condition at the idle self-kill check precisely targets the problematic code path where migrations could be killed mid-flight. The implementation is straightforward with no edge cases in the core fix.
  • No files require special attention

Sequence Diagram

sequenceDiagram
    participant SQS
    participant PollLoop as Polling Loop
    participant Counter as activeMigrationJobs
    participant Handler as handleSingleMessage
    participant Stats as logStatsAndCheckZeroMessages

    SQS->>PollLoop: Message received (JobName.Migration)
    PollLoop->>Counter: activeMigrationJobs++ (now > 0)
    PollLoop->>Handler: fire-and-forget (no await)
    PollLoop->>SQS: continue polling

    Note over Stats: setInterval (every 60s)
    Stats->>Stats: messagesProcessed === 0?
    Stats->>Counter: activeMigrationJobs === 0?
    alt activeMigrationJobs > 0
        Stats-->>Stats: Skip idle self-kill (migration running)
    else activeMigrationJobs === 0
        Stats-->>Stats: process.exit(0) if idle threshold met
    end

    Handler-->>Handler: processMessage completes
    Handler->>Counter: activeMigrationJobs-- via .finally()
Loading

Last reviewed commit: beaa71d

@johnyeocx johnyeocx requested a review from ay-rod as a code owner March 3, 2026 17:56
@vercel
Copy link

vercel bot commented Mar 3, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
autumn-vite Ready Ready Preview, Comment Mar 3, 2026 5:57pm

Request Review

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 1 file

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

@johnyeocx johnyeocx merged commit 070d278 into main Mar 3, 2026
9 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant