Bug #31148 bool close_thread_table(THD*, TABLE**): Assertion `table->key_read == 0' failed.
Submitted: 22 Sep 2007 14:43 Modified: 9 Nov 2007 21:17
Reporter: Shane Bester (Platinum Quality Contributor) Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: General Severity:S3 (Non-critical)
Version:5.1.23-debug, 6.0.3-debug OS:Any
Assigned to: Alexander Nozdrin CPU Architecture:Any
Tags: assertion, crash

[22 Sep 2007 14:43] Shane Bester
Description:
Version: '5.1.23-beta-debug'  socket: '/tmp/mysql.sock'  port: 3306  yes
mysqld: sql_base.cc:1263: bool close_thread_table(THD*, TABLE**): Assertion `table->key_read == 0' failed.
070922 15:43:02 - mysqld got signal 6;
This could be because you hit a bug. It is also possible that this binary
or one of the libraries it was linked against is corrupt, improperly built,
or misconfigured. This error can also be caused by malfunctioning hardware.
We will try our best to scrape up some info that will hopefully help diagnose
the problem, but since we have already crashed, something is definitely wrong
and this may fail.

key_buffer_size=8388572
read_buffer_size=131072
max_used_connections=2
max_threads=151
threads_connected=1
It is possible that mysqld could use up to 
key_buffer_size + (read_buffer_size + sort_buffer_size)*max_threads = 337620 K
bytes of memory
Hope that's ok; if not, decrease some variables in the equation.

thd: 0x8dbc7b8
Attempting backtrace. You can use the following information to find out
where mysqld died. If you see no messages after this, something went
terribly wrong...
Cannot determine thread, fp=0x42896944, backtrace may not be correct.
Stack range sanity check OK, backtrace follows:
0x820111b handle_segfault + 541
0xffffe410 _end + -142173536
0x400edb75 _end + 932549125
0x400e5903 _end + 932515731
0x8246562 close_thread_table(THD*, st_table**) + 280
0x824634b close_thread_tables(THD*, bool, bool) + 537
0x820d727 dispatch_command(enum_server_command, THD*, char*, unsigned int) + 5209
0x820c2c2 do_command(THD*) + 600
0x820acbd handle_one_connection + 255
0x40038aa7 _end + 931807543
0x4017ec2e _end + 933143230
New value of fp=(nil) failed sanity check, terminating stack trace!
Please read http://dev.mysql.com/doc/refman/5.1/en/resolve-stack-dump.html
and follow instructions on how to resolve the stack trace.
Resolved stack trace is much more helpful in diagnosing the
problem, so please do resolve it
Trying to get some variables.
Some pointers may be invalid and cause the dump to abort...

How to repeat:
drop table if exists t1;
create table `t1` (a int primary key auto_increment)engine=myisam;
insert into t1 values (),(),();
select 1 from t1 order by ( select 1 from t1 group by greatest(last_insert_id(),`t1`.`a`) asc limit 1);

The query will return some results to client, but the server will have asserted in the background.  

Suggested fix:
Evaluate why does this assertion get hit.
[22 Sep 2007 14:48] MySQL Verification Team
debug outputs from windows build of 5.1.21-debug

Attachment: bug31148-5.1.21-debug-windows-stack-trace.txt (text/plain), 6.62 KiB.

[20 Oct 2007 17:46] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/35987

ChangeSet@1.2595, 2007-10-20 21:48:15+04:00, anozdrin@station. +3 -0
  Fix for BUG#31148: bool close_thread_table(THD*, TABLE**):
  Assertion `table->key_read == 0' failed.
  
  The problem was that key_read on a table in a sub-select was not
  properly reset. That happens because the code responsible for that
  is copy&pasted all around the server. In some place, it was obviously
  forgotten to be pasted.
  
  The fix is to reset key_read properly.
[20 Oct 2007 17:50] Alexander Nozdrin
Pushed into 5.1-runtime.
[7 Nov 2007 21:59] Bugs System
Pushed into 6.0.4-alpha
[7 Nov 2007 22:01] Bugs System
Pushed into 5.1.23-rc
[9 Nov 2007 21:17] Paul DuBois
Noted in 5.1.23, 6.0.4 changelogs.

A key read in a subquery could be impproperly reset, resulting in an
assertion failure.
[12 Nov 2007 19:23] Paul DuBois
Correction: No changelog entry is needed, per Joro.