Skip to content

Prevent Infinite Recursion on Cyclic Page Corruption#1301

Merged
cberner merged 2 commits into
cberner:masterfrom
am-kantox:master
Jul 17, 2026
Merged

Prevent Infinite Recursion on Cyclic Page Corruption#1301
cberner merged 2 commits into
cberner:masterfrom
am-kantox:master

Conversation

@am-kantox

Copy link
Copy Markdown
Contributor

During database startup/recovery or integrity checks on a corrupted .redb file, B-tree tree-walking operations could get stuck in an infinite recursion, eventually exhausting the call stack and crashing the application.

This was occurring in two main recursive operations:

  1. RawBtree::verify_checksum_helper: Recursing down branch children to verify leaf checksums.
  2. UntypedBtree::visit_pages_helper: Recursing down branch children to visit pages.

If page corruption introduces cyclic pointers (e.g., a child page number points to an ancestor page), these functions would loop indefinitely.

This commit resolves the issue by introducing cycle detection:

  • In RawBtree::verify_checksum_helper, we track the active DFS traversal path via a visited stack (Vec<PageNumber>). If a page has already been visited in the active path, it shotcircuits to return Ok(false) (checksum verification failure) instead of looping, allowing standard database repair/rollback to recover or fail gracefully.
  • In UntypedBtree::visit_pages_helper, we verify if a child page is already part of the active path (path.parents() or current page_number). If a cycle is detected, it returns a StorageError::Corrupted error.

Added unit tests:

  • test_page_path_cycle_detection to verify loop detection on PagePaths.
  • test_verify_checksum_cycle_detection to verify loop detection on active verify stacks.

I met this issue in the real project.

…ption

During database startup/recovery or integrity checks on a corrupted redb file,
B-tree tree-walking operations could get stuck in an infinite recursion,
eventually exhausting the call stack and crashing the application.

This was occurring in two main recursive operations:
1. RawBtree::verify_checksum_helper: Recursing down branch children to verify leaf checksums.
2. UntypedBtree::visit_pages_helper: Recursing down branch children to visit pages.

If page corruption introduces cyclic pointers (e.g., a child page number points
to an ancestor page), these functions would loop indefinitely.

This commit resolves the issue by introducing cycle detection:
- In RawBtree::verify_checksum_helper, we track the active DFS traversal path via a visited stack (Vec<PageNumber>). If a page has already been visited in the active path, it returns Ok(false) (checksum verification failure) instead of looping, allowing standard database repair/rollback to recover or fail gracefully.
- In UntypedBtree::visit_pages_helper, we verify if a child page is already part of the active path (path.parents() or current page_number). If a cycle is detected, it returns a StorageError::Corrupted error.

Added unit tests:
- test_page_path_cycle_detection to verify loop detection on PagePaths.
- test_verify_checksum_cycle_detection to verify loop detection on active verify stacks.
@codecov

codecov Bot commented Jul 16, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.14530% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 90.29%. Comparing base (adf0353) to head (a03db6c).

Files with missing lines Patch % Lines
src/tree_store/btree.rs 99.14% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1301      +/-   ##
==========================================
+ Coverage   90.22%   90.29%   +0.06%     
==========================================
  Files          36       36              
  Lines       16457    16572     +115     
==========================================
+ Hits        14848    14963     +115     
  Misses       1609     1609              
Files with missing lines Coverage Δ
src/tree_store/btree.rs 88.69% <99.14%> (+1.48%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This adds a unit test 'test_cycle_detection_in_btree' which constructs
corrupted cyclic branch nodes and verifies that RawBtree::verify_checksum_helper
and UntypedBtree::visit_pages_helper correctly detect and handle cycles,
ensuring high test coverage for the loop-prevention changes.

Assisted-by: Antigravity
@cberner
cberner merged commit 2451c0e into cberner:master Jul 17, 2026
8 checks passed
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.

2 participants