Skip to content

fix(state): Write database format version to disk atomically to avoid a rare panic - #8795

Merged
mergify[bot] merged 7 commits into
mainfrom
atomic-write-db-version
Aug 29, 2024
Merged

fix(state): Write database format version to disk atomically to avoid a rare panic#8795
mergify[bot] merged 7 commits into
mainfrom
atomic-write-db-version

Conversation

@arya2

@arya2 arya2 commented Aug 21, 2024

Copy link
Copy Markdown
Contributor

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 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
Comment thread zebra-chain/src/common.rs Outdated
Comment thread zebra-state/src/config.rs Outdated
Comment thread zebra-chain/src/common.rs Outdated
Comment thread zebra-chain/src/common.rs Outdated
Comment thread zebra-chain/src/common.rs Outdated
Comment thread zebra-chain/src/common.rs Outdated
Comment thread zebra-network/src/config.rs Outdated
Comment thread zebra-network/src/config.rs Outdated
@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
Comment thread zebra-state/src/config.rs Outdated
Comment thread zebra-state/src/config.rs Outdated
Comment thread zebra-chain/src/common.rs Outdated
Comment thread zebra-network/src/config.rs Outdated
Comment thread zebra-network/src/config.rs Outdated
Co-authored-by: Marek <mail@marek.onl>
mergify Bot added a commit that referenced this pull request Aug 29, 2024
@mergify
mergify Bot merged commit 0ef9987 into main Aug 29, 2024
@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 <mail@marek.onl>

---------

Co-authored-by: Marek <mail@marek.onl>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-bug Category: This is a bug 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