Bug #9814 Cursors: Closing a cursor that is not open hangs MySQL
Submitted: 11 Apr 2005 13:05 Modified: 5 Jul 2005 8:35
Reporter: Disha Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server Severity:S1 (Critical)
Version:5.0.3 Beta OS:Any (ALL)
Assigned to: Sergey Petrunya CPU Architecture:Any

[11 Apr 2005 13:05] Disha
Description:
Closing a cursor that is not open causes MySQL to hang.

Although this happens only after the fetch statement has been executed. That is, if the script reads as follows, then the error message 'Cursor Not Open' is appropriately displayed.

Open Cur1;
Close Cur1;
Close Cur1;

However, in case of the following, the system hangs.

How to repeat:
1. Delimiter //
2. Drop table if exists t1//
    Drop table if exists t1//

3.  create table t1(f1 int)//
     create table t2(f1 int)//
 
4. DROP PROCEDURE IF EXISTS SP001//
   CREATE PROCEDURE SP001()
   P1: BEGIN
	DECLARE ENDTABLE INT DEFAULT 0;
	DECLARE TEMP_NUM INT;
	DECLARE TEMP_SUM INT;
	DECLARE C1 CURSOR FOR SELECT F1 FROM T1;
	DECLARE C2 CURSOR FOR SELECT F1 FROM T2;
	DECLARE CONTINUE HANDLER FOR NOT FOUND SET ENDTABLE = 1;

	SET ENDTABLE=0;
	SET TEMP_SUM=0;
	SET TEMP_NUM=0;
	
	OPEN C1;

	FETCH C1 INTO TEMP_NUM;
	WHILE ENDTABLE = 0 DO
		SET TEMP_SUM=TEMP_NUM+TEMP_SUM;
		FETCH C1 INTO TEMP_NUM;
	END WHILE;
	SELECT TEMP_SUM;
	CLOSE C1;
	CLOSE C1;
END P1//

Expected Result:
ERROR 1326 (24000): Cursor is not open

Actual Result:
1. The client hangs and stops responding.
2. The user has to close the client and start all over again with new client.
[11 Apr 2005 15:16] MySQL Verification Team
I wasn't able to repeat the behavior reported on Linux with BK source
I will test on Windows:

mysql> CREATE PROCEDURE SP001()
    -> P1: BEGIN
    -> DECLARE ENDTABLE INT DEFAULT 0;
    -> DECLARE TEMP_NUM INT;
    -> DECLARE TEMP_SUM INT;
    -> DECLARE C1 CURSOR FOR SELECT F1 FROM T1;
    -> DECLARE C2 CURSOR FOR SELECT F1 FROM T2;
    -> DECLARE CONTINUE HANDLER FOR NOT FOUND SET ENDTABLE = 1;
    -> SET ENDTABLE=0;
    -> SET TEMP_SUM=0;
    -> SET TEMP_NUM=0;
    -> OPEN C1;
    -> FETCH C1 INTO TEMP_NUM;
    -> WHILE ENDTABLE = 0 DO
    -> SET TEMP_SUM=TEMP_NUM+TEMP_SUM;
    -> FETCH C1 INTO TEMP_NUM;
    -> END WHILE;
    -> SELECT TEMP_SUM;
    -> CLOSE C1;
    -> CLOSE C1;
    -> END P1//
Query OK, 0 rows affected (0.01 sec)

mysql> select version()//
+------------------+
| version()        |
+------------------+
| 5.0.5-beta-debug |
+------------------+
1 row in set (0.00 sec)
[11 Apr 2005 15:38] Disha
Hi,

We also need to call the sp001 for the server to hang.
Could you please reopen the bug.

Added repro step is as follows:

Call sp001()//

thanks, 
ashwin
[11 Apr 2005 16:10] Disha
Updated OS.
[12 Apr 2005 9:53] Per-Erik Martin
This is probably a duplicate of BUG#9771
[4 Jul 2005 18:57] 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/internals/26651
[4 Jul 2005 19:37] 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/internals/26652
[4 Jul 2005 20:15] Sergey Petrunya
Pushed into 5.0.9 tree
[5 Jul 2005 8:35] Jon Stephens
Thank you for your bug report. This issue has been committed to our
source repository of that product and will be incorporated into the
next release.

If necessary, you can access the source repository and build the latest
available version, including the bugfix, yourself. More information 
about accessing the source trees is available at
    http://www.mysql.com/doc/en/Installing_source_tree.html

Additional info:

Documented fix in 5.0.9 changelog; closed.