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

fix(server/v2): respect context cancellation in start command #22346

Merged
merged 2 commits into from
Oct 23, 2024

Conversation

mark-rushakoff
Copy link
Member

@mark-rushakoff mark-rushakoff commented Oct 23, 2024

When running external tests that involve starting a v2 server, the only way to get the command to shut down was through sending an interrupt or termination signal, which is not possible to do independently through many concurrent serer commands within the same process.

Respect the root context being cancelled, as a signal that the server needs to be stopped.

Unfortunately there are no existing unit tests in the server/v2 package with running a start command, that can be expanded to cover this behavior.

Summary by CodeRabbit

  • New Features

    • Improved signal handling for graceful shutdown, enhancing server responsiveness during cancellation.
  • Bug Fixes

    • Addressed potential blocking issues during shutdown by implementing a more efficient control flow.

When running external tests that involve starting a v2 server, the only
way to get the command to shut down was through sending an interrupt or
termination signal, which is not possible to do independently through
many concurrent serer commands within the same process.

Respect the root context being cancelled, as a signal that the server
needs to be stopped.

Unfortunately there are no existing unit tests in the server/v2 package
with running a start command, that can be expanded to cover this
behavior.
Copy link
Contributor

coderabbitai bot commented Oct 23, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The changes in this pull request focus on the createStartCommand function within the server/v2/commands.go file. The primary modification involves the signal handling logic for graceful shutdown, replacing the direct wait on a signal with a select statement that listens for both OS signals and context cancellation. This adjustment allows for a more responsive shutdown process, particularly beneficial in testing scenarios. The overall structure and error handling remain unchanged, with no modifications to the signatures of exported or public entities.

Changes

File Change Summary
server/v2/commands.go Modified createStartCommand to use a select statement for signal handling, allowing for non-blocking context cancellation during shutdown. Error handling remains consistent.

Possibly related PRs

  • feat(server/v2): enable pprof #22284: The changes in this PR involve modifications to the createStartCommand function in server/v2/commands.go, which is directly related to the main PR's updates to the same function for improved signal handling and shutdown logic.

Suggested reviewers

  • hieuvubk
  • kocubinski
  • tac0turtle

📜 Recent review details

Configuration used: .coderabbit.yml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 559b5ef and a8fa60c.

📒 Files selected for processing (1)
  • server/v2/commands.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • server/v2/commands.go

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>, please review it.
    • Generate unit testing code for this file.
    • 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 generate unit testing code 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 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 generate unit testing code.
    • @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.

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 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.

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.

@github-actions github-actions bot added the C:server/v2 Issues related to server/v2 label Oct 23, 2024
Copy link
Contributor

@mark-rushakoff your pull request is missing a changelog!

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.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
server/v2/commands.go (1)

123-131: Add signal cleanup to prevent resource leaks.

The implementation correctly handles both OS signals and context cancellation. However, the signal channel should be cleaned up to prevent potential resource leaks.

Consider adding signal cleanup:

 go func() {
   sigCh := make(chan os.Signal, 1)
   signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
+  defer signal.Stop(sigCh)
   select {
   case sig := <-sigCh:
     cancelFn()
     cmd.Printf("caught %s signal\n", sig.String())
   case <-ctx.Done():
     cancelFn()
   }
📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 762fad2 and 559b5ef.

📒 Files selected for processing (1)
  • server/v2/commands.go (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
server/v2/commands.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

🔇 Additional comments (1)
server/v2/commands.go (1)

123-131: 🛠️ Refactor suggestion

Consider using sync.Once for cancelFn to prevent potential race conditions.

There's a potential race condition where cancelFn could be called twice if both a signal is received and the context is cancelled simultaneously. While this might not cause issues with the current context.CancelFunc implementation, it's better to be explicit about ensuring single execution.

Consider using sync.Once:

 go func() {
+  var once sync.Once
   sigCh := make(chan os.Signal, 1)
   signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
+  defer signal.Stop(sigCh)
   select {
   case sig := <-sigCh:
-    cancelFn()
+    once.Do(cancelFn)
     cmd.Printf("caught %s signal\n", sig.String())
   case <-ctx.Done():
-    cancelFn()
+    once.Do(cancelFn)
   }

Let's verify the potential race condition:

@julienrbrt julienrbrt added this pull request to the merge queue Oct 23, 2024
Merged via the queue into main with commit bf12702 Oct 23, 2024
75 of 76 checks passed
@julienrbrt julienrbrt deleted the mr/server-command-context branch October 23, 2024 20:16
alpe added a commit that referenced this pull request Oct 25, 2024
* main: (157 commits)
  feat(core): add ConfigMap type (#22361)
  test: migrate e2e/genutil to systemtest (#22325)
  feat(accounts): re-introduce bundler (#21562)
  feat(log): add slog-backed Logger type (#22347)
  fix(x/tx): add feePayer as signer (#22311)
  test: migrate e2e/baseapp to integration tests (#22357)
  test: add x/circuit system tests (#22331)
  test: migrate e2e/tx tests to systemtest (#22152)
  refactor(simdv2): allow non-comet server components (#22351)
  build(deps): Bump rtCamp/action-slack-notify from 2.3.1 to 2.3.2 (#22352)
  fix(server/v2): respect context cancellation in start command (#22346)
  chore(simdv2): allow overriding the --home flag (#22348)
  feat(server/v2): add SimulateWithState to AppManager (#22335)
  docs(x/accounts): improve comments (#22339)
  chore: remove unused local variables (#22340)
  test: x/accounts systemtests (#22320)
  chore(client): use command's configured output (#22334)
  perf(log): use sonic json lib  (#22233)
  build(deps): bump x/tx (#22327)
  fix(x/accounts/lockup): fix proto path (#22319)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C:server/v2 Issues related to server/v2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants