Skip to content

Commit c773444

Browse files
authored
Merge pull request #5421 from stacks-network/feat/remove-db-lock-panic
feat: remove panic in DB busy handler
2 parents c6528b8 + 04f5c9d commit c773444

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to the versioning scheme outlined in the [README.md](RE
99

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

1314
## [3.0.0.0.0]
1415

stacks-common/src/util/db.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,25 @@ pub fn update_lock_table(conn: &Connection) {
5151
/// Called by `rusqlite` if we are waiting too long on a database lock
5252
/// If called too many times, will assume a deadlock and panic
5353
pub fn tx_busy_handler(run_count: i32) -> bool {
54-
const TIMEOUT: Duration = Duration::from_secs(300);
5554
const AVG_SLEEP_TIME_MS: u64 = 100;
5655

56+
// Every ~5min, report an error with a backtrace
57+
// 5min * 60s/min * 1_000ms/s / 100ms
58+
const ERROR_COUNT: u32 = 3_000;
59+
5760
// First, check if this is taking unreasonably long. If so, it's probably a deadlock
5861
let run_count = run_count.unsigned_abs();
59-
let approx_time_elapsed =
60-
Duration::from_millis(AVG_SLEEP_TIME_MS.saturating_mul(u64::from(run_count)));
61-
if approx_time_elapsed > TIMEOUT {
62-
error!("Deadlock detected. Waited {} seconds (estimated) for database lock. Giving up", approx_time_elapsed.as_secs();
62+
if run_count > 0 && run_count % ERROR_COUNT == 0 {
63+
error!("Deadlock detected. Waited 5 minutes (estimated) for database lock.";
6364
"run_count" => run_count,
6465
"backtrace" => ?Backtrace::capture()
6566
);
6667
for (k, v) in LOCK_TABLE.lock().unwrap().iter() {
6768
error!("Database '{k}' last locked by {v}");
6869
}
69-
panic!("Deadlock in thread {:?}", thread::current().name());
7070
}
7171

7272
let mut sleep_time_ms = 2u64.saturating_pow(run_count);
73-
7473
sleep_time_ms = sleep_time_ms.saturating_add(thread_rng().gen_range(0..sleep_time_ms));
7574

7675
if sleep_time_ms > AVG_SLEEP_TIME_MS {

0 commit comments

Comments
 (0)