Skip to content

Commit

Permalink
Merge pull request #5421 from stacks-network/feat/remove-db-lock-panic
Browse files Browse the repository at this point in the history
feat: remove panic in DB busy handler
  • Loading branch information
obycode authored Nov 4, 2024
2 parents c6528b8 + 04f5c9d commit c773444
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to the versioning scheme outlined in the [README.md](RE

### Changed
- Add index for StacksBlockId to nakamoto block headers table (improves node performance)
- Remove the panic for reporting DB deadlocks (just error and continue waiting)

## [3.0.0.0.0]

Expand Down
13 changes: 6 additions & 7 deletions stacks-common/src/util/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,25 @@ pub fn update_lock_table(conn: &Connection) {
/// Called by `rusqlite` if we are waiting too long on a database lock
/// If called too many times, will assume a deadlock and panic
pub fn tx_busy_handler(run_count: i32) -> bool {
const TIMEOUT: Duration = Duration::from_secs(300);
const AVG_SLEEP_TIME_MS: u64 = 100;

// Every ~5min, report an error with a backtrace
// 5min * 60s/min * 1_000ms/s / 100ms
const ERROR_COUNT: u32 = 3_000;

// First, check if this is taking unreasonably long. If so, it's probably a deadlock
let run_count = run_count.unsigned_abs();
let approx_time_elapsed =
Duration::from_millis(AVG_SLEEP_TIME_MS.saturating_mul(u64::from(run_count)));
if approx_time_elapsed > TIMEOUT {
error!("Deadlock detected. Waited {} seconds (estimated) for database lock. Giving up", approx_time_elapsed.as_secs();
if run_count > 0 && run_count % ERROR_COUNT == 0 {
error!("Deadlock detected. Waited 5 minutes (estimated) for database lock.";
"run_count" => run_count,
"backtrace" => ?Backtrace::capture()
);
for (k, v) in LOCK_TABLE.lock().unwrap().iter() {
error!("Database '{k}' last locked by {v}");
}
panic!("Deadlock in thread {:?}", thread::current().name());
}

let mut sleep_time_ms = 2u64.saturating_pow(run_count);

sleep_time_ms = sleep_time_ms.saturating_add(thread_rng().gen_range(0..sleep_time_ms));

if sleep_time_ms > AVG_SLEEP_TIME_MS {
Expand Down

0 comments on commit c773444

Please sign in to comment.