diff --git a/mysql-test/r/drop.result b/mysql-test/r/drop.result
index ff11905aa34515983437f8844a8b346055a50bb9..4626dd580f77b486121267e13c07355c195268ed 100644
--- a/mysql-test/r/drop.result
+++ b/mysql-test/r/drop.result
@@ -85,3 +85,8 @@ select 1;
 1
 unlock tables;
 End of 5.0 tests
+create database mysql_test;
+create table mysql_test.t1(f1 int);
+create table mysql_test.`#sql-347f_7` (f1 int);
+drop database mysql_test;
+End of 5.1 tests
diff --git a/mysql-test/t/drop.test b/mysql-test/t/drop.test
index a1451773e90948724934cccd7fece4bb9d2bea9e..a79044436ebd4193d4d0831960b395b73662ee16 100644
--- a/mysql-test/t/drop.test
+++ b/mysql-test/t/drop.test
@@ -122,3 +122,16 @@ disconnect addconroot2;
 connection default;
 
 --echo End of 5.0 tests
+
+#
+# Bug#30152 MySQLD crash duing alter table causes DROP DATABASE to FAIL due to temp file
+#
+create database mysql_test;
+create table mysql_test.t1(f1 int);
+create table mysql_test.`#sql-347f_7` (f1 int);
+create table mysql_test.`#sql-347f_8` (f1 int);
+drop table mysql_test.`#sql-347f_8`;
+copy_file $MYSQLTEST_VARDIR/master-data/mysql_test/t1.frm $MYSQLTEST_VARDIR/master-data/mysql_test/#sql-347f_6.frm;
+drop database mysql_test;
+
+--echo End of 5.1 tests
diff --git a/sql/sql_db.cc b/sql/sql_db.cc
index abbf2131957f423e35c1f76d2861baf5cc7b9345..88902e65a42aba8e25ba0a248bc186f6dc195b5d 100644
--- a/sql/sql_db.cc
+++ b/sql/sql_db.cc
@@ -1111,6 +1111,7 @@ static long mysql_rm_known_files(THD *thd, MY_DIR *dirp, const char *db,
       VOID(filename_to_tablename(file->name, table_list->table_name,
                                  strlen(file->name) + 1));
       table_list->alias= table_list->table_name;	// If lower_case_table_names=2
+      table_list->internal_tmp_table= is_prefix(file->name, tmp_file_prefix);
       /* Link into list */
       (*tot_list_next)= table_list;
       tot_list_next= &table_list->next_local;
diff --git a/sql/sql_table.cc b/sql/sql_table.cc
index bf15968f09bdf3c78e831e9a0f841733386694c0..75c97d8edd417fcc0cfa99282aa9174b6ba3d49e 100644
--- a/sql/sql_table.cc
+++ b/sql/sql_table.cc
@@ -1663,8 +1663,9 @@ int mysql_rm_table_part2(THD *thd, TABLE_LIST *tables, bool if_exists,
       }
       alias= (lower_case_table_names == 2) ? table->alias : table->table_name;
       /* remove .frm file and engine files */
-      path_length= build_table_filename(path, sizeof(path),
-                                        db, alias, reg_ext, 0);
+      path_length= build_table_filename(path, sizeof(path), db, alias, reg_ext,
+                                        table->internal_tmp_table ?
+                                        FN_IS_TMP : 0);
     }
     if (drop_temporary ||
         (table_type == NULL &&        
diff --git a/sql/table.h b/sql/table.h
index 2bbd71b70c6757dd3d1653f72610b86c419bfc72..440f3a3d5cf8997c1d210e9d9e8c832e238a832a 100644
--- a/sql/table.h
+++ b/sql/table.h
@@ -1081,6 +1081,7 @@ struct TABLE_LIST
     ... SELECT implementation).
   */
   bool          create;
+  bool          internal_tmp_table;
 
 
   /* View creation context. */