Skip to content

Checking your reading list…

Retry

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

Databases 10 min read

What Happens When You Press Ctrl+C in MySQL? Inside the KILL QUERY Mechanism

This article explains how pressing Ctrl+C in the MySQL client triggers a KILL QUERY command, details the server's multi‑step handling—including thread coordination, flag setting, and transaction rollback—and clarifies differences when autocommit is on or off.

1. What the client does

When you press Ctrl + C in the mysql client, the client actually sends a KILL QUERY command to the server. For example, you can start the client with mysql -h127.0.0.1 -uroot -v, run an UPDATE statement, and press Ctrl + C before it finishes. The client output shows that it sends KILL QUERY 11 and the server replies with ERROR 1317 (70100): Query execution was interrupted.

mysql -h127.0.0.1 -uroot -v

2. KILL QUERY execution flow

The server creates a separate Kill thread to handle the command while the original Update thread continues processing the statement. The steps are:

Kill thread looks up the Update thread by query id (the id column from SHOW PROCESSLIST). If not found, the command ends.

Kill thread checks whether the client user has the KILL privilege. If not, the command ends.

Kill thread checks whether the Update thread is currently accessing data‑dictionary tables. If it is, the Update thread itself will take over the remaining steps.

If not, the Kill thread marks the Update thread’s killed flag as KILL_QUERY.

If the Update thread holds storage‑engine locks, they are released; if it is waiting for a lock, the wait is aborted.

If the Update thread is waiting on a condition variable ( current_cond), the Kill thread broadcasts to wake the waiters.

Both the Kill thread and the Update thread may execute steps 3‑6; the Update thread will finally see the KILL_QUERY flag and stop.

3. How a thread “kills itself”

MySQL does not abruptly terminate a running thread. Instead, the thread must notice the KILL_QUERY flag and cooperate by exiting its execution loops. The source code in sql/sql_update.cc shows a check:

bool Sql_cmd_update::update_single_table(THD *thd)
{
    ...
    while (true) {
        error = iterator->Read();
        // abort if error or thd->killed is set (i.e., KILL_QUERY)
        if (error || thd->killed) break;
        ...
    }
    ...
}

When the flag is set, the loop breaks and the statement is aborted.

4. Transaction rollback

If the aborted statement was part of a transaction, MySQL rolls back the transaction in mysql_execute_command():

int mysql_execute_command(THD *thd, bool first_level)
{
    ...
    if ((thd->is_error() && !early_error_on_rep_command) ||
        (thd->variables.option_bits & OPTION_MASTER_SQL_ERROR))
        trans_rollback_stmt(thd);
    else {
        thd->get_stmt_da()->set_overwrite_status(true);
        trans_commit_stmt(thd);
        thd->get_stmt_da()->set_overwrite_status(false);
    }
    ...
}

The thd->is_error() flag becomes true when the KILL QUERY interruption occurs, causing trans_rollback_stmt(thd) to be called.

5. Summary

Pressing Ctrl + C in the MySQL client sends a KILL QUERY to the server, which creates a Kill thread, marks the executing thread with KILL_QUERY, and forces it to abort. The abort triggers a transaction rollback unless the statement finished before the flag was set.

6. Extra notes

The description assumes autocommit is ON and no explicit BEGIN was issued. The same mechanism applies when a transaction is started explicitly or when autocommit is OFF; the only difference is that the rollback in section 4 affects only the current statement, while the overall transaction outcome depends on an explicit COMMIT or ROLLBACK.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

MySQLThread Managementtransaction rollbackKILL QUERY

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion