Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graceful shutdown -- handle termination signals #14

Merged
merged 4 commits into from
Feb 21, 2024

Conversation

efrecon
Copy link
Owner

@efrecon efrecon commented Feb 21, 2024

Summary by CodeRabbit

  • New Features
    • Enhanced command-line argument handling for better script orchestration.
    • Introduced comprehensive process management functions for improved cross-platform process handling.
    • Improved cleanup and termination processes for scripts, ensuring cleaner exits and better resource management.
    • Added asynchronous execution capabilities in runner scripts for more efficient operation.
  • Refactor
    • Updated various scripts to include better error handling, logging, and job control settings for more robust script execution.

Implement graceful shutdown of all the runners on termination signals
received by the orchestrator. In order to avoid ctrl-c from being sent
down the entire tree, a separate process group is created by the runner
loop: this prevents abrupt termination of the microVM. When termination
is received, a termination file (with the same secret as for the break
file) is created. The implementation will react to the termination file
and kill and unregister the runner.

In order for all termination to be handled in the background, all
processes are started in the background and waited for. The library is
enhanced with a number of functions to handle processes, these should be
portable to macOS.
When the token file is present, remove through creating a termination
file. Otherwise, kill the entire process tree directly, as the runner
has not (yet) been registered at GitHub.
Copy link
Contributor

coderabbitai bot commented Feb 21, 2024

Walkthrough

The recent updates enhance process management and script cleanup across several shell scripts. Key changes include reordering command-line arguments, introducing new process management functions, refining cleanup procedures, and improving job control for asynchronous execution. These modifications aim to boost the efficiency and reliability of the scripts, focusing on better handling of command-line arguments, process tracking, and termination logic.

Changes

Files Change Summary
demo/demo.sh Adjusted the order of command-line arguments passed to orchestrator.sh.
lib/common.sh Added process management functions (tac, pgrep_P, ps_tree, running, waitpid, kill_tree) using awk and ps.
orchestrator.sh Modified cleanup process and trap behavior for EXIT to improve runner process termination.
runner.sh, runner/runner.sh Introduced functions for termination logic (vm_terminate), cleanup (cleanup), and PID tracking. Enhanced asynchronous execution and job control. Improved logging and error handling.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

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>.
    • Generate unit-tests for this file.
  • 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 generate unit tests for this file.
    • @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 generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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 as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between b07253c and 7412f21.
Files selected for processing (5)
  • demo/demo.sh (1 hunks)
  • lib/common.sh (1 hunks)
  • orchestrator.sh (3 hunks)
  • runner.sh (3 hunks)
  • runner/runner.sh (3 hunks)
Additional comments: 15
demo/demo.sh (1)
  • 18-18: The adjustment of command-line arguments passed to orchestrator.sh seems appropriate. Ensure that orchestrator.sh is updated to correctly process these arguments in their new order.
lib/common.sh (6)
  • 78-80: The tac function implementation using awk is correct and efficiently reverses input lines. This is a good cross-platform solution.
  • 84-86: The pgrep_P function correctly mimics the behavior of pgrep -P using ps and awk, providing a cross-platform way to find child processes. Ensure that the ps options used are compatible across different UNIX-like systems.
  • 88-95: The ps_tree function recursively lists a process tree starting from a given PID. This implementation is logically sound and uses recursion effectively. However, consider adding error handling for invalid PIDs.
  • 97-105: The running function constructs a list of running PIDs from a given list. It's important to ensure that the ps command's output format is consistent across different systems for this function to work correctly.
  • 108-118: The waitpid function uses a polling mechanism to wait for a list of PIDs to terminate. While this approach is functional, consider the performance implications of polling with a fixed sleep interval. An adaptive sleep interval might improve performance in some scenarios.
  • 121-126: The kill_tree function effectively kills a process tree starting from a given PID. This function is well-implemented, but ensure that the signal specified in the second argument is handled correctly by all targeted processes.
orchestrator.sh (3)
  • 92-100: The modifications to the cleanup function, including the adjustment of the trap behavior for EXIT signals and the enhanced process termination logic, are well-implemented. Ensure that the waitpid function is robust and handles all edge cases correctly.
  • 130-130: Setting up the trap for the cleanup function on EXIT signals is a good practice for ensuring resources are properly released upon script termination. Verify that all necessary cleanup actions are included in the cleanup function.
  • 171-173: The explicit call to waitpid at the end of the script, followed by a call to cleanup, ensures that all child processes are waited on before proceeding with cleanup. This is a good practice for ensuring a graceful shutdown.
runner.sh (3)
  • 265-272: The modifications to vm_start() to include PID tracking and wait logic are well-implemented. The use of job control settings and the restoration of the original option state are good practices. Ensure that the PID tracking mechanism is robust and handles all edge cases correctly.
  • 287-311: The vm_terminate() function introduces a comprehensive termination logic that handles different scenarios effectively. Ensure that the signal used in kill_tree is appropriate for the processes being terminated and that the waiting logic correctly handles all child processes.
  • 314-322: The cleanup() function is well-designed to manage cleanup tasks and utilizes the vm_terminate() function effectively. The use of trap to ensure cleanup on script exit is a good practice. Verify that all necessary cleanup actions are included and correctly implemented.
runner/runner.sh (2)
  • 420-431: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [423-439]

The implementation of asynchronous execution and PID tracking for the runner process is correctly done using & to run the command in the background and capturing the PID with $!. However, ensure that the user permissions and environment are correctly set up for the runner process when running as a non-root user, especially when changing ownership of the $RUNNER_WORKDIR. This is crucial for avoiding permission issues that could affect the runner's operation.

  • 447-466: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [1-466]

Overall, the script modifications align with the PR objectives of enhancing graceful shutdown capabilities. Ensure that all external commands and utilities used in the script are available in the target environments, especially considering the script's potential to run in diverse environments. Additionally, thorough testing in different scenarios (e.g., different user permissions, unexpected termination signals) is recommended to ensure the changes work as intended across various configurations.

runner/runner.sh Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 7412f21 and b344d5f.
Files selected for processing (1)
  • runner/runner.sh (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • runner/runner.sh

@efrecon efrecon merged commit 929a7af into main Feb 21, 2024
2 of 3 checks passed
@efrecon efrecon deleted the feature/signal-handling branch February 21, 2024 23:26
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