Bug #36518 Plugin refcount leak with cursors and exceptions
Submitted: 5 May 2008 21:32 Modified: 6 May 2008 14:23
Reporter: Marc ALFF Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Stored Routines Severity:S3 (Non-critical)
Version:5.1 and up OS:Any
Assigned to: CPU Architecture:Any

[5 May 2008 21:32] Marc ALFF
Description:
When executing code involving a cursor that raise an exception,
the state of the cursor is not completely cleaned up,
which leads to reference count leaks in the MEMORY storage engine plug-in.

How to repeat:
--disable_warnings
drop table if exists t1 ;
drop table if exists t2 ;
drop procedure if exists p1 ;
drop function if exists f1 ;
--enable_warnings

create table t1 (s1 int) ;
create table t2 (s1 int unique) ;
insert into t1 values (1) ;

delimiter $$;

create procedure p1()
begin
  declare a int;
  declare c cursor for select f1() from t1;
  declare continue handler for sqlexception
    select "caught an exception";
  declare continue handler for 1326
    select "caught cursor is not open";

  select "Before open";
  open c;
  select "Before fetch";
  fetch c into a;
  select "Before close";
  close c;
end $$

create function f1() returns int
begin
  insert into t2 values (2), (2);
  return 5;
end $$

delimiter ;$$

## FIXME : MEMORY plugin warning
call p1() ;

drop table t1 ;
drop procedure p1 ;
drop function f1 ;

results:

call p1() ;
Before open
Before open
caught an exception
caught an exception
Before fetch
Before fetch
caught cursor is not open
caught cursor is not open
Before close
Before close
caught cursor is not open
caught cursor is not open
--> this is as expected

mysql-test-run: WARNING: Got errors/warnings while running tests, please examine                                                           "/home/malff/TREE/mysql-5.1-base/mysql-test/var/log/warnings" for details.

master.err: main.marc: 080506  0:24:09 [ERROR] Plugin 'MEMORY' has ref_count=1 after shutdown.
--> this is a bug
[6 May 2008 14:23] MySQL Verification Team
Thank you for the bug report.

miguel@hegel:~/dbs/mysql-5.1/mysql-test> cat /home/miguel/dbs/mysql-5.1/mysql-test/var/log/warnings
master.err: main.bug36518: 080506 18:19:24 [ERROR] Plugin 'MEMORY' has ref_count=1 after shutdown.
miguel@hegel:~/dbs/mysql-5.1/mysql-test>
[14 Nov 2008 11:37] MySQL Verification Team
I got this error when killing prepared statements.

081114 13:39:25 [Warning] Forcing shutdown of 1 plugins
081114 13:39:25 [Note] Plugin 'MEMORY' will be forced to shutdown
081114 13:39:25 [ERROR] Plugin 'MEMORY' has ref_count=4 after shutdown.
081114 13:39:25 [Note] mysqld-debug: Shutdown complete