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(state): Write database format version to disk atomically to avoid a rare panic #8795

Merged
merged 7 commits into from
Aug 29, 2024

Conversation

arya2
Copy link
Contributor

@arya2 arya2 commented Aug 21, 2024

Motivation

We want to avoid potentially corrupting the database format version file when updating it.

Closes #8793.

Solution

  • Factors code for atomically updating the peer cache into an atomic_write_to_tmp_file() fn.
  • Replaces call to fs::write() in write_database_format_version_to_disk() with a call to atomic_write_to_tmp_file().

Tests

Parts of this change are covered by existing tests for update_peer_cache().

We could also add a test for the atomic file write that interrupts a very expensive file system operation and checks that nothing has been written to the target file path, though I would prefer to leave that as follow-up work.

Follow-up Work

  • Add a test to check that atomic_write_to_tmp_file() doesn't write anything to the target file path when interrupted.
  • Ensure that all in-place database format upgrades can succeed when re-run in case the disk format version is outdated.

PR Author's Checklist

  • The PR name will make sense to users.
  • The PR provides a CHANGELOG summary.
  • The solution is tested.
  • The documentation is up to date.
  • The PR has a priority label.

PR Reviewer's Checklist

  • The PR Author's checklist is complete.
  • The PR resolves the issue.

@arya2 arya2 added C-bug Category: This is a bug I-panic Zebra panics with an internal error message A-state Area: State / database changes P-Low ❄️ labels Aug 21, 2024
@arya2 arya2 self-assigned this Aug 21, 2024
@arya2 arya2 requested review from a team as code owners August 21, 2024 21:56
@arya2 arya2 requested review from upbqdn and removed request for a team August 21, 2024 21:56
…s callsite to avoid adding tokio as a dependency of zebra-chain.
upbqdn
upbqdn previously approved these changes Aug 26, 2024
zebra-chain/src/common.rs Outdated Show resolved Hide resolved
zebra-state/src/config.rs Outdated Show resolved Hide resolved
zebra-chain/src/common.rs Outdated Show resolved Hide resolved
zebra-chain/src/common.rs Outdated Show resolved Hide resolved
zebra-chain/src/common.rs Outdated Show resolved Hide resolved
zebra-chain/src/common.rs Outdated Show resolved Hide resolved
zebra-network/src/config.rs Outdated Show resolved Hide resolved
zebra-network/src/config.rs Outdated Show resolved Hide resolved
@arya2 arya2 added do-not-merge Tells Mergify not to merge this PR and removed do-not-merge Tells Mergify not to merge this PR labels Aug 27, 2024
zebra-state/src/config.rs Outdated Show resolved Hide resolved
zebra-state/src/config.rs Outdated Show resolved Hide resolved
zebra-chain/src/common.rs Outdated Show resolved Hide resolved
zebra-network/src/config.rs Outdated Show resolved Hide resolved
zebra-network/src/config.rs Outdated Show resolved Hide resolved
mergify bot added a commit that referenced this pull request Aug 29, 2024
@mergify mergify bot merged commit 0ef9987 into main Aug 29, 2024
135 checks passed
@mergify mergify bot deleted the atomic-write-db-version branch August 29, 2024 21:09
dmidem pushed a commit to QED-it/zebra that referenced this pull request Oct 29, 2024
… a rare panic (ZcashFoundation#8795)

* Splits `atomic_write_to_tmp_file` out of `zebra_network::Config::update_peer_cache`

* Uses the new `atomic_write_to_tmp_file` fn in `update_peer_cache()`

* Replaces repetitive code for getting the default peer and state cache directories with `default_cache_dir()`

* Converts `atomic_write_to_tmp_file` to a blocking function and adds `spawn_atomic_write_to_tmp_file` for use in async environments.

* Uses `atomic_write_to_tmp_file` to write database versions to disk

* Removes `spawn_atomic_write_to_tmp_file()` and inlines its body at its callsite to avoid adding tokio as a dependency of zebra-chain.

* Apply suggestions from code review

Co-authored-by: Marek <[email protected]>

---------

Co-authored-by: Marek <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-state Area: State / database changes C-bug Category: This is a bug I-panic Zebra panics with an internal error message P-Low ❄️
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix panic when Zebra in database_format_version_at_path
2 participants