List:Internals« Previous MessageNext Message »
From:ingo Date:November 3 2005 6:26pm
Subject:bk commit into 4.1 tree (ingo:1.2448) BUG#14397
View as plain text  
Below is the list of changes that have just been committed into a local
4.1 repository of mydev. When mydev does a push these changes will
be propagated to the main repository and, within 24 hours after the
push, to the public repository.
For information on how to access the public repository
see http://dev.mysql.com/doc/mysql/en/installing-source-tree.html

ChangeSet
  1.2448 05/11/03 18:26:52 ingo@stripped +7 -0
  Bug#14397 - OPTIMIZE TABLE with an open HANDLER causes a crash
  Version for 4.1.
  It fixes two problems:
  1. The cause of the bug was that we did not check the table version for
     the HANDLER ... READ commands. We did not notice when a table was
     replaced by a new one. This can happen during ALTER TABLE, REPAIR
     TABLE, and OPTIMIZE TABLE (there might be more cases). I call the fix
     for this problem "the primary bug fix".
  2. mysql_ha_flush() was not always called with a locked LOCK_open.
     Though the function comment clearly said it must.
     I changed the code so that the locking is done when required. I call
     the fix for this problem "the secondary fix".

  sql/sql_table.cc
    1.303 05/11/03 18:26:48 ingo@stripped +3 -3
    Bug#14397 - OPTIMIZE TABLE with an open HANDLER causes a crash
    Changed function calls for the secondary fix.

  sql/sql_handler.cc
    1.63 05/11/03 18:26:48 ingo@stripped +45 -4
    Bug#14397 - OPTIMIZE TABLE with an open HANDLER causes a crash
    The first two diffs make the primary bug fix.
    The rest is for the secondary fix.

  sql/sql_class.cc
    1.203 05/11/03 18:26:48 ingo@stripped +1 -1
    Bug#14397 - OPTIMIZE TABLE with an open HANDLER causes a crash
    Changed a function call for the secondary fix.

  sql/sql_base.cc
    1.260 05/11/03 18:26:48 ingo@stripped +4 -3
    Bug#14397 - OPTIMIZE TABLE with an open HANDLER causes a crash
    Changed function calls for the secondary fix.

  sql/mysql_priv.h
    1.368 05/11/03 18:26:48 ingo@stripped +2 -1
    Bug#14397 - OPTIMIZE TABLE with an open HANDLER causes a crash
    Changed a definition for the secondary fix.

  mysql-test/t/handler.test
    1.23 05/11/03 18:26:48 ingo@stripped +28 -0
    Bug#14397 - OPTIMIZE TABLE with an open HANDLER causes a crash
    The test case.

  mysql-test/r/handler.result
    1.23 05/11/03 18:26:48 ingo@stripped +18 -0
    Bug#14397 - OPTIMIZE TABLE with an open HANDLER causes a crash
    The test result.

# This is a BitKeeper patch.  What follows are the unified diffs for the
# set of deltas contained in the patch.  The rest of the patch, the part
# that BitKeeper cares about, is below these diffs.
# User:	ingo
# Host:	chilla.local
# Root:	/home/mydev/mysql-4.1-4100

--- 1.367/sql/mysql_priv.h	2005-09-14 14:31:32 +02:00
+++ 1.368/sql/mysql_priv.h	2005-11-03 18:26:48 +01:00
@@ -701,7 +701,8 @@
 int mysql_ha_close(THD *thd, TABLE_LIST *tables);
 int mysql_ha_read(THD *, TABLE_LIST *,enum enum_ha_read_modes,char *,
                List<Item> *,enum ha_rkey_function,Item *,ha_rows,ha_rows);
-int mysql_ha_flush(THD *thd, TABLE_LIST *tables, uint mode_flags);
+int mysql_ha_flush(THD *thd, TABLE_LIST *tables, uint mode_flags,
+                   bool is_locked);
 /* mysql_ha_flush mode_flags bits */
 #define MYSQL_HA_CLOSE_FINAL        0x00
 #define MYSQL_HA_REOPEN_ON_USAGE    0x01

--- 1.259/sql/sql_base.cc	2005-09-13 20:46:53 +02:00
+++ 1.260/sql/sql_base.cc	2005-11-03 18:26:48 +01:00
@@ -306,7 +306,8 @@
     thd->proc_info="Flushing tables";
 
     close_old_data_files(thd,thd->open_tables,1,1);
-    mysql_ha_flush(thd, tables, MYSQL_HA_REOPEN_ON_USAGE | MYSQL_HA_FLUSH_ALL);
+    mysql_ha_flush(thd, tables, MYSQL_HA_REOPEN_ON_USAGE | MYSQL_HA_FLUSH_ALL,
+                   TRUE);
     bool found=1;
     /* Wait until all threads has closed all the tables we had locked */
     DBUG_PRINT("info",
@@ -860,7 +861,7 @@
   }
 
   /* close handler tables which are marked for flush */
-  mysql_ha_flush(thd, (TABLE_LIST*) NULL, MYSQL_HA_REOPEN_ON_USAGE);
+  mysql_ha_flush(thd, (TABLE_LIST*) NULL, MYSQL_HA_REOPEN_ON_USAGE, TRUE);
 
   for (table=(TABLE*) hash_search(&open_cache,(byte*) key,key_length) ;
        table && table->in_use ;
@@ -1265,7 +1266,7 @@
   {
     thd->some_tables_deleted=0;
     close_old_data_files(thd,thd->open_tables,0,dropping_tables != 0);
-    mysql_ha_flush(thd, (TABLE_LIST*) NULL, MYSQL_HA_REOPEN_ON_USAGE);
+    mysql_ha_flush(thd, (TABLE_LIST*) NULL, MYSQL_HA_REOPEN_ON_USAGE, TRUE);
     if (!table_is_used(thd->open_tables,1))
       break;
     (void) pthread_cond_wait(&COND_refresh,&LOCK_open);

--- 1.202/sql/sql_class.cc	2005-10-14 15:34:49 +02:00
+++ 1.203/sql/sql_class.cc	2005-11-03 18:26:48 +01:00
@@ -353,7 +353,7 @@
     close_thread_tables(this);
   }
   mysql_ha_flush(this, (TABLE_LIST*) 0,
-                 MYSQL_HA_CLOSE_FINAL | MYSQL_HA_FLUSH_ALL);
+                 MYSQL_HA_CLOSE_FINAL | MYSQL_HA_FLUSH_ALL, FALSE);
   hash_free(&handler_tables_hash);
   delete_dynamic(&user_var_events);
   hash_free(&user_vars);

--- 1.302/sql/sql_table.cc	2005-09-12 14:08:36 +02:00
+++ 1.303/sql/sql_table.cc	2005-11-03 18:26:48 +01:00
@@ -220,7 +220,7 @@
   for (table=tables ; table ; table=table->next)
   {
     char *db=table->db;
-    mysql_ha_flush(thd, table, MYSQL_HA_CLOSE_FINAL);
+    mysql_ha_flush(thd, table, MYSQL_HA_CLOSE_FINAL, TRUE);
     if (!close_temporary_table(thd, db, table->real_name))
     {
       tmp_table_deleted=1;
@@ -1920,7 +1920,7 @@
   if (protocol->send_fields(&field_list, 1))
     DBUG_RETURN(-1);
 
-  mysql_ha_flush(thd, tables, MYSQL_HA_CLOSE_FINAL);
+  mysql_ha_flush(thd, tables, MYSQL_HA_CLOSE_FINAL, FALSE);
   for (table = tables; table; table = table->next)
   {
     char table_name[NAME_LEN*2+2];
@@ -2773,7 +2773,7 @@
     new_db= db;
   used_fields=create_info->used_fields;
 
-  mysql_ha_flush(thd, table_list, MYSQL_HA_CLOSE_FINAL);
+  mysql_ha_flush(thd, table_list, MYSQL_HA_CLOSE_FINAL, FALSE);
   /* DISCARD/IMPORT TABLESPACE is always alone in an ALTER TABLE */
   if (alter_info->tablespace_op != NO_TABLESPACE_OP)
     DBUG_RETURN(mysql_discard_or_import_tablespace(thd,table_list,

--- 1.22/mysql-test/r/handler.result	2004-10-07 09:50:09 +02:00
+++ 1.23/mysql-test/r/handler.result	2005-11-03 18:26:48 +01:00
@@ -445,3 +445,21 @@
 drop table t3;
 drop table t4;
 drop table t5;
+create table t1 (c1 int);
+insert into t1 values (1);
+handler t1 open;
+handler t1 read first;
+c1
+1
+send the below to another connection, do not wait for the result
+ optimize table t1;
+proceed with the normal connection
+handler t1 read next;
+c1
+1
+handler t1 close;
+read the result from the other connection
+Table	Op	Msg_type	Msg_text
+test.t1	optimize	status	OK
+proceed with the normal connection
+drop table t1;

--- 1.22/mysql-test/t/handler.test	2005-09-01 17:19:15 +02:00
+++ 1.23/mysql-test/t/handler.test	2005-11-03 18:26:48 +01:00
@@ -347,4 +347,32 @@
 drop table t4;
 drop table t5;
 
+#
+# Bug#14397 - OPTIMIZE TABLE with an open HANDLER causes a crash
+#
+create table t1 (c1 int);
+insert into t1 values (1);
+# client 1
+handler t1 open;
+handler t1 read first;
+# client 2
+connect (con2,localhost,root,,);
+connection con2;
+--exec echo send the below to another connection, do not wait for the result
+send optimize table t1;
+--sleep 1
+# client 1
+--exec echo proceed with the normal connection
+connection default;
+handler t1 read next;
+handler t1 close;
+# client 2
+--exec echo read the result from the other connection
+connection con2;
+reap;
+# client 1
+--exec echo proceed with the normal connection
+connection default;
+drop table t1;
+
 # End of 4.1 tests

--- 1.62/sql/sql_handler.cc	2005-06-07 22:43:18 +02:00
+++ 1.63/sql/sql_handler.cc	2005-11-03 18:26:48 +01:00
@@ -354,6 +354,7 @@
     ha_rows select_limit,ha_rows offset_limit)
 {
   TABLE_LIST    *hash_tables;
+  TABLE         **table_ptr;
   TABLE         *table;
   MYSQL_LOCK    *lock;
   List<Item>	list;
@@ -383,6 +384,27 @@
     DBUG_PRINT("info-in-hash",("'%s'.'%s' as '%s' tab %p",
                                hash_tables->db, hash_tables->real_name,
                                hash_tables->alias, table));
+    /* Table might have been flushed. */
+    if (table && (table->version != refresh_version))
+    {
+      /*
+        We must follow the thd->handler_tables chain, as we need the
+        address of the 'next' pointer referencing this table
+        for close_thread_table().
+      */
+      for (table_ptr= &(thd->handler_tables);
+           *table_ptr && (*table_ptr != table);
+           table_ptr= &(*table_ptr)->next)
+      {}
+      VOID(pthread_mutex_lock(&LOCK_open));
+      if (close_thread_table(thd, table_ptr))
+      {
+        /* Tell threads waiting for refresh that something has happened */
+        VOID(pthread_cond_broadcast(&COND_refresh));
+      }
+      VOID(pthread_mutex_unlock(&LOCK_open));
+      table= hash_tables->table= NULL;
+    }
     if (!table)
     {
       /*
@@ -616,6 +638,7 @@
                                 MYSQL_HA_REOPEN_ON_USAGE mark for reopen.
                                 MYSQL_HA_FLUSH_ALL flush all tables, not only
                                 those marked for flush.
+    is_locked                   If LOCK_open is locked.
 
   DESCRIPTION
     The list of HANDLER tables may be NULL, in which case all HANDLER
@@ -623,7 +646,6 @@
     If 'tables' is NULL and MYSQL_HA_FLUSH_ALL is not set,
     all HANDLER tables marked for flush are closed.
     Broadcasts a COND_refresh condition, for every table closed.
-    The caller must lock LOCK_open.
 
   NOTE
     Since mysql_ha_flush() is called when the base table has to be closed,
@@ -633,10 +655,12 @@
     0  ok
 */
 
-int mysql_ha_flush(THD *thd, TABLE_LIST *tables, uint mode_flags)
+int mysql_ha_flush(THD *thd, TABLE_LIST *tables, uint mode_flags,
+                   bool is_locked)
 {
   TABLE_LIST    *tmp_tables;
   TABLE         **table_ptr;
+  bool          did_lock= FALSE;
   DBUG_ENTER("mysql_ha_flush");
   DBUG_PRINT("enter", ("tables: %p  mode_flags: 0x%02x", tables, mode_flags));
 
@@ -662,6 +686,12 @@
                              (*table_ptr)->table_cache_key,
                              (*table_ptr)->real_name,
                              (*table_ptr)->table_name));
+          /* The first time it is required, lock for close_thread_table(). */
+          if (! did_lock && ! is_locked)
+          {
+            VOID(pthread_mutex_lock(&LOCK_open));
+            did_lock= TRUE;
+          }
           mysql_ha_flush_table(thd, table_ptr, mode_flags);
           continue;
         }
@@ -680,6 +710,12 @@
       if ((mode_flags & MYSQL_HA_FLUSH_ALL) ||
           ((*table_ptr)->version != refresh_version))
       {
+        /* The first time it is required, lock for close_thread_table(). */
+        if (! did_lock && ! is_locked)
+        {
+          VOID(pthread_mutex_lock(&LOCK_open));
+          did_lock= TRUE;
+        }
         mysql_ha_flush_table(thd, table_ptr, mode_flags);
         continue;
       }
@@ -687,6 +723,10 @@
     }
   }
 
+  /* Release the lock if it was taken by this function. */
+  if (did_lock)
+    VOID(pthread_mutex_unlock(&LOCK_open));
+
   DBUG_RETURN(0);
 }
 
@@ -718,8 +758,8 @@
                       table->table_name, mode_flags));
 
   if ((hash_tables= (TABLE_LIST*) hash_search(&thd->handler_tables_hash,
-                                        (byte*) (*table_ptr)->table_name,
-                                        strlen((*table_ptr)->table_name) + 1)))
+                                              (byte*) table->table_name,
+                                              strlen(table->table_name) + 1)))
   {
     if (! (mode_flags & MYSQL_HA_REOPEN_ON_USAGE))
     {
@@ -734,6 +774,7 @@
   }    
 
   (*table_ptr)->file->ha_index_or_rnd_end();
+  safe_mutex_assert_owner(&LOCK_open);
   if (close_thread_table(thd, table_ptr))
   {
     /* Tell threads waiting for refresh that something has happened */
Thread
bk commit into 4.1 tree (ingo:1.2448) BUG#14397ingo3 Nov