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

chore: ensure thread safety #6896

Merged

Conversation

SWvheerden
Copy link
Collaborator

@SWvheerden SWvheerden commented Mar 31, 2025

Description

Make sure the can we modify SMT is access inside of the write lock

Summary by CodeRabbit

  • Refactor
    • Improved internal process flow to enhance consistency without affecting any visible functionality.

@SWvheerden SWvheerden requested a review from a team as a code owner March 31, 2025 07:00
Copy link
Contributor

coderabbitai bot commented Mar 31, 2025

Walkthrough

The change reorders the evaluation in the insert_tip_block_body method of the LMDBDatabase. The variable can_we_change_smt is now evaluated before attempting to acquire a write lock on the smt rather than after. The overall logic and error handling remain unaltered.

Changes

File Change Summary
base_layer/core/src/chain_storage/.../lmdb_db.rs Moved the declaration of can_we_change_smt to occur before acquiring the write lock on smt

Sequence Diagram(s)

sequenceDiagram
    participant Client as Client
    participant LMDB as LMDBDatabase

    Client->>LMDB: insert_tip_block_body()
    Note over LMDB: Evaluate can_we_change_smt (moved earlier)
    LMDB->>LMDB: Acquire write lock on smt
    LMDB->>LMDB: Perform error handling for lock acquisition
    LMDB->>LMDB: Check for existing block body and insert if absent
    LMDB-->>Client: Return result
Loading

Poem

I'm a bunny in the code maze, hopping free,
Skipping lines with swift agility,
Evaluating variables before the lock's embrace,
With nibbling precision and playful grace,
🥕 Cheers to changes—hop on, happy code race!


📜 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 61eda04 and e27ea1f.

📒 Files selected for processing (1)
  • base_layer/core/src/chain_storage/lmdb_db/lmdb_db.rs (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (7)
  • GitHub Check: ci
  • GitHub Check: Cucumber tests / FFI
  • GitHub Check: test (mainnet, stagenet)
  • GitHub Check: Cucumber tests / Base Layer
  • GitHub Check: test (testnet, esmeralda)
  • GitHub Check: test (nextnet, nextnet)
  • GitHub Check: cargo check with stable
🔇 Additional comments (1)
base_layer/core/src/chain_storage/lmdb_db/lmdb_db.rs (1)

1255-1262: Thread safety improvement: Load the SMT modification flag right after acquiring the write lock.

The change ensures that the decision about whether to modify the SMT is made immediately after acquiring the write lock. This eliminates a potential thread safety issue where the value of allow_smt_change could change between lock acquisition and usage.

By evaluating can_we_change_smt right after acquiring the write lock, you guarantee that the flag is checked in a thread-safe manner before it's used later in the function at lines 1335-1346 and 1380-1391.

✨ Finishing Touches
  • 📝 Generate Docstrings

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.
    • 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.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @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.

Copy link

Test Results (CI)

    3 files    129 suites   44m 51s ⏱️
1 352 tests 1 352 ✅ 0 💤 0 ❌
4 054 runs  4 054 ✅ 0 💤 0 ❌

Results for commit e27ea1f.

Copy link

Test Results (Integration tests)

36 tests  +36   36 ✅ +36   16m 11s ⏱️ + 16m 11s
11 suites +11    0 💤 ± 0 
 2 files   + 2    0 ❌ ± 0 

Results for commit e27ea1f. ± Comparison against base commit 61eda04.

@SWvheerden SWvheerden merged commit dd4279c into tari-project:development Mar 31, 2025
17 checks passed
@SWvheerden SWvheerden deleted the sw_makebool_thread_safe branch March 31, 2025 09:31
sdbondi added a commit to sdbondi/tari that referenced this pull request Apr 2, 2025
* development: (412 commits)
  chore: new release v1.13.1-pre.0 (tari-project#6898)
  fix: excess sig order in the tx tab (tari-project#6897)
  chore: ensure thread safety (tari-project#6896)
  fix: peer order (tari-project#6894)
  fix: use plain string for grpc address (tari-project#6881)
  fix: startup arg (tari-project#6889)
  feat: add num connections to network state (tari-project#6884)
  refactor: reduce logging to make it less noisy (tari-project#6882)
  feat: check coinbase count (tari-project#6880)
  chore(deps): bump dorny/test-reporter from 1 to 2 (tari-project#6883)
  chore: update readme (tari-project#6878)
  fix:  libtor cli option (tari-project#6877)
  chore: new version v1.13.0-pre.0 (tari-project#6875)
  chore: reset network (tari-project#6874)
  fix:  prune mode validation (tari-project#6873)
  perf: remove duplicate metadata signature verification (tari-project#6866)
  feat: remove static moneroD response (tari-project#6867)
  chore(ci): updates to pull version from workspace (tari-project#6868)
  chore(ci): binaries build continue-on-error when release (tari-project#6865)
  chore: handle seed words env var (tari-project#6855)
  ...
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