Recent

Author Topic: Firebird connect / diconnect more times error connection shutdown  (Read 1196 times)

uganof

  • New Member
  • *
  • Posts: 10
Hello everyone,
this is an old problem:

using the sqldb components to connect to the Firebird (TSQLConnector) if you connect the first time it's fine,
if you disconnect and then retry a connection you get the following error:

connection shutdown

after several searches the problem, in my opinion, is in the following piece of code in file ibase60.inc:

Code: Pascal  [Select][+][-]
  1. Procedure ReleaseIBase60;
  2.  
  3. begin
  4.   if RefCount>1 then
  5.     Dec(RefCount)
  6.   else
  7.     begin
  8.     // Shutdown embedded subsystem with timeout 300ms (Firebird 2.5+)
  9.     // Required before unloading library; has no effect on non-embedded client
  10.     if (pointer(fb_shutdown)<>nil) and (fb_shutdown(300,1)<>0) then
  11.       begin
  12.       //todo: log error; still try to unload library below as the timeout may have been insufficient
  13.       end;
  14.     if UnloadLibrary(LibHandle) then
  15.       begin
  16.       Dec(RefCount);
  17.       LibHandle := NilHandle;
  18.       LoadedLibrary:='';
  19.       end;
  20.     end;
  21. end;
  22.  

calling fb_shutdown creates problems for non-embedded databases

Code: Pascal  [Select][+][-]
  1. Procedure ReleaseIBase60;
  2.  
  3. begin
  4.   if RefCount>1 then
  5.     Dec(RefCount)
  6.   else
  7.     begin
  8.     If UseEmbeddedFirebird then
  9.       begin
  10.       // Shutdown embedded subsystem with timeout 300ms (Firebird 2.5+)
  11.       // Required before unloading library; has no effect on non-embedded client
  12.       if (pointer(fb_shutdown)<>nil) and (fb_shutdown(300,1)<>0) then
  13.         begin
  14.         //todo: log error; still try to unload library below as the timeout may have been insufficient
  15.         end;
  16.       end;
  17.     if UnloadLibrary(LibHandle) then
  18.       begin
  19.       Dec(RefCount);
  20.       LibHandle := NilHandle;
  21.       LoadedLibrary:='';
  22.       end;
  23.     end;
  24. end;  

if this is a correct consideration, how do you communicate the patch to the free pascal development team?

paweld

  • Hero Member
  • *****
  • Posts: 1214
Re: Firebird connect / diconnect more times error connection shutdown
« Reply #1 on: March 06, 2023, 12:35:19 pm »
To work with Firebird try IBX components - you can install in OPM (Lazarus menu > Packages > Online package manager) or directly from the develper website: https://mwasoftware.co.uk/ibx
Best regards / Pozdrawiam
paweld

uganof

  • New Member
  • *
  • Posts: 10
Re: Firebird connect / diconnect more times error connection shutdown
« Reply #2 on: March 06, 2023, 04:04:29 pm »
Thank you for the suggestion.
I already know the package, useful and convenient, but I need to use sqldb because it's more flexible for my purposes.

rvk

  • Hero Member
  • *****
  • Posts: 6361
Re: Firebird connect / diconnect more times error connection shutdown
« Reply #3 on: March 06, 2023, 05:52:31 pm »
if this is a correct consideration, how do you communicate the patch to the free pascal development team?
You can use the bugtracker (on the left here) to report this.

But why do you consider this a bug?
Why would you only call fb_shutdown for embedded version and not for the normal connection?

The fact you have an error and this "fix" works, doesn't mean it's the correct way.
There could be a bug in your program.

uganof

  • New Member
  • *
  • Posts: 10
Re: Firebird connect / diconnect more times error connection shutdown
« Reply #4 on: March 06, 2023, 06:04:56 pm »
Thank you @rvk,

Quote
The fact you have an error and this "fix" works, doesn't mean it's the correct way.
There could be a bug in your program.

I'm not sure but I read the firebird api manual which I reproduce below

Quote
fb_shutdown()
fb_shutdown() performs a smart shutdown of various Firebird subsystems (yValve, engine, redirector). It was primarily designed for use by the internal engine, since it is only applicable to the current process. It is exposed by the API for its possible usefulness to user applications in the embedded server environment.

Currently operational only for the embedded engine, this function terminates all the current activity, rolls back active transactions, disconnects active attachments and shuts down the embedded engine instance gracefully.

Important for Application Developers
fb_shutdown() does not perform a shutdown of a remote server to which your application might be concurrently attached. In fact, all of the Firebird client libraries—including the one in embedded—call it automatically at exit(), as long as the client is attached to at least one database or service.

Hence, it should never be called by a client in the context of a remote attachment.

As I understand it, it shouldn't be called for remote databases, like it was before the patch

rvk

  • Hero Member
  • *****
  • Posts: 6361
Re: Firebird connect / diconnect more times error connection shutdown
« Reply #5 on: March 06, 2023, 06:29:23 pm »
Quote
Currently operational only for the embedded engine,...
Hence, it should never be called by a client in the context of a remote attachment.
The fb_shutdown actually should only have effect on the embedded version.
So calling it in remote situation should not matter.

I think IBX for Lazarus also calls fb_shutdown (although with parameters fb_shutdown(0,-7) of which the -7 is also incorrect, I think it should be positive).

https://firebirdsql.org/rlsnotesh/rnfb25-apiods-api.html#rnfb25-apiods-api-shtdn

But you can open a bugtracker report for it to get fixed.

Edit: Maybe similar: https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues/29034
You don't have the IBX for Lazarus installed?
« Last Edit: March 06, 2023, 06:37:30 pm by rvk »

uganof

  • New Member
  • *
  • Posts: 10
Re: Firebird connect / diconnect more times error connection shutdown
« Reply #6 on: March 07, 2023, 10:13:30 am »
Quote
The fb_shutdown actually should only have effect on the embedded version.
So calling it in remote situation should not matter.

It actually blocks connections even on the remote version, something I've verified on windows, linux, and macos

Quote
Edit: Maybe similar: https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues/29034
You don't have the IBX for Lazarus installed?

YES, I've already seen it and in fact it's the same problem but it is mistakenly thought that it is caused by IBX but it is not so.
But also IBX, as you say, uses fb_shutdown improperly.

My tests are always without IBX

rvk

  • Hero Member
  • *****
  • Posts: 6361
Re: Firebird connect / diconnect more times error connection shutdown
« Reply #7 on: March 07, 2023, 10:51:39 am »
Did you set the DEFINE LinkDynamically manually?
Because this could only be an issue if it's compiled with DEFINE LinkDynamically.

That's also probably the reason this isn't a widespread problem because the default installation doesn't use ReleaseIBase60 because it isn't compiled with LinkDynamically.

uganof

  • New Member
  • *
  • Posts: 10
Re: Firebird connect / diconnect more times error connection shutdown
« Reply #8 on: March 07, 2023, 11:03:59 am »
Quote
Did you set the DEFINE LinkDynamically manually?

No, but I noticed that the behavior is different between Windows and Macos, in the latter it uses dynamic link and fb_shutdown does damage, under Windows that uses the static link at runtime it doesn't cause problems because it doesn't go through fb_shutdown

rvk

  • Hero Member
  • *****
  • Posts: 6361
Re: Firebird connect / diconnect more times error connection shutdown
« Reply #9 on: March 07, 2023, 11:12:35 am »
Ah, Ok. I don't use Macos.

You did add Windows to the bugreport, but you are not sure it is really a problem on that platform (?)
It uses a different .dll/.so so it might not even be a problem on Windows (even when compiled with LinkDynamically).

(You might want to clarify that in your bugreport)

https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues/40151

uganof

  • New Member
  • *
  • Posts: 10
Re: Firebird connect / diconnect more times error connection shutdown
« Reply #10 on: March 07, 2023, 11:20:11 am »
Quote
You did add Windows to the bugreport, but you are not sure it is really a problem on that platform (?)

the problem is also on Windows you can see it simply by placing a TSQLConnector component on a form and connecting it to a Firebird database, without compiling
On Macos this happens at runtime, but not on the form without compiling. In any case by changing the code as I indicated the problems disappeared from both platforms, on linux I haven't tried it yet but I guess it's the same

I believe, always with the conditional, that the damage is always caused by an improper use of fb_shutdown

 

TinyPortal © 2005-2018