View Issue Details

IDProjectCategoryView StatusLast Update
0038358FPCDatabasepublic2021-01-24 20:29
ReporterZdravko Gabrovski Assigned ToMichael Van Canneyt  
PrioritynormalSeverityminorReproducibilityalways
Status resolvedResolutionfixed 
PlatformAllOSAll 
Product Version3.3.1 
Fixed in Version3.3.1 
Summary0038358: Unable to reclose closed cursor in Ibconnection.pp
DescriptionAfter a new fresh upgrade from the trunk I catch a serious (for my applications) problem that comes from the modification of the method
procedure TIBConnection.FreeFldBuffers(cursor : TSQLCursor); in ibconnection.pp.

Old code is:
procedure TIBConnection.FreeFldBuffers(cursor : TSQLCursor);

begin
  with cursor as TIBCursor do
    begin
    FreeSQLDABuffer(SQLDA);
    FreeSQLDABuffer(in_SQLDA);
    SetLength(FieldBinding,0);
    end;
end;

The New code is:

procedure TIBConnection.FreeFldBuffers(cursor : TSQLCursor);

begin
  with cursor as TIBCursor do
    begin
    if FSelectable and (CursorName<>'') then
      begin
      if isc_dsql_free_statement(@Status, @StatementHandle, DSQL_close)<>0 then <<<<<<<<<<<<<<<<< this code
        CheckError('Close Cursor', Status); // Ignore this, it can already be closed.
      end;
    end;
end;

The problem happens when there is a Transaction commit/rollback before close of the query.
It raises "IBConnection1 : Close Cursor : -Attempt to reclose a closed cursor."


Steps To ReproducePlease, find attached example.
Unzip in your favor folder.
Open project, compile it.
Start it.
Click button "Open"
Click button "Close"
It will raise error.

If you start the project with old version of the ibconnection.pp, there is no problem!
TagsNo tags attached.
Fixed in Revision48409
FPCOldBugId
FPCTarget3.2.2
Attached Files

Activities

Zdravko Gabrovski

2021-01-14 22:21

reporter  

RecloseCursorBug.7z (2,911 bytes)

Zdravko Gabrovski

2021-01-14 22:24

reporter   ~0128327

I forgot to mention that the test project needs local firebird on 127.0.0.1 and EMPLOYEE database.
and TSQLQuery has "KeepOpenOnCommit" flag.

Anton Kavalenka

2021-01-15 11:59

reporter   ~0128338

Have a look to that IB cursor mess in
0038124

Michael Van Canneyt

2021-01-15 12:09

administrator   ~0128339

Last edited: 2021-01-15 12:16

View 2 revisions

@Anton, The two issues are unrelated.
The problem is that KeepOpenOnCommit causes the transaction to be closed while the dataset remains open.
Firebird then internally closes statement handles connected to the transaction.
Later when the dataset closes it attempts to close an already closed statement.

Zdravko Gabrovski

2021-01-15 14:56

reporter   ~0128344

@Michael, you are 100% right. That's the case.
From yesterday I think to ignore that kind of error in the case of isc_dsql_free_statement.
Unfortunately there is no function in the firebird library that checks if the handle is already closed.
Do you want to do some patch in that direction?

Zdravko Gabrovski

2021-01-16 09:02

reporter   ~0128368

PLease, find attached proposal for the patch.
After I patched in this way all my applications working well.
I add a new boolean flag in CheckError method and if it is true I ignore check for that specific IB Error code.
ibconn.diff (1,781 bytes)   
Index: packages/fcl-db/src/sqldb/interbase/ibconnection.pp
===================================================================
--- packages/fcl-db/src/sqldb/interbase/ibconnection.pp	(revision 48161)
+++ packages/fcl-db/src/sqldb/interbase/ibconnection.pp	(working copy)
@@ -92,7 +92,7 @@
     procedure GetFloat(CurrBuff, Buffer : pointer; Size : Byte);
     procedure SetFloat(CurrBuff: pointer; Dbl: Double; Size: integer);
 
-    procedure CheckError(ProcName : string; Status : PISC_STATUS);
+    procedure CheckError(ProcName : string; Status : PISC_STATUS;IgnrReclose:Boolean=False);
     procedure SetParameters(cursor : TSQLCursor; aTransation : TSQLTransaction; AParams : TParams);
     procedure FreeSQLDABuffer(var aSQLDA : PXSQLDA);
     function  IsDialectStored: boolean;
@@ -169,7 +169,7 @@
   SQL_NULL = 32767;
   INVALID_DATA = -1;
 
-procedure TIBConnection.CheckError(ProcName : string; Status : PISC_STATUS);
+procedure TIBConnection.CheckError(ProcName : string; Status : PISC_STATUS;IgnrReclose:Boolean=False);
 var
   i,ErrorCode : longint;
   Msg, SQLState : string;
@@ -181,6 +181,8 @@
   if ((Status[0] = 1) and (Status[1] <> 0)) then
     begin
     ErrorCode := Status[1];
+    if IgnrReclose and (ErrorCode = 335544577) then  // Ignore Attempt to reclose a closed cursor
+      Exit;
 {$IFDEF LinkDynamically}
     if assigned(fb_sqlstate) then // >= Firebird 2.5
     begin
@@ -967,7 +969,7 @@
     if FSelectable and (CursorName<>'') then
       begin
       if isc_dsql_free_statement(@Status, @StatementHandle, DSQL_close)<>0 then
-        CheckError('Close Cursor', Status); // Ignore this, it can already be closed.
+        CheckError('Close Cursor', Status, True); // Ignore this, it can already be closed.
       end;
     end;
 end;
ibconn.diff (1,781 bytes)   

Michael Van Canneyt

2021-01-24 19:38

administrator   ~0128570

Fixed slightly differently. Thanks for reporting and the patch !

Zdravko Gabrovski

2021-01-24 20:29

reporter   ~0128571

The most important - you fix!
I will test and close the issue.

Issue History

Date Modified Username Field Change
2021-01-14 22:21 Zdravko Gabrovski New Issue
2021-01-14 22:21 Zdravko Gabrovski File Added: RecloseCursorBug.7z
2021-01-14 22:24 Zdravko Gabrovski Note Added: 0128327
2021-01-15 08:30 Michael Van Canneyt Assigned To => Michael Van Canneyt
2021-01-15 08:30 Michael Van Canneyt Status new => assigned
2021-01-15 11:59 Anton Kavalenka Note Added: 0128338
2021-01-15 12:09 Michael Van Canneyt Note Added: 0128339
2021-01-15 12:16 Michael Van Canneyt Note Edited: 0128339 View Revisions
2021-01-15 14:56 Zdravko Gabrovski Note Added: 0128344
2021-01-16 09:02 Zdravko Gabrovski Note Added: 0128368
2021-01-16 09:02 Zdravko Gabrovski File Added: ibconn.diff
2021-01-24 19:38 Michael Van Canneyt Status assigned => resolved
2021-01-24 19:38 Michael Van Canneyt Resolution open => fixed
2021-01-24 19:38 Michael Van Canneyt Fixed in Version => 3.3.1
2021-01-24 19:38 Michael Van Canneyt Fixed in Revision => 48409
2021-01-24 19:38 Michael Van Canneyt FPCTarget => 3.2.2
2021-01-24 19:38 Michael Van Canneyt Note Added: 0128570
2021-01-24 20:29 Zdravko Gabrovski Note Added: 0128571