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(db): Fix a sprout/history tree read panic in Zebra 1.4.0, which only happens before the 25.3.0 state upgrade completes #7972

Merged
merged 8 commits into from
Nov 22, 2023

Conversation

teor2345
Copy link
Collaborator

@teor2345 teor2345 commented Nov 21, 2023

Motivation

Zebra 1.4.0 only writes the sprout and history trees when they have changed, but it expects to find them at the empty key () or tip height. If there aren't any sprout transactions, or we're before heartwood, the trees can actually be below the tip height.

See this detailed explanation:
#7961 (comment)

There is also a race condition that can happen when upgrading from 1.1.0 to 1.4.0, which we support on a best effort basis.

See this detailed explanation for this possible race condition:
#7961 (comment)

Close #7961.

PR Author Checklist

Check before marking the PR as ready for review:

  • Will the PR name make sense to users?
  • Does the PR have a priority label?
  • Have you added or updated tests?
  • Is the documentation up to date?
For significant changes:
  • Is there a summary in the CHANGELOG?
  • Can these changes be split into multiple PRs?

If a checkbox isn't relevant to the PR, mark it as done.

We might want a changelog entry, I'm not sure.

Complex Code or Requirements

This is concurrent code involving a data race.

Solution

  • Look for the sprout tree at the greatest available height before and during the 25.3.0 upgrade, rather than just checking the tip height
  • Minimise the risk of a potential data race before and during the 25.3.0 state upgrade, by moving slow code to another database batch

Testing

Marek and the ticket reporter have both manually tested this code and confirmed it is correct.

I don't think we can test for the race condition that's fixed in this PR.

Review

This is a high priority fix.

Reviewer Checklist

Check before approving the PR:

  • Does the PR scope match the ticket?
  • Are there enough tests to make sure it works? Do the tests cover the PR motivation?
  • Are all the PR blockers dealt with?
    PR blockers can be dealt with in new tickets or PRs.

And check the PR Author checklist is complete.

Follow Up Work

Document how to avoid this happening again in the state upgrade docs.

In particular, don't concurrently read and write the same keys in the same column family:

  • in an upgrade, and
  • in the sync.

And make sure the read and write methods have compatible conditions.

@teor2345 teor2345 added C-bug Category: This is a bug P-High 🔥 I-panic Zebra panics with an internal error message A-state Area: State / database changes A-concurrency Area: Async code, needs extra work to make it work properly. labels Nov 21, 2023
@teor2345 teor2345 self-assigned this Nov 21, 2023
@teor2345 teor2345 requested a review from a team as a code owner November 21, 2023 07:55
@teor2345 teor2345 requested review from arya2 and removed request for a team November 21, 2023 07:55
@teor2345 teor2345 changed the title fix(db): Avoid a race condition causing data corruption in the 25.3.0 state upgrade fix(db): Fix a state sprout/history tree read panic in Zebra 1.4.0, before the 25.3.0 state upgrade is completed Nov 21, 2023
@teor2345 teor2345 changed the title fix(db): Fix a state sprout/history tree read panic in Zebra 1.4.0, before the 25.3.0 state upgrade is completed fix(db): Fix a sprout/history tree read panic in Zebra 1.4.0, which only happens before the 25.3.0 state upgrade completes Nov 21, 2023
…x_tree_key_type.rs

Co-authored-by: Marek <mail@marek.onl>
@teor2345 teor2345 requested a review from upbqdn November 22, 2023 20:44
mergify bot added a commit that referenced this pull request Nov 22, 2023
@mergify mergify bot merged commit d20ec39 into main Nov 22, 2023
97 checks passed
@mergify mergify bot deleted the tree-state-race branch November 22, 2023 23:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-concurrency Area: Async code, needs extra work to make it work properly. A-state Area: State / database changes C-bug Category: This is a bug I-panic Zebra panics with an internal error message
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug: Crash: Sprout note commitment tree must exist if there is a finalized tip
2 participants