1
0
mirror of https://github.com/MariaDB/server.git synced 2025-07-30 16:24:05 +03:00

Bug #54332 Deadlock with two connections doing LOCK TABLE+INSERT DELAYED

The problem was that deadlocks involving INSERT DELAYED were not detected.

The reason for this is that two threads are involved in INSERT DELAYED:
the connection thread and the handler thread. The connection thread would
wait while the handler thread acquired locks and opened the table.
In essence, this adds an edge to the wait-for-graph between the 
connection thread and the handler thread that the deadlock detector is
unaware of. Therefore many deadlocks involving INSERT DELAYED were not 
detected.

This patch fixes the problem by having the connection thread acquire the
metadata lock the table before starting the handler thread. This allows the
deadlock detector to detect any possible deadlocks resulting from trying to
acquire a metadata lock the table. If a metadata lock is successfully acquired,
the handler thread is started and given a copy of the ticket representing the
metadata lock. When the handler thread then tries to lock and open the table,
it will find that it already has the metadata lock and therefore not acquire
any new metadata locks.

Test cases added to delayed.test.
This commit is contained in:
Jon Olav Hauglid
2010-08-23 17:42:53 +02:00
parent 6e7687ddbb
commit d2210981e3
3 changed files with 297 additions and 10 deletions

View File

@ -388,3 +388,162 @@ CREATE TABLE t1 LIKE t2;
DROP TABLE t2;
DROP TABLE t1;
--echo #
--echo # Bug#54332 Deadlock with two connections doing LOCK TABLE+INSERT DELAYED
--echo #
--disable_warnings
DROP TABLE IF EXISTS t1, t2;
--enable_warnings
CREATE TABLE t1 (a INT);
CREATE TABLE t2 (a INT);
CREATE TABLE t3 (a INT);
--echo # Test 1: Using LOCK TABLE
--echo # Connection con1
connect (con1, localhost, root);
LOCK TABLE t1 WRITE;
--echo # Connection default
connection default;
LOCK TABLE t2 WRITE;
--echo # Sending:
--send INSERT DELAYED INTO t1 VALUES (1)
--echo # Connection con1
connection con1;
--echo # Wait until INSERT DELAYED is blocked on table 't1'.
let $wait_condition=
SELECT COUNT(*) = 1 FROM information_schema.processlist
WHERE state = "Waiting for table metadata lock"
AND info = "INSERT DELAYED INTO t1 VALUES (1)";
--source include/wait_condition.inc
--error ER_LOCK_DEADLOCK
INSERT DELAYED INTO t2 VALUES (1);
UNLOCK TABLES;
--echo # Connection default
connection default;
--echo # Reaping: INSERT DELAYED INTO t1 VALUES (1)
--reap
UNLOCK TABLES;
--echo # Test 2: Using ALTER TABLE
START TRANSACTION;
SELECT * FROM t1 WHERE a=0;
--echo # Connection con1
connection con1;
--echo # Sending:
--send ALTER TABLE t1 COMMENT 'test'
--echo # Connection default
connection default;
--echo # Wait until ALTER TABLE is blocked on table 't1'.
let $wait_condition=
SELECT COUNT(*) = 1 FROM information_schema.processlist
WHERE state = "Waiting for table metadata lock"
AND info = "ALTER TABLE t1 COMMENT 'test'";
--source include/wait_condition.inc
--error ER_LOCK_DEADLOCK
INSERT DELAYED INTO t1 VALUES (3);
COMMIT;
--echo # Connection con1
connection con1;
--echo # Reaping: ALTER TABLE t1 COMMENT 'test'
--reap
--echo # Test 3: Using RENAME TABLE
--echo # Connection default
connection default;
START TRANSACTION;
INSERT INTO t2 VALUES (1);
--echo # Connection con1
connection con1;
--echo # Sending:
--send RENAME TABLE t1 to t5, t2 to t4
--echo # Connection default
connection default;
--echo # Wait until RENAME TABLE is blocked on table 't1'.
let $wait_condition=
SELECT COUNT(*) = 1 FROM information_schema.processlist
WHERE state = "Waiting for table metadata lock"
AND info = "RENAME TABLE t1 to t5, t2 to t4";
--source include/wait_condition.inc
--error ER_LOCK_DEADLOCK
INSERT DELAYED INTO t1 VALUES (4);
COMMIT;
--echo # Connection con1
connection con1;
--echo # Reaping: RENAME TABLE t1 to t5, t2 to t4
--reap
--echo # Connection default
connection default;
--echo # Reverting the renames
RENAME TABLE t5 to t1, t4 to t2;
--echo # Test 4: Two INSERT DELAYED on the same table
START TRANSACTION;
INSERT INTO t2 VALUES (1);
--echo # Connection con2
connect (con2, localhost, root);
--send LOCK TABLE t1 WRITE, t2 WRITE
--echo # Connection con1
connection con1;
--echo # Wait until LOCK TABLE is blocked on table 't2'.
let $wait_condition=
SELECT COUNT(*) = 1 FROM information_schema.processlist
WHERE state = "Waiting for table metadata lock"
AND info = "LOCK TABLE t1 WRITE, t2 WRITE";
--source include/wait_condition.inc
--send INSERT DELAYED INTO t1 VALUES (5)
--echo # Connection default
connection default;
--echo # Wait until INSERT DELAYED is blocked on table 't1'.
let $wait_condition=
SELECT COUNT(*) = 1 FROM information_schema.processlist
WHERE state = "Waiting for table metadata lock"
AND info = "INSERT DELAYED INTO t1 VALUES (5)";
--source include/wait_condition.inc
--error ER_LOCK_DEADLOCK
INSERT DELAYED INTO t1 VALUES (6);
COMMIT;
--echo # Connection con2
connection con2;
--echo # Reaping: LOCK TABLE t1 WRITE, t2 WRITE
--reap
UNLOCK TABLES;
--echo # Connection con1
connection con1;
--echo # Reaping: INSERT DELAYED INTO t1 VALUES (5)
--reap
--echo # Connection con2
connection con2;
disconnect con2;
--source include/wait_until_disconnected.inc
--echo # Connection con1
connection con1;
disconnect con1;
--source include/wait_until_disconnected.inc
--echo # Connection default
connection default;
DROP TABLE t1, t2, t3;