Skip to content

Enable redb database logging & close DB cleanly - #60

Closed
flyq wants to merge 4 commits into
mainfrom
liquan/add_redb_log
Closed

Enable redb database logging & close DB cleanly#60
flyq wants to merge 4 commits into
mainfrom
liquan/add_redb_log

Conversation

@flyq

@flyq flyq commented Dec 25, 2025

Copy link
Copy Markdown
Member

Summary

Enable redb database logging and refactor shutdown to ensure the database closes cleanly.

Problem

When the validator was restarted after exit, it displayed a warning:

2025-12-25T15:59:11.356785Z  WARN redb::db: Database "FileBackend { lock_supported: true, file: File { fd: 11, path: \"/Users/worker13/.chain-ops/devnet/stateless-validator/validator.redb\", read: true, write: true } }" not shutdown cleanly. Repairing

This occurred because background tasks holding Arc references could prevent the database from being dropped before the process exited.

Changes

Enable redb Logging

  • Added logging feature to the redb dependency in validator-core/Cargo.toml

Clean Shutdown Refactoring

  • Replaced manual tokio runtime management with #[tokio::main] async main function
  • Refactored chain_sync() from an async function to a sync function that returns:
    • impl Future<Output = Result<()>> - the main synchronization loop
    • Vec<JoinHandle<Result<()>>> - all spawned background task handles
  • Changed validator_db parameter from Arc to &Arc to avoid transferring ownership
  • Added explicit shutdown sequence:
    a. Abort all background tasks via their handles
    b. Wait up to 1 second for tasks to complete
    c. Explicitly drop ValidatorDB to ensure clean database closure
  • Updated tests to match new chain_sync signature

Files Changed

File Changes
Cargo.lock Added log dependency to redb
bin/stateless-validator/src/main.rs Refactored main/chain_sync for clean shutdown
validator-core/Cargo.toml Enabled logging feature for redb

@flyq
flyq requested a review from yilongli December 25, 2025 16:11
Comment thread validator-core/Cargo.toml Outdated
Comment thread bin/stateless-validator/src/main.rs Outdated
);

let validator_logic = chain_sync(client.clone(), validator_db.clone(), config, chain_spec);
let (validator_logic, handles) = chain_sync(client, &validator_db, config, chain_spec)?;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

handles => bg_tasks

Comment thread bin/stateless-validator/src/main.rs Outdated
info!("[Main] Total execution time: {:?}", start.elapsed());

// Abort all background tasks and wait for them to finish (with timeout)
for handle in &handles {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

handle => bg_task

Comment thread bin/stateless-validator/src/main.rs Outdated
if let Err(e) = tokio::time::timeout(timeout, future::join_all(handles)).await {
warn!(
"[Main] Tokio runtime shutdown reached the {:?} timeout, error: {e}.",
timeout

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

inline timeout

Comment thread bin/stateless-validator/src/main.rs Outdated
info!("[Chain Sync] Task recovery completed");

// Spawn all background tasks
let mut handles = Vec::new();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

handles => bg_tasks

Comment thread bin/stateless-validator/src/main.rs Outdated
.await
.unwrap();
// Spawn background tasks and run chain sync
let (main_loop, handles) = chain_sync(client, &validator_db, config, chain_spec).unwrap();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

handles => bg_tasks

@flyq flyq changed the title Add redb-logging & close DB cleanly Enable redb database logging & close DB cleanly Dec 26, 2025
@yilongli yilongli closed this Jan 13, 2026
@flyq
flyq deleted the liquan/add_redb_log branch March 8, 2026 15:09
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