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): avoid server stop get call before start for multi components #22811

Merged
merged 8 commits into from
Dec 10, 2024

Conversation

mmsqe
Copy link
Contributor

@mmsqe mmsqe commented Dec 10, 2024

  • init httpServer early to avoid nil httpServer when stop
  • since not easy to reproduce though in system test, for more info
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x1050bd0f0]

goroutine 1 [running]:
net/http.(*Server).Shutdown(0x10851b518?, {0x108560af8?, 0x14003124720?})
	net/http/server.go:3050 +0x30
cosmossdk.io/server/v2/api/rest.(*Server[...]).Stop(0xd, {0x108560af8?, 0x14003124720})
	cosmossdk.io/server/v2@v2.0.0-20240718121635-a877e3e8048a/api/rest/server.go:93 +0xe4
cosmossdk.io/server/v2.(*Server[...]).Stop(0x1085be3c0, {0x108560af8, 0x14003124720})
	cosmossdk.io/server/v2@v2.0.0-20240718121635-a877e3e8048a/server.go:119 +0x21c
cosmossdk.io/server/v2.createStartCommand[...].func1.2.1()
	cosmossdk.io/server/v2@v2.0.0-20240718121635-a877e3e8048a/commands.go:103 +0xdc
cosmossdk.io/server/v2.createStartCommand[...].func1.2()
	cosmossdk.io/server/v2@v2.0.0-20240718121635-a877e3e8048a/co

Description

Closes: #XXXX


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title, you can find examples of the prefixes below:
  • confirmed ! in the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • updated the relevant documentation or specification, including comments for documenting Go code
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

Please see Pull Request Reviewer section in the contributing guide for more information on how to review a pull request.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

Summary by CodeRabbit

  • New Features

    • Enhanced initialization process for full nodes to ensure proper configuration for gRPC gateway communication.
    • Improved server initialization for telemetry, encapsulating setup logic within a dedicated method.
    • Streamlined server initialization for the gRPC gateway, centralizing server setup in the creation method.
  • Bug Fixes

    • Streamlined server lifecycle management with improved initialization and stopping procedures for the HTTP server.
  • Documentation

    • Minor adjustments to comments and formatting for clarity.

…ponents

init httpServer early to avoid nil httpServer when stop

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x1050bd0f0]

goroutine 1 [running]:
net/http.(*Server).Shutdown(0x10851b518?, {0x108560af8?, 0x14003124720?})
	net/http/server.go:3050 +0x30
cosmossdk.io/server/v2/api/rest.(*Server[...]).Stop(0xd, {0x108560af8?, 0x14003124720})
	cosmossdk.io/server/v2@v2.0.0-20240718121635-a877e3e8048a/api/rest/server.go:93 +0xe4
cosmossdk.io/server/v2.(*Server[...]).Stop(0x1085be3c0, {0x108560af8, 0x14003124720})
	cosmossdk.io/server/v2@v2.0.0-20240718121635-a877e3e8048a/server.go:119 +0x21c
cosmossdk.io/server/v2.createStartCommand[...].func1.2.1()
	cosmossdk.io/server/v2@v2.0.0-20240718121635-a877e3e8048a/commands.go:103 +0xdc
cosmossdk.io/server/v2.createStartCommand[...].func1.2()
	cosmossdk.io/server/v2@v2.0.0-20240718121635-a877e3e8048a/co
Copy link
Contributor

coderabbitai bot commented Dec 10, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The pull request introduces modifications to the Server struct and its methods across several files, improving the initialization and lifecycle management of the httpServer. The New function now initializes the httpServer directly after configuration, while the Start method has been simplified to remove redundant initialization. The Stop method has been updated to include deferred reinitialization of the httpServer. Additionally, changes in systemtests/system.go enhance the AddFullnode method by configuring the grpc-gateway address for full nodes.

Changes

File Change Summary
server/v2/api/rest/server.go - Added init method for initializing httpServer.
- Updated New method to call init after setting configuration.
- Removed httpServer initialization from Start method.
- Modified Stop method to include deferred call to init.
systemtests/system.go - Updated AddFullnode method to set grpc-gateway address in the configuration.
server/v2/api/telemetry/server.go - Added init method for HTTP server initialization and route setup.
- Removed inline initialization from Start method, now calls init.
server/v2/api/grpcgateway/server.go - Centralized http.Server and http.ServeMux initialization to New method.
- Simplified Start method to focus on starting the server without reinitialization.

Possibly related PRs

Suggested labels

C:server/v2 cometbft

Suggested reviewers

  • kocubinski
  • julienrbrt
  • tac0turtle
  • sontrinh16
  • hieuvubk

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 generate docstrings to generate docstrings for this PR. (Experiment)
  • @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 C:server/v2 Issues related to server/v2 C:server/v2 api labels Dec 10, 2024
@mmsqe mmsqe marked this pull request as ready for review December 10, 2024 09:04
@mmsqe mmsqe requested review from julienrbrt, hieuvubk, kocubinski and a team as code owners December 10, 2024 09:04
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: 1

📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between e07fee5 and 70751b6.

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

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

server/v2/api/rest/server.go (1)

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

🔇 Additional comments (2)
server/v2/api/rest/server.go (1)

49-52: LGTM: Early server initialization prevents nil pointer issues.

The early initialization of httpServer in New() is a good approach to prevent nil pointer dereference when Stop() is called before Start().

systemtests/system.go (1)

738-739: LGTM: Proper configuration of gateway addresses

The configuration properly sets unique addresses for grpc-gateway and rest endpoints for each full node, ensuring no port conflicts.

Comment on lines 90 to 95
defer func() {
s.httpServer = &http.Server{
Addr: s.config.Address,
Handler: s.router,
}
}()
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Reconsider server reinitialization in Stop()

The deferred reinitialization of the httpServer after shutdown could be problematic:

  1. It's unclear why the server needs to be recreated after shutdown
  2. This could lead to resource leaks if the server is repeatedly stopped and started
  3. The reinitialization might mask other underlying issues

Consider removing the deferred reinitialization:

func (s *Server[T]) Stop(ctx context.Context) error {
    if !s.config.Enable {
        return nil
    }

    s.logger.Info("stopping HTTP server")
-    defer func() {
-        s.httpServer = &http.Server{
-            Addr:    s.config.Address,
-            Handler: s.router,
-        }
-    }()
    return s.httpServer.Shutdown(ctx)
}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
defer func() {
s.httpServer = &http.Server{
Addr: s.config.Address,
Handler: s.router,
}
}()
return s.httpServer.Shutdown(ctx)

@@ -89,6 +87,12 @@ func (s *Server[T]) Stop(ctx context.Context) error {
}

s.logger.Info("stopping HTTP server")
defer func() {
Copy link
Member

Choose a reason for hiding this comment

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

If httpServer is set in New how come we need this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we call Start again later after Stop, the old httpServer will fail since Shutdown was called.

Copy link
Member

@julienrbrt julienrbrt Dec 10, 2024

Choose a reason for hiding this comment

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

I see, but why would you do that? What is the use case?
I think we should just add in the server component docs, like in the std http server docs something like:

Once Stop has been called on a server, it may not be reused.

Copy link
Member

@julienrbrt julienrbrt Dec 10, 2024

Choose a reason for hiding this comment

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

Before we wanted to support server reloading, so then it would have made sense, but due to the config reloading limitation, we haven't gone that way. I think it makes server components simpler to write if you know once you call stop you don't need to make it re-startable. So setting the server in New instead of Start does makes sense, but imho, we don't need to change anything else in Stop.

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 (3)
server/v2/api/rest/server.go (2)

62-67: Add documentation for the init method

The initialization logic is well-encapsulated, but could benefit from documentation explaining its purpose and that it's safe to call multiple times.

Add documentation like:

+// init initializes the HTTP server with the current configuration.
+// It is safe to call multiple times, especially after server shutdown.
 func (s *Server[T]) init() {

94-95: Consider making server restart handling more explicit

While reinitializing the server after shutdown is necessary for supporting restart scenarios, the current implementation could be improved:

  1. The restart capability isn't obvious from the API
  2. There's no guarantee that the same config is still valid at restart time

Consider a more explicit approach:

 func (s *Server[T]) Stop(ctx context.Context) error {
     if !s.config.Enable {
         return nil
     }
 
     s.logger.Info("stopping HTTP server")
-    defer s.init()
+    err := s.httpServer.Shutdown(ctx)
+    if err != nil {
+        return err
+    }
+    
+    // Prepare for potential restart
+    s.init()
+    return nil
 }

Also consider adding a comment in the struct documentation about restart capability:

// Server implements a REST server that supports stopping and restarting
// with the same configuration.
type Server[T transaction.Tx] struct {
server/v2/api/telemetry/server.go (1)

59-71: Add documentation for the init method

The initialization logic is well-structured, combining route setup and server initialization. Consider adding documentation for clarity.

Add documentation like:

+// init initializes the HTTP server and sets up the routes.
+// It is safe to call multiple times, especially after server shutdown.
 func (s *Server[T]) init() {
📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 70751b6 and 9db84ba.

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

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

server/v2/api/rest/server.go (1)

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

🔇 Additional comments (3)
server/v2/api/rest/server.go (1)

49-49: LGTM: Early initialization prevents nil pointer issues

The addition of srv.init() after config setup ensures the httpServer is properly initialized before any potential Stop calls, directly addressing the nil pointer dereference issue.

server/v2/api/telemetry/server.go (2)

55-55: LGTM: Consistent initialization pattern

The addition of srv.init() follows the same pattern as the REST server, ensuring proper initialization after all dependencies are set up.


105-105: Apply the same improvements as REST server

The server reinitialization pattern here matches the REST server. Consider applying the same improvements suggested for the REST server's Stop method to make the restart capability more explicit and safer.

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: 1

🧹 Outside diff range and nitpick comments (2)
server/v2/api/grpcgateway/server.go (2)

78-79: Consider using a more specific route pattern.

The catch-all pattern "/" might be too permissive. Consider using a more specific base path that matches your API structure.

Example:

 mux := http.NewServeMux()
-mux.Handle("/", s.GRPCGatewayRouter)
+mux.Handle("/api/", http.StripPrefix("/api", s.GRPCGatewayRouter))

Line range hint 116-125: Consider adding synchronization to ensure server is fully started.

There's a potential race condition where Stop could be called after Start returns but before the server is fully listening. Consider adding a way to ensure the server is fully started.

Example approach:

func (s *Server[T]) Start(ctx context.Context) error {
    if !s.config.Enable {
        s.logger.Info(fmt.Sprintf("%s server is disabled via config", s.Name()))
        return nil
    }

    // Create a channel to signal server start
    ready := make(chan struct{})
    go func() {
        // Signal when listener is ready
        ln, err := net.Listen("tcp", s.config.Address)
        if err != nil {
            close(ready)
            return
        }
        close(ready)
        s.server.Serve(ln)
    }()

    // Wait for server to start or context to cancel
    select {
    case <-ready:
        return nil
    case <-ctx.Done():
        return ctx.Err()
    }
}
📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 9db84ba and dbe76eb.

📒 Files selected for processing (3)
  • server/v2/api/grpcgateway/server.go (1 hunks)
  • server/v2/api/rest/server.go (1 hunks)
  • server/v2/api/telemetry/server.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • server/v2/api/telemetry/server.go
  • server/v2/api/rest/server.go
🧰 Additional context used
📓 Path-based instructions (1)
server/v2/api/grpcgateway/server.go (1)

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

server/v2/api/grpcgateway/server.go Show resolved Hide resolved
Copy link
Member

@julienrbrt julienrbrt left a comment

Choose a reason for hiding this comment

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

lgtm! thanks!

Copy link
Contributor

@hieuvubk hieuvubk left a comment

Choose a reason for hiding this comment

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

lgtm

@julienrbrt julienrbrt added this pull request to the merge queue Dec 10, 2024
Merged via the queue into cosmos:main with commit cb26ce3 Dec 10, 2024
71 of 75 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C:server/v2 api C:server/v2 Issues related to server/v2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants