How to fix SQLITE_INTERRUPT: Operation terminated by sqlite3_interrupt() in SQLite

SQLiteINTERMEDIATEMEDIUMUpdated Dec 29, 2025· 159 views

This error occurs when a database operation is intentionally interrupted using the sqlite3_interrupt() function, typically in response to user cancellation. Handle it by properly finalizing interrupted statements and implementing timeout mechanisms.

Quick fix

Fixes SQLITE_INTERRUPT: Operation terminated by sqlite3_interrupt()

sqlite3_stmt *stmt;
int rc = sqlite3_step(stmt);
if (rc == SQLITE_INTERRUPT) {
  // Operation was interrupted
  sqlite3_finalize(stmt);  // Always finalize to avoid memory leak
}
  1. Finalize the interrupted statement
  2. Check if interruption was intentional
  3. Implement proper timeout handling with sqlite3_interrupt()
  4. Use sqlite3_progress_handler() as a safer alternative
sqlite — error
sqlite3_stmt *stmt;
int rc = sqlite3_step(stmt);
if (rc == SQLITE_INTERRUPT) {
// Operation was interrupted
sqlite3_finalize(stmt); // Always finalize to avoid memory leak
}
SQLITE_INTERRUPT: Operation terminated by sqlite3_interrupt()

What this error means

SQLITE_INTERRUPT (error code 9) is returned when a running SQL operation is terminated by calling sqlite3_interrupt(). This is not a bug—it's an intentional signal that an in-progress query or transaction has been cancelled. The sqlite3_interrupt() function is designed to halt long-running operations at the earliest safe point, making it useful for implementing query timeouts or responding to user cancellation requests.

Typical symptoms

  • Database operations fail with "SQLITE_INTERRUPT: Operation terminated by sqlite3_interrupt()"
  • Queries timeout or get cancelled unexpectedly during execution
  • INSERT, UPDATE, or DELETE operations roll back automatically when interrupted
  • Application receives error code 9 from SQLite
  • Long-running queries are abruptly halted mid-execution

Common causes

  1. Another thread or context called sqlite3_interrupt() on the database connection while a query was running
  2. A query timeout mechanism triggered and called sqlite3_interrupt() to cancel the operation
  3. User pressed Ctrl-C or clicked Cancel while a query was executing
  4. Application implemented cancellation logic that calls sqlite3_interrupt() to halt in-progress operations
  5. Progress handler callback or timeout handler terminated the query

How to fix "SQLITE_INTERRUPT: Operation terminated by sqlite3_interrupt()"

1Finalize the interrupted statement

After receiving SQLITE_INTERRUPT, always call sqlite3_finalize() on the prepared statement to release resources. Failing to finalize causes memory leaks.

c
sqlite3_stmt *stmt;
int rc = sqlite3_step(stmt);
if (rc == SQLITE_INTERRUPT) {
  // Operation was interrupted
  sqlite3_finalize(stmt);  // Always finalize to avoid memory leak
}
2Check if interruption was intentional

Determine whether the interrupt was expected (user-initiated) or unexpected (timeout). If intentional, handle gracefully. If unexpected, log the error and investigate the cause.

c
if (rc == SQLITE_INTERRUPT) {
  if (user_cancelled) {
    // Expected cancellation - inform user operation was cancelled
    log_user_message("Query cancelled by user");
  } else {
    // Unexpected interruption - log for debugging
    log_error("Unexpected SQLITE_INTERRUPT on query");
  }
}
3Implement proper timeout handling with sqlite3_interrupt()

If using sqlite3_interrupt() for timeouts, spawn a timer in a separate thread that calls sqlite3_interrupt() if a query exceeds the time limit. Always pair this with proper statement finalization.

c
// Timer thread that interrupts after timeout
void* timeout_thread(void* arg) {
  sqlite3 *db = (sqlite3*)arg;
  sleep(QUERY_TIMEOUT_SECONDS);
  sqlite3_interrupt(db);
  return NULL;
}

// Main query execution
pthread_t timer;
pthread_create(&timer, NULL, timeout_thread, db);
int rc = sqlite3_step(stmt);
if (rc == SQLITE_INTERRUPT) {
  sqlite3_finalize(stmt);
}
pthread_join(timer, NULL);
4Use sqlite3_progress_handler() as a safer alternative

For more controlled interruption without threading concerns, use sqlite3_progress_handler(). It allows you to check operation progress and return non-zero to stop execution, without risking closed database connections.

c
int progress_callback(void *arg) {
  time_t *start = (time_t*)arg;
  time_t elapsed = time(NULL) - *start;
  if (elapsed > QUERY_TIMEOUT_SECONDS) {
    return 1;  // Non-zero return stops execution
  }
  return 0;    // Zero return continues execution
}

time_t query_start = time(NULL);
sqlite3_progress_handler(db, PROGRESS_OPCODES, progress_callback, &query_start);
int rc = sqlite3_step(stmt);
5Handle transaction rollback after interrupted writes

If sqlite3_interrupt() is called during an INSERT, UPDATE, or DELETE within a transaction, the entire transaction is automatically rolled back. Verify transaction state after an interrupt and either retry or inform the user.

c
int rc = sqlite3_step(stmt);
if (rc == SQLITE_INTERRUPT) {
  sqlite3_finalize(stmt);
  // Transaction is automatically rolled back
  // Check if we need to retry or log the failure
  char *errmsg = sqlite3_errmsg(db);
  log_error("Transaction interrupted: %s", errmsg);
  // Optionally retry the operation
  retry_operation();
}

Advanced notes

Thread Safety: sqlite3_interrupt() is thread-safe and can be called from a different thread than the one executing the query. However, do not call sqlite3_interrupt() on a database connection that might close before the call completes, as this is undefined behavior. Duration of Effect: Once sqlite3_interrupt() is called, the interrupt flag remains active until all currently running statements complete. Any new statements started before the running statement count reaches zero will also be interrupted. After all statements finish, new statements are not affected. Checking Interrupt Status: Use sqlite3_is_interrupted(db) to check if an interrupt is currently in effect for a connection. This is useful in long-running loops to determine if a user has requested cancellation. Performance: The interrupt check only occurs at VDBE loop points during sqlite3_step(), not during prepare or finalize, so very fast queries may not be interruptible.

Frequently asked questions

What does "SQLITE_INTERRUPT: Operation terminated by sqlite3_interrupt()" mean?
SQLITE_INTERRUPT (error code 9) is returned when a running SQL operation is terminated by calling sqlite3_interrupt(). This is not a bug—it's an intentional signal that an in-progress query or transaction has been cancelled. The sqlite3_interrupt() function is designed to halt long-running operations at the earliest safe point, making it useful for implementing query timeouts or responding to user cancellation requests.
How do I fix "SQLITE_INTERRUPT: Operation terminated by sqlite3_interrupt()"?
This error occurs when a database operation is intentionally interrupted using the sqlite3_interrupt() function, typically in response to user cancellation. Handle it by properly finalizing interrupted statements and implementing timeout mechanisms.
What causes "SQLITE_INTERRUPT: Operation terminated by sqlite3_interrupt()"?
Another thread or context called sqlite3_interrupt() on the database connection while a query was running A query timeout mechanism triggered and called sqlite3_interrupt() to cancel the operation User pressed Ctrl-C or clicked Cancel while a query was executing
Is the "SQLITE_INTERRUPT: Operation terminated by sqlite3_interrupt()" error serious?
It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.

Official resources & further reading