Skip to content

Commit 51254da

Browse files
committed
MDEV-15359 Thread stay in "cleaning up" status after finishing
make thd_get_error_context_description() to show not just thd->proc_info, but exactly the same thread state that SHOW PROCESSLIST shows.
1 parent d2e1ed8 commit 51254da

File tree

4 files changed

+111
-97
lines changed

4 files changed

+111
-97
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
create table t1 (a int) engine=innodb;
2+
start transaction;
3+
insert t1 values (1);
4+
state from show engine innodb status
5+
6+
state from show processlist
7+
8+
drop table t1;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#
2+
# MDEV-15359 Thread stay in "cleaning up" status after finishing
3+
#
4+
source include/have_innodb.inc;
5+
6+
create table t1 (a int) engine=innodb;
7+
start transaction;
8+
insert t1 values (1);
9+
let id=`select connection_id()`;
10+
connect con2,localhost,root;
11+
let s=query_get_value(show engine innodb status,Status,1);
12+
disable_query_log;
13+
eval select regexp_replace("$s", '(?s)^.*MySQL thread id $id,.*root([^\n]*)\n.*', '\\\\1') as `state from show engine innodb status`;
14+
eval select state as `state from show processlist` from information_schema.processlist where id = $id;
15+
enable_query_log;
16+
disconnect con2;
17+
connection default;
18+
drop table t1;

sql/sql_class.cc

Lines changed: 0 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -717,103 +717,6 @@ extern "C"
717717
}
718718

719719

720-
/**
721-
Dumps a text description of a thread, its security context
722-
(user, host) and the current query.
723-
724-
@param thd thread context
725-
@param buffer pointer to preferred result buffer
726-
@param length length of buffer
727-
@param max_query_len how many chars of query to copy (0 for all)
728-
729-
@return Pointer to string
730-
*/
731-
732-
extern "C"
733-
char *thd_get_error_context_description(THD *thd, char *buffer,
734-
unsigned int length,
735-
unsigned int max_query_len)
736-
{
737-
String str(buffer, length, &my_charset_latin1);
738-
const Security_context *sctx= &thd->main_security_ctx;
739-
char header[256];
740-
int len;
741-
742-
mysql_mutex_lock(&LOCK_thread_count);
743-
744-
/*
745-
The pointers thd->query and thd->proc_info might change since they are
746-
being modified concurrently. This is acceptable for proc_info since its
747-
values doesn't have to very accurate and the memory it points to is static,
748-
but we need to attempt a snapshot on the pointer values to avoid using NULL
749-
values. The pointer to thd->query however, doesn't point to static memory
750-
and has to be protected by thd->LOCK_thd_data or risk pointing to
751-
uninitialized memory.
752-
*/
753-
const char *proc_info= thd->proc_info;
754-
755-
len= my_snprintf(header, sizeof(header),
756-
"MySQL thread id %lu, OS thread handle 0x%lx, query id %lu",
757-
thd->thread_id, (ulong) thd->real_id, (ulong) thd->query_id);
758-
str.length(0);
759-
str.append(header, len);
760-
761-
if (sctx->host)
762-
{
763-
str.append(' ');
764-
str.append(sctx->host);
765-
}
766-
767-
if (sctx->ip)
768-
{
769-
str.append(' ');
770-
str.append(sctx->ip);
771-
}
772-
773-
if (sctx->user)
774-
{
775-
str.append(' ');
776-
str.append(sctx->user);
777-
}
778-
779-
if (proc_info)
780-
{
781-
str.append(' ');
782-
str.append(proc_info);
783-
}
784-
785-
/* Don't wait if LOCK_thd_data is used as this could cause a deadlock */
786-
if (!mysql_mutex_trylock(&thd->LOCK_thd_data))
787-
{
788-
if (thd->query())
789-
{
790-
if (max_query_len < 1)
791-
len= thd->query_length();
792-
else
793-
len= MY_MIN(thd->query_length(), max_query_len);
794-
str.append('\n');
795-
str.append(thd->query(), len);
796-
}
797-
mysql_mutex_unlock(&thd->LOCK_thd_data);
798-
}
799-
mysql_mutex_unlock(&LOCK_thread_count);
800-
801-
if (str.c_ptr_safe() == buffer)
802-
return buffer;
803-
804-
/*
805-
We have to copy the new string to the destination buffer because the string
806-
was reallocated to a larger buffer to be able to fit.
807-
*/
808-
DBUG_ASSERT(buffer != NULL);
809-
length= MY_MIN(str.length(), length-1);
810-
memcpy(buffer, str.c_ptr_quick(), length);
811-
/* Make sure that the new string is null terminated */
812-
buffer[length]= '\0';
813-
return buffer;
814-
}
815-
816-
817720
#if MARIA_PLUGIN_INTERFACE_VERSION < 0x0200
818721
/**
819722
TODO: This function is for API compatibility, remove it eventually.

sql/sql_show.cc

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9773,3 +9773,88 @@ static void get_cs_converted_string_value(THD *thd,
97739773
return;
97749774
}
97759775
#endif
9776+
9777+
/**
9778+
Dumps a text description of a thread, its security context
9779+
(user, host) and the current query.
9780+
9781+
@param thd thread context
9782+
@param buffer pointer to preferred result buffer
9783+
@param length length of buffer
9784+
@param max_query_len how many chars of query to copy (0 for all)
9785+
9786+
@return Pointer to string
9787+
*/
9788+
9789+
extern "C"
9790+
char *thd_get_error_context_description(THD *thd, char *buffer,
9791+
unsigned int length,
9792+
unsigned int max_query_len)
9793+
{
9794+
String str(buffer, length, &my_charset_latin1);
9795+
const Security_context *sctx= &thd->main_security_ctx;
9796+
char header[256];
9797+
int len;
9798+
9799+
mysql_mutex_lock(&LOCK_thread_count);
9800+
9801+
len= my_snprintf(header, sizeof(header),
9802+
"MySQL thread id %lu, OS thread handle 0x%lx, query id %lu",
9803+
thd->thread_id, (ulong) thd->real_id, (ulong) thd->query_id);
9804+
str.length(0);
9805+
str.append(header, len);
9806+
9807+
if (sctx->host)
9808+
{
9809+
str.append(' ');
9810+
str.append(sctx->host);
9811+
}
9812+
9813+
if (sctx->ip)
9814+
{
9815+
str.append(' ');
9816+
str.append(sctx->ip);
9817+
}
9818+
9819+
if (sctx->user)
9820+
{
9821+
str.append(' ');
9822+
str.append(sctx->user);
9823+
}
9824+
9825+
/* Don't wait if LOCK_thd_data is used as this could cause a deadlock */
9826+
if (!mysql_mutex_trylock(&thd->LOCK_thd_data))
9827+
{
9828+
if (const char *info= thread_state_info(thd))
9829+
{
9830+
str.append(' ');
9831+
str.append(info);
9832+
}
9833+
9834+
if (thd->query())
9835+
{
9836+
if (max_query_len < 1)
9837+
len= thd->query_length();
9838+
else
9839+
len= MY_MIN(thd->query_length(), max_query_len);
9840+
str.append('\n');
9841+
str.append(thd->query(), len);
9842+
}
9843+
mysql_mutex_unlock(&thd->LOCK_thd_data);
9844+
}
9845+
mysql_mutex_unlock(&LOCK_thread_count);
9846+
9847+
if (str.c_ptr_safe() == buffer)
9848+
return buffer;
9849+
9850+
/*
9851+
We have to copy the new string to the destination buffer because the string
9852+
was reallocated to a larger buffer to be able to fit.
9853+
*/
9854+
DBUG_ASSERT(buffer != NULL);
9855+
length= MY_MIN(str.length(), length-1);
9856+
memcpy(buffer, str.c_ptr_quick(), length);
9857+
/* Make sure that the new string is null terminated */
9858+
buffer[length]= '\0';
9859+
return buffer;
9860+
}

0 commit comments

Comments
 (0)