Firebird connect / diconnect more times error connection shutdown lazarus / 16-Databases ---------------------------------------------------------------------- uganof · 2023-03-06 10:22 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Procedure ReleaseIBase60; begin  if RefCount>1 then    Dec(RefCount)  else    begin    // Shutdown embedded subsystem with timeout 300ms (Firebird 2.5+)    // Required before unloading library; has no effect on non- embedded client    if (pointer(fb_shutdown)<>nil) and (fb_shutdown(300,1)<>0) then      begin      //todo: log error; still try to unload library below as the timeout may have been insufficient      end;    if UnloadLibrary(LibHandle) then      begin      Dec(RefCount);      LibHandle := NilHandle;      LoadedLibrary:='';    end;    end;end;  calling fb_shutdown creates problems for non-embedded databases Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Procedure ReleaseIBase60; begin  if RefCount>1 then    Dec(RefCount)  else    begin    If UseEmbeddedFirebird then      begin      // Shutdown embedded subsystem with timeout 300ms (Firebird 2.5+)      // Required before unloading library; has no effect on non-embedded client      if (pointer(fb_shutdown)<>nil) and (fb_shutdown(300,1)<>0) then        begin        //todo: log error; still try to unload library below as the timeout may have been insufficient        end;      end;    if UnloadLibrary(LibHandle) then      begin      Dec(RefCount);      LibHandle := NilHandle;      LoadedLibrary:='';      end;    end;end; if this is a correct consideration, how do you communicate the patch to the free pascal development team? ---------------------------------------------------------------------- paweld · 2023-03-06 11:35 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 ---------------------------------------------------------------------- uganof · 2023-03-06 15:04 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 · 2023-03-06 16:52 Quote from: uganof on March 06, 2023, 11:22:43 amif 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 · 2023-03-06 17:04 Thank you @rvk, QuoteThe 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 Quotefb_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 · 2023-03-06 17:29 Quote from: uganof on March 06, 2023, 06:04:56 pmQuoteCurrently 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? ---------------------------------------------------------------------- uganof · 2023-03-07 09:13 QuoteThe 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 QuoteEdit: 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 · 2023-03-07 09:51 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 · 2023-03-07 10:03 QuoteDid 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 · 2023-03-07 10:12 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 · 2023-03-07 10:20 QuoteYou 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