Skip to content

Commit 621501f

Browse files
committed
MDEV-25606: Concurrent CREATE TRIGGER statements mix up in binlog and break replication
The bug is that we don't have a a lock on the trigger name, so it is possible for two threads to try to create the same trigger at the same time and both thinks that they have succeed. Same thing can happen with drop trigger or a combinations of create and drop trigger. Fixed by adding a mdl lock for the trigger name for the duration of the create/drop. Patch tested by Elena
1 parent 5d86848 commit 621501f

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

sql/sql_trigger.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ bool mysql_create_or_drop_trigger(THD *thd, TABLE_LIST *tables, bool create)
397397
String stmt_query;
398398
bool lock_upgrade_done= FALSE;
399399
MDL_ticket *mdl_ticket= NULL;
400+
MDL_request mdl_request_for_trn;
400401
Query_tables_list backup;
401402
DBUG_ENTER("mysql_create_or_drop_trigger");
402403

@@ -446,6 +447,16 @@ bool mysql_create_or_drop_trigger(THD *thd, TABLE_LIST *tables, bool create)
446447
DBUG_RETURN(TRUE);
447448
}
448449

450+
/* Protect against concurrent create/drop */
451+
MDL_REQUEST_INIT(&mdl_request_for_trn, MDL_key::TABLE,
452+
create ? tables->db.str : thd->lex->spname->m_db.str,
453+
thd->lex->spname->m_name.str,
454+
MDL_EXCLUSIVE, MDL_EXPLICIT);
455+
if (thd->mdl_context.acquire_lock(&mdl_request_for_trn,
456+
thd->variables.lock_wait_timeout))
457+
goto end;
458+
459+
449460
if (!create)
450461
{
451462
bool if_exists= thd->lex->if_exists();
@@ -635,6 +646,9 @@ bool mysql_create_or_drop_trigger(THD *thd, TABLE_LIST *tables, bool create)
635646
thd->lex->spname->m_name.str, static_cast<uint>(thd->lex->spname->m_name.length));
636647
}
637648

649+
if (mdl_request_for_trn.ticket)
650+
thd->mdl_context.release_lock(mdl_request_for_trn.ticket);
651+
638652
DBUG_RETURN(result);
639653
#ifdef WITH_WSREP
640654
wsrep_error_label:

0 commit comments

Comments
 (0)