Skip to content

fix(bot): Phase 2.4 SIGTERM listener + connection cleanup - #674

Closed
LucasSantana-Dev wants to merge 1 commit into
mainfrom
fix/phase-2-4-sigterm-cleanup
Closed

LucasSantana-Dev wants to merge 1 commit into
mainfrom
fix/phase-2-4-sigterm-cleanup

Conversation

@LucasSantana-Dev

@LucasSantana-Dev LucasSantana-Dev commented Apr 17, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • Add SIGTERM/SIGINT signal handler at bot bootstrap to enable graceful shutdown
  • Enrich shutdown method to remove all Discord listeners and disconnect Redis
  • Prevents listener accumulation on hot-reload and ensures clean exit on graceful deploy

Files Changed

  • packages/bot/src/index.ts: Add signal handler registration in main()
  • packages/bot/src/bot/start/initializer.ts: Enhance shutdown() to call removeAllListeners() and disconnect Redis
  • packages/bot/src/index.spec.ts: Add tests for SIGTERM/SIGINT signal handling

Smoke Test

# Deploy bot
docker start lucky-bot

# Send graceful shutdown signal
docker stop lucky-bot  # Sends SIGTERM

# Verify no listener leak or hanging connections in logs
docker logs lucky-bot | grep -E "(SIGTERM|shutdown|listener|disconnect)"

All listeners are now properly cleaned up on process termination.

Summary by CodeRabbit

Release Notes

  • New Features

    • Graceful shutdown: Bot now properly handles system termination signals, ensuring all event listeners and external connections are cleanly disconnected before shutdown.
  • Tests

    • Added comprehensive test coverage for signal-based shutdown and error handling during termination.

@vercel

vercel Bot commented Apr 17, 2026 •

Copy link
Copy Markdown
Contributor

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

Project Deployment Actions Updated (UTC)
lucky Ready Ready Preview, Comment Apr 17, 2026 1:50am

Request Review

@LucasSantana-Dev
LucasSantana-Dev enabled auto-merge (squash) April 17, 2026 01:48
@coderabbitai

coderabbitai Bot commented Apr 17, 2026 •

Copy link
Copy Markdown

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

The changes implement graceful shutdown for the bot. The BotInitializer.shutdown() method now logs a startup message, removes event listeners, and disconnects Redis. The entry point adds signal handlers for SIGTERM and SIGINT that trigger shutdown, flush Sentry, and exit with appropriate status codes. Tests verify this shutdown flow.

Changes

Cohort / File(s) Summary
Graceful Shutdown Implementation
packages/bot/src/bot/start/initializer.ts
Extended shutdown() to log a message, remove all event listeners via removeAllListeners(), and disconnect Redis after client destruction.
Signal Handler Setup
packages/bot/src/index.ts
Added setupSignalHandlers() function that registers listeners for SIGTERM and SIGINT signals, triggering graceful shutdown, Sentry flush, and process exit with appropriate status codes (0 for success, 1 for errors).
Shutdown Flow Tests
packages/bot/src/index.spec.ts
Added three test cases: verification of signal handler registration and graceful shutdown for both SIGTERM and SIGINT, and error handling when shutdown fails during signal processing.

Sequence Diagram(s)

sequenceDiagram
    participant Process as Bot Process
    participant Handler as Signal Handler
    participant Bot as BotInitializer
    participant Redis as Redis Client
    participant Sentry as Sentry

    Process->>Handler: SIGTERM/SIGINT received
    Handler->>Handler: Log graceful shutdown start
    Handler->>Bot: await shutdown()
    Bot->>Bot: Log shutdown message
    Bot->>Bot: removeAllListeners()
    Bot->>Bot: destroy client
    Bot->>Redis: disconnect()
    Bot->>Bot: null client/state
    Bot->>Bot: Log shutdown complete
    Handler->>Sentry: flushSentry(3000)
    Handler->>Process: exit(0)
    
    alt Error during shutdown
        Bot->>Handler: Error thrown
        Handler->>Handler: Log error
        Handler->>Process: exit(1)
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Suggested labels

bot, size/m, infra

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main changes: adding SIGTERM/SIGINT signal handlers and enhancing connection cleanup (Discord listeners removal and Redis disconnection).

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/phase-2-4-sigterm-cleanup

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@LucasSantana-Dev

Copy link
Copy Markdown
Owner Author

Superseded by consolidated #676 (Phase 2 memory hygiene bundle).

auto-merge was automatically disabled April 17, 2026 01:52

Pull request was closed

@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
0.0% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

This branch was successfully deployed

1 active deployment
Preview — 054b78e0 Deployed Apr 17, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant