Skip to content

Conversation

@creativeprojects
Copy link
Owner

Stop running other profiles in group after receiving interrupt signal

This PR improves signal handling for profile groups by ensuring that interrupt signals (Ctrl+C, SIGTERM, SIGABRT) properly stop the execution of remaining profiles in a group.

Changes

  • Moved signal handling up: Signal catching is now set up in startProfileOrGroup() instead of individual runProfile() calls
  • Added context-based interruption: Uses signal.NotifyContext() to create a Go context that gets canceled on interrupt signals
  • Group execution safety: Added check for goCtx.Err() in the group profile loop to detect interruption and stop processing remaining profiles
  • Unified signal handling: Both single profiles and profile groups now share the same signal handling mechanism

Behavior

  • When running a single profile: Interrupt signals stop execution immediately (existing behavior)
  • When running a profile group: Interrupt signals stop the current profile and prevent execution of remaining profiles in the group
  • Graceful interruption logging: Shows warning message when group execution is interrupted

Impact

This ensures that users can cleanly interrupt long-running profile groups without having to wait for all profiles to complete, improving the user experience when managing backup operations.

Fixes #535

@coderabbitai
Copy link

coderabbitai bot commented Jul 28, 2025

Walkthrough

The signal handling for interrupt signals (SIGINT, SIGTERM, SIGABRT) was moved from the runProfile function to the higher-level startProfileOrGroup function. Signal notification and context cancellation are now managed at this higher level, and group execution is interrupted if a signal is received. Notification calls were also centralised.

Changes

Cohort / File(s) Change Summary
Signal Handling Refactor
run_profile.go
Moved signal handling from runProfile to startProfileOrGroup. Introduced context-based interruption for groups. Centralised notification calls. Removed redundant signal handling from runProfile.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant startProfileOrGroup
    participant goCtx (signal.NotifyContext)
    participant runProfile

    User->>startProfileOrGroup: Start group/profile
    startProfileOrGroup->>goCtx: Set up signal context (SIGINT, SIGTERM, SIGABRT)
    startProfileOrGroup->>startProfileOrGroup: notifyStart()
    alt Running group
        loop For each profile
            startProfileOrGroup->>goCtx: Check for cancellation
            alt goCtx cancelled
                startProfileOrGroup->>startProfileOrGroup: Log warning, exit loop
            else not cancelled
                startProfileOrGroup->>runProfile: Run profile
            end
        end
    else Running single profile
        startProfileOrGroup->>runProfile: Run profile
    end
    startProfileOrGroup->>startProfileOrGroup: notifyStop()
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~15 minutes

Assessment against linked issues

Objective Addressed Explanation
Properly handle SIGINT and SIGTERM to gracefully cancel all operations and exit (#535)

Possibly related PRs


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between adf90cb and 560bd93.

📒 Files selected for processing (1)
  • run_profile.go (3 hunks)
🔇 Additional comments (5)
run_profile.go (5)

4-4: Import addition looks good.

The context package import is necessary for the new signal handling implementation using signal.NotifyContext.


42-44: Good consolidation of notification calls.

Moving notifyStart() and notifyStop() outside the conditional blocks ensures consistent notification behaviour for both single profiles and profile groups. The deferred cleanup is properly placed.


139-210: Good architectural decision to move signal handling up.

Moving signal handling from runProfile to startProfileOrGroup is the right approach. This ensures that interrupt signals are handled consistently at the appropriate level, enabling proper group interruption while maintaining the same behaviour for single profiles.


30-40: Dual signal handling confirmed as necessary

After inspecting the codebase, ctx.sigChan is used extensively to propagate OS interrupt signals to child processes, sleep routines, and test harnesses, while signal.NotifyContext is only used to cancel the profiling loop. Removing one of these mechanisms would break existing behaviour.

Key usages of ctx.sigChan:

  • context.go (line 31): declared as the termination request channel
  • wrapper.go & wrapper_streamsource.go: forwarding os.Interrupt to commands and sleeps
  • shell/command_unix.go & shell/command_windows.go: interrupting child processes
  • wrapper_test.go: tests rely on signalling via sigChan

No changes are required—both the traditional signal channel and signal.NotifyContext should be kept.


64-67: Interruption handling already invokes all necessary cleanup via defers

Both the top‐level function and each runProfile call use defer for their cleanup steps:

  • defer cancelGoCtx() and defer notifyStop() are declared immediately in startProfileOrGroup, so an early return nil will still cancel the signal context and notify systemd.
  • Inside runProfile, openProfile(…) returns a cleanup function that’s also deferred at the start of each profile run.

No additional cleanup or status updates are skipped when returning early on interrupt. Changes can be approved as is.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch interrupt-group

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@codecov
Copy link

codecov bot commented Jul 28, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.33%. Comparing base (adf90cb) to head (560bd93).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #539   +/-   ##
=======================================
  Coverage   79.33%   79.33%           
=======================================
  Files         136      136           
  Lines       13305    13305           
=======================================
  Hits        10555    10555           
  Misses       2332     2332           
  Partials      418      418           
Flag Coverage Δ
unittests 79.33% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@sonarqubecloud
Copy link

@creativeprojects creativeprojects merged commit 99e6d33 into master Jul 28, 2025
11 checks passed
@creativeprojects creativeprojects deleted the interrupt-group branch July 28, 2025 18:15
@creativeprojects creativeprojects added this to the v0.32.0 milestone Jul 28, 2025
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.

Response to SIGINT and SIGTERM

2 participants