Skip to content

Commit daa2d48

Browse files
Bug #21983865 UNEXPECTED DEADLOCK WITH INNODB_AUTOINC_LOCK_MODE=0
Problem: ======= This bug involves multiple thread waiting for AUTOINC_LOCK for the table and it leads to deadlock. Deadlock is happening because the count of the nodes visited exceeds the threshold limit(10^6). - Marking the subtree visited logic in the current deadlock search algorithm having some issue respect to table level lock. - While traversing the lock wait queue and if it reaches the current waiting lock then we mark the subtree as visited. - In row level lock, we are traversing from the head of the queue and eventually we will reach the current waiting lock in the lock wait queue. - But in table level lock, we are traversing from the tail of the queue, we will never reach the current waiting lock in the lock wait queue. Fix: ==== To fix visiting subtree logic for table level lock, traverse from the head of the lock wait queue. Reviewed-by: Debarun Banerjee<[email protected]> RB: 12147
1 parent 99751d8 commit daa2d48

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#
2+
# Bug #21983865 UNEXPECTED DEADLOCK WITH INNODB_AUTOINC_LOCK_MODE=0
3+
#
4+
create table t1(f1 int not null auto_increment primary key) engine=innodb;
5+
# Hold autoinc_lock on table t1 from connection con1
6+
set debug_sync='ib_after_row_insert SIGNAL others WAIT_FOR continue_others';
7+
insert into t1 values(NULL);
8+
# Create 40 connections and make it to wait for autoinc_lock on table t1.
9+
# Release the auto_inc lock on table t1 for connection con1
10+
set debug_sync='now SIGNAL continue_others';
11+
# Now all 40 connections can finish the insert operation on t1.
12+
drop table t1;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--innodb-autoinc-lock-mode=0
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
--source include/have_debug.inc
2+
--source include/have_innodb.inc
3+
--source include/have_debug_sync.inc
4+
--source include/count_sessions.inc
5+
6+
--echo #
7+
--echo # Bug #21983865 UNEXPECTED DEADLOCK WITH INNODB_AUTOINC_LOCK_MODE=0
8+
--echo #
9+
10+
create table t1(f1 int not null auto_increment primary key) engine=innodb;
11+
12+
--echo # Hold autoinc_lock on table t1 from connection con1
13+
connect (con1, localhost, root);
14+
set debug_sync='ib_after_row_insert SIGNAL others WAIT_FOR continue_others';
15+
--send insert into t1 values(NULL)
16+
17+
connection default;
18+
--echo # Create 40 connections and make it to wait for autoinc_lock on table t1.
19+
--disable_query_log
20+
set debug_sync='now WAIT_FOR others';
21+
let $1=2;
22+
while ($1 < 40) {
23+
connect (con$1,localhost,root);
24+
--send insert into t1 values(NULL)
25+
inc $1;
26+
connection default;
27+
}
28+
29+
connect (con40,localhost,root);
30+
--enable_query_log
31+
--echo # Release the auto_inc lock on table t1 for connection con1
32+
set debug_sync='now SIGNAL continue_others';
33+
34+
--echo # Now all 40 connections can finish the insert operation on t1.
35+
let $1=1;
36+
connection default;
37+
disconnect con40;
38+
while ($1 < 40) {
39+
connection con$1;
40+
--reap
41+
connection default;
42+
disconnect con$1;
43+
inc $1;
44+
}
45+
46+
drop table t1;
47+
--source include/wait_until_count_sessions.inc

storage/innobase/lock/lock0lock.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3616,7 +3616,7 @@ lock_get_next_lock(
36163616
ut_ad(heap_no == ULINT_UNDEFINED);
36173617
ut_ad(lock_get_type_low(lock) == LOCK_TABLE);
36183618

3619-
lock = UT_LIST_GET_PREV(un_member.tab_lock.locks, lock);
3619+
lock = UT_LIST_GET_NEXT(un_member.tab_lock.locks, lock);
36203620
}
36213621
} while (lock != NULL
36223622
&& lock->trx->lock.deadlock_mark > ctx->mark_start);
@@ -3666,7 +3666,8 @@ lock_get_first_lock(
36663666
} else {
36673667
*heap_no = ULINT_UNDEFINED;
36683668
ut_ad(lock_get_type_low(lock) == LOCK_TABLE);
3669-
lock = UT_LIST_GET_PREV(un_member.tab_lock.locks, lock);
3669+
dict_table_t* table = lock->un_member.tab_lock.table;
3670+
lock = UT_LIST_GET_FIRST(table->locks);
36703671
}
36713672

36723673
ut_a(lock != NULL);

0 commit comments

Comments
 (0)