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(baseapp): introduce mutex to state #18846

Merged
merged 4 commits into from
Dec 21, 2023

Conversation

nivasan1
Copy link
Contributor

Description

Closes: #18844

In This PR

  • I introduce a sync.RWMutex to the state object
  • I take a ReadLock on each call to Context (getter)
  • I take a Lock on each call to UpdateContext (setter)
  • I migrate all raw .ctx accesses to either Context() for reads, or UpdateContext for writes

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

  • [x ] included the correct type prefix in the PR title
  • [ x] confirmed ! in the type prefix if API or client breaking change
  • [ x] targeted the correct branch (see PR Targeting)
  • [ x] provided a link to the relevant issue or specification
  • [ x] reviewed "Files changed" and left comments if necessary
  • [ x] included the necessary unit and integration tests
  • [ x] added a changelog entry to CHANGELOG.md
  • [x ] updated the relevant documentation or specification, including comments for documenting Go code
  • [x ] 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.

I have...

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

@nivasan1 nivasan1 requested a review from a team as a code owner December 20, 2023 19:44
Copy link
Contributor

coderabbitai bot commented Dec 20, 2023

Walkthrough

The changes across the files aim to enhance thread safety in the Cosmos SDK by introducing mutexes and updating the context handling. The modifications include encapsulation of context within state structures, ensuring synchronized access to shared resources, and altering method signatures to accommodate the new thread-safe design. These changes aim to address race conditions, specifically within the app.checkTxState and related functions.

Changes

File(s) Summary of Changes
baseapp/abci.go Context handling and method invocations updated to use Context() and UpdateContext() methods, altering function signatures to be thread-safe.
baseapp/baseapp.go Method calls related to context handling in the BaseApp type updated to use Context() and UpdateContext() methods instead of direct access or modification of the ctx field.
baseapp/state.go Introduction of sync.RWMutex in the state struct and addition of synchronization methods UpdateContext and Context() to prevent race conditions.

Assessment against linked issues

Objective Addressed Explanation
Address race condition in app.checkTxState within internalFinalizeBlock and CreateQueryContext functions (Cosmos SDK v0.50) [#18844]
Add a CurrentHeader field to baseApp wrapped by a mutex to synchronize accesses to app.checkTxState [#18844] The provided summary does not mention a CurrentHeader field, but mutexes are introduced which could be part of the solution.
Make baseApp ABCI methods thread-safe [#18844] The introduction of mutexes and the refactoring of context handling suggest thread-safety has been addressed.

The code changes appear to align with the objectives of addressing thread safety concerns, as indicated in the linked issue. However, without explicit mention of the CurrentHeader field, it's unclear if this specific part of the solution was implemented or if an alternative approach was taken. The introduction of mutexes and context encapsulation methods does indicate an effort to make the baseApp ABCI methods thread-safe, which is the overarching goal of the issue.

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 X ?


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • You can reply to a review comment made by CodeRabbit.
  • You can tag CodeRabbit on specific lines of code or files in the PR by tagging @coderabbitai in a comment.
  • You can tag @coderabbitai in a PR comment and ask one-off questions about the PR and the codebase. Use quoted replies to pass the context for follow-up questions.

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

Copy link
Contributor

@Eric-Warehime Eric-Warehime left a comment

Choose a reason for hiding this comment

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

LGTM.

I'm not 100% sure that state objects themselves are being updated/read in a threadsafe way (would have to check), but this solves the problem for context access.

Copy link
Member

@tac0turtle tac0turtle left a comment

Choose a reason for hiding this comment

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

utACK. this is for sure safer than what we have today. I wonder if there will be any sort of regressions in performance, but its mute since this is safer. With server v2 we will be able to avoid this

Copy link
Contributor

@likhita-809 likhita-809 left a comment

Choose a reason for hiding this comment

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

lgtm, can you fix the lint issue coming from imports in state.go file?

@tac0turtle tac0turtle added C:baseapp backport/v0.50.x PR scheduled for inclusion in the v0.50's next stable release labels Dec 21, 2023
Copy link
Contributor

@alexanderbez alexanderbez left a comment

Choose a reason for hiding this comment

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

LGTM. Just two minor nits

baseapp/state.go Outdated Show resolved Hide resolved
baseapp/state.go Outdated Show resolved Hide resolved
@alexanderbez alexanderbez added this pull request to the merge queue Dec 21, 2023
Merged via the queue into cosmos:main with commit c519104 Dec 21, 2023
50 of 55 checks passed
mergify bot pushed a commit that referenced this pull request Dec 21, 2023
Co-authored-by: Aleksandr Bezobchuk <[email protected]>
(cherry picked from commit c519104)

# Conflicts:
#	baseapp/abci.go
alexanderbez added a commit that referenced this pull request Dec 22, 2023
Co-authored-by: Nikhil Vasan <[email protected]>
Co-authored-by: marbar3778 <[email protected]>
Co-authored-by: Aleksandr Bezobchuk <[email protected]>
@faddat faddat mentioned this pull request Nov 8, 2024
12 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport/v0.50.x PR scheduled for inclusion in the v0.50's next stable release C:baseapp
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Race condition on app.checkTxState
5 participants