Skip to content

tokio: Pin main loop on heap #6673

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

Merged
merged 6 commits into from
Nov 26, 2024
Merged

tokio: Pin main loop on heap #6673

merged 6 commits into from
Nov 26, 2024

Conversation

tolbrino
Copy link
Contributor

@tolbrino tolbrino commented Nov 25, 2024

By pinning the main loop onto the heap the internally generated futures and respective scaffolding would live on the heap as well instead of blowing up the stack.

Fixes #6656

Some references:

https://users.rust-lang.org/t/tools-for-inspecting-stack-usage/115132
https://rust-lang.github.io/wg-async/vision/submitted_stories/status_quo/alan_runs_into_stack_trouble.html
https://deislabs.io/posts/a-heaping-helping-of-stacks/

@tolbrino tolbrino requested review from Teebor-Choka and NumberFour8 and removed request for Teebor-Choka November 25, 2024 13:35
@tolbrino tolbrino self-assigned this Nov 25, 2024
Copy link
Contributor

coderabbitai bot commented Nov 25, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The pull request introduces significant modifications to the logging and error handling mechanisms in the RpcOperations and HoprIndexerRpcOperations implementations within chain/rpc/src/indexer.rs. Enhancements include improved debug logging for the stream_logs method and refined error handling in the try_stream_logs method, including checks for empty filters and structured handling of log retrieval failures. Additionally, the main function in hoprd/hoprd/src/main.rs has been transformed from an asynchronous to a synchronous entry point, encapsulating asynchronous logic within a newly defined inner_main function.

Changes

File Path Change Summary
chain/rpc/src/indexer.rs Enhanced stream_logs with debug logging for parameters; refined try_stream_logs for error handling, including checks for empty filters and structured log processing.
hoprd/hoprd/src/main.rs Changed main from asynchronous to synchronous; added inner_main to manage async operations.

Assessment against linked issues

Objective Addressed Explanation
1. Prevent crashes during operation (#6656)
2. Log stack overflow errors clearly (#6656)

Possibly related PRs

  • Add check on safe module on start #6526: This PR modifies the HoprRpcOperations trait and introduces new methods related to safe module configuration, which may interact with the logging and error handling improvements in the main PR's HoprIndexerRpcOperations methods.
  • Improve logging to remove clutter from the indexer process #6646: This PR adjusts the logging levels in the HoprIndexerRpcOperations implementation, specifically in the try_stream_logs method, which is directly related to the changes made in the main PR that enhance logging and error handling in the same method.

Suggested labels

effort:medium, complexity:normal, status:in-progress

Suggested reviewers

  • NumberFour8

📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between f295a36 and c4b1ef8.

📒 Files selected for processing (1)
  • chain/rpc/src/indexer.rs (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • chain/rpc/src/indexer.rs

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.

@tolbrino tolbrino marked this pull request as ready for review November 25, 2024 13:37
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: 2

🧹 Outside diff range and nitpick comments (2)
hoprd/hoprd/src/main.rs (2)

163-163: Consider making thread stack size configurable

Currently, the thread stack size is hard-coded to 8MB. Making this value configurable would provide flexibility for different environments or use cases.

Consider adding a configuration option or environment variable to set the thread stack size. For example:

+    let stack_size = std::env::var("HOPRD_THREAD_STACK_SIZE")
+        .ok()
+        .and_then(|s| s.parse::<usize>().ok())
+        .unwrap_or(8 * 1024 * 1024);
     
     tokio::runtime::Builder::new_multi_thread()
         .enable_all()
-        .thread_stack_size(8 * 1024 * 1024)
+        .thread_stack_size(stack_size)
         .build()?

166-166: Simplify block_on call by removing unnecessary Box::pin

The block_on method can accept the future directly without the need to Box::pin it. Removing Box::pin simplifies the code.

Apply this diff to simplify the block_on call:

     .build()?
-    .block_on(Box::pin(inner_main()))
+    .block_on(inner_main())
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 4c64784 and e3a3dce.

📒 Files selected for processing (2)
  • chain/rpc/src/indexer.rs (1 hunks)
  • hoprd/hoprd/src/main.rs (1 hunks)
🔇 Additional comments (1)
chain/rpc/src/indexer.rs (1)

74-78: Enhanced debug logging improves traceability

The added debug statements provide valuable information on the block subranges being fetched, which will aid in debugging and monitoring the log retrieval process.

@tolbrino tolbrino changed the title tokio: Increase thread stack size to 8MB tokio: Pin main loop Nov 25, 2024
Co-authored-by: Tibor <9529609+Teebor-Choka@users.noreply.github.com>
@tolbrino tolbrino changed the title tokio: Pin main loop tokio: Pin main loop on heap Nov 25, 2024
@Teebor-Choka Teebor-Choka added this to the 2.2.0-rc.1 milestone Nov 25, 2024
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)
hoprd/hoprd/src/main.rs (1)

174-174: Consider implementing Drop trait for cleanup

The inner_main function handles graceful shutdown via signal handling, but consider implementing the Drop trait for critical resources to ensure cleanup even in case of panic.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 731c8cc and 196b2de.

📒 Files selected for processing (1)
  • hoprd/hoprd/src/main.rs (1 hunks)
🔇 Additional comments (2)
hoprd/hoprd/src/main.rs (2)

159-167: LGTM: Stack overflow mitigation implemented correctly

The changes effectively address the stack overflow issues by:

  1. Setting an explicit thread stack size of 2MB
  2. Moving the main loop to the heap via Box::pin
  3. Using a multi-threaded runtime with all features enabled

168-170: Verify runtime-async-std implementation

The async-std implementation is present but might need additional configuration to match the Tokio implementation's stack overflow prevention measures.

✅ Verification successful

Let me analyze the results and generate a final response:

The async-std implementation is correctly configured and safe

The codebase shows that async-std is properly integrated with appropriate stack and runtime configurations:

  • The async-std implementation is used as a conditional feature alongside tokio
  • The tokio implementation explicitly configures thread stack size (2MB) while async-std uses its default configuration which is safe for most workloads
  • The async-std runtime is properly integrated across the dependency tree with consistent feature flags
  • No evidence of stack overflow issues in the async-std implementation based on the codebase analysis
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if runtime-async-std feature is used in the codebase

# Check Cargo.toml for runtime feature configuration
rg -A 5 'runtime-async-std|runtime-tokio' 

# Check for any async-std runtime configurations
rg -A 5 'async_std::task::Builder'

Length of output: 67128

Co-authored-by: Tibor <9529609+Teebor-Choka@users.noreply.github.com>
@Teebor-Choka Teebor-Choka merged commit 5fcc893 into master Nov 26, 2024
28 checks passed
@Teebor-Choka Teebor-Choka deleted the tb/20241125-stack-size branch November 26, 2024 00:27
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.

thread 'tokio-runtime-worker' has overflowed its stack and then restart
2 participants