diff -urB mysql-5.0.68-percona-highperf/include/mysql_com.h mysql-5.0.68-percona-highperf-ifidle/include/mysql_com.h
--- mysql-5.0.68-percona-highperf/include/mysql_com.h	2008-09-08 23:44:48.000000000 +0000
+++ mysql-5.0.68-percona-highperf-ifidle/include/mysql_com.h	2008-10-06 17:06:26.000000000 +0000
@@ -166,6 +166,7 @@
 #define NET_WAIT_TIMEOUT	8*60*60		/* Wait for new query */
 
 #define ONLY_KILL_QUERY         1
+#define ONLY_KILL_IF_IDLE       2
 
 struct st_vio;					/* Only C */
 typedef struct st_vio Vio;
diff -urB mysql-5.0.68-percona-highperf/sql/lex.h mysql-5.0.68-percona-highperf-ifidle/sql/lex.h
--- mysql-5.0.68-percona-highperf/sql/lex.h	2008-09-08 23:44:48.000000000 +0000
+++ mysql-5.0.68-percona-highperf-ifidle/sql/lex.h	2008-10-06 17:06:58.000000000 +0000
@@ -229,6 +229,7 @@
   { "HOUR_SECOND",	SYM(HOUR_SECOND_SYM)},
   { "IDENTIFIED",	SYM(IDENTIFIED_SYM)},
   { "IF",		SYM(IF)},
+  { "IF_IDLE",          SYM(IF_IDLE_SYM)},
   { "IGNORE",		SYM(IGNORE_SYM)},
   { "IMPORT",		SYM(IMPORT)},
   { "IN",		SYM(IN_SYM)},
diff -urB mysql-5.0.68-percona-highperf/sql/mysql_priv.h mysql-5.0.68-percona-highperf-ifidle/sql/mysql_priv.h
--- mysql-5.0.68-percona-highperf/sql/mysql_priv.h	2008-09-08 23:44:48.000000000 +0000
+++ mysql-5.0.68-percona-highperf-ifidle/sql/mysql_priv.h	2008-10-06 17:15:44.000000000 +0000
@@ -86,7 +86,8 @@
 			       CHARSET_INFO *from_cs,
 			       uint32 max_res_length,
 			       CHARSET_INFO *to_cs, uint32 *result_length);
-void kill_one_thread(THD *thd, ulong id, bool only_kill_query);
+void kill_one_thread(THD *thd, ulong id, bool only_kill_query,
+                     bool only_kill_if_idle);
 bool net_request_file(NET* net, const char* fname);
 char* query_table_status(THD *thd,const char *db,const char *table_name);
 
diff -urB mysql-5.0.68-percona-highperf/sql/sql_parse.cc mysql-5.0.68-percona-highperf-ifidle/sql/sql_parse.cc
--- mysql-5.0.68-percona-highperf/sql/sql_parse.cc	2008-09-08 23:44:48.000000000 +0000
+++ mysql-5.0.68-percona-highperf-ifidle/sql/sql_parse.cc	2008-10-06 17:17:30.000000000 +0000
@@ -2404,7 +2404,7 @@
   {
     statistic_increment(thd->status_var.com_stat[SQLCOM_KILL], &LOCK_status);
     ulong id=(ulong) uint4korr(packet);
-    kill_one_thread(thd,id,false);
+    kill_one_thread(thd,id,false,false);
     break;
   }
   case COM_SET_OPTION:
@@ -4573,7 +4573,8 @@
 		 MYF(0));
       goto error;
     }
-    kill_one_thread(thd, (ulong)it->val_int(), lex->type & ONLY_KILL_QUERY);
+    kill_one_thread(thd, (ulong)it->val_int(), lex->type & ONLY_KILL_QUERY,
+                    lex->type & ONLY_KILL_IF_IDLE);
     break;
   }
 #ifndef NO_EMBEDDED_ACCESS_CHECKS
@@ -7573,8 +7574,8 @@
   NOTES
     This is written such that we have a short lock on LOCK_thread_count
 */
-
-void kill_one_thread(THD *thd, ulong id, bool only_kill_query)
+void kill_one_thread(THD *thd, ulong id, bool only_kill_query,
+                     bool only_kill_if_idle)
 {
   THD *tmp;
   uint error=ER_NO_SUCH_THREAD;
@@ -7594,7 +7595,11 @@
     if ((thd->security_ctx->master_access & SUPER_ACL) ||
 	!strcmp(thd->security_ctx->user, tmp->security_ctx->user))
     {
-      tmp->awake(only_kill_query ? THD::KILL_QUERY : THD::KILL_CONNECTION);
+      // Kill a session when either:
+      // * it is sleeping and only_kill_if_idle is true
+      // * only_kill_if_idle is false
+      if (!only_kill_if_idle || tmp->command == COM_SLEEP)
+        tmp->awake(only_kill_query ? THD::KILL_QUERY : THD::KILL_CONNECTION);
       error=0;
     }
     else
diff -urB mysql-5.0.68-percona-highperf/sql/sql_yacc.yy mysql-5.0.68-percona-highperf-ifidle/sql/sql_yacc.yy
--- mysql-5.0.68-percona-highperf/sql/sql_yacc.yy	2008-09-08 23:44:48.000000000 +0000
+++ mysql-5.0.68-percona-highperf-ifidle/sql/sql_yacc.yy	2008-10-06 17:38:55.000000000 +0000
@@ -462,7 +462,7 @@
   Currently there are 240 shift/reduce conflicts.
   We should not introduce new conflicts any more.
 */
-%expect 240
+%expect 241
 
 %token  END_OF_INPUT
 
@@ -672,6 +672,7 @@
 %token  IDENTIFIED_SYM
 %token  IDENT_QUOTED
 %token  IF
+%token  IF_IDLE_SYM
 %token  IGNORE_SYM
 %token  IMPORT
 %token  INDEXES
@@ -8464,6 +8465,7 @@
 	/* empty */	 { Lex->type= 0; }
 	| CONNECTION_SYM { Lex->type= 0; }
 	| QUERY_SYM      { Lex->type= ONLY_KILL_QUERY; }
+	| IF_IDLE_SYM    { Lex->type= ONLY_KILL_IF_IDLE; }
         ;
 
 /* change database */
@@ -9436,6 +9438,7 @@
 	| HOSTS_SYM		{}
 	| HOUR_SYM		{}
 	| IDENTIFIED_SYM	{}
+        | IF_IDLE_SYM           {}
 	| INVOKER_SYM		{}
 	| IMPORT		{}
 	| INDEXES		{}

