Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segmentation fault in FreePascal multi-threaded program when using 2.5.x client library on Linux [CORE3646] #3997

Closed
firebird-automations opened this issue Oct 28, 2011 · 9 comments

Comments

@firebird-automations
Copy link
Collaborator

Submitted by: Pumuqui (pumuqui)

Segmentation fault for a FreePascal program,
that connects to a FB 2.5 database inside a thread.
The segmentation fault just occurs, when there is a
Firebird exception caught inside the thread.

The example code below should illustrate the situation.

There are no problems on Windows, nor on Linux when using
a 2.1 client library, the fault just happens for Linux and the 2.5 client.

I posted the error first on the fpc-devel list, where they told me that
this should be an error in the Firebird client dll:
http://www.mail-archive.com/fpc-devel@lists.freepascal.org/msg24051.html

Change the connection string in the following code, so that it points
to an existing database, then put the code into file fbthreadtest.lpr and compile with
<code>
fpc -gl -dUseCThreads -ofbthreadtest fbthreadtest.lpr
<code>

Run with
<code>
foo@PSERVER:~/Development/src$./fbthreadtest
select...
...exception
Violación de segmento
</code>

This is the example program:
<code>
program fbthreadtest;
{$IFDEF FPC}
{$MODE DELPHI}{$H+}
{$ENDIF}

{$APPTYPE CONSOLE}
{$LINKLIB pthread}

uses
{$IFDEF UNIX}
cthreads,
cwstring,
{$ENDIF}
Classes,
IBConnection, sqldb;

type
TFBThread = class(TThread)
private
protected
procedure Execute; override;
public
constructor Create;
end;

constructor TFBThread.Create;
begin
inherited Create(True);
FreeOnTerminate := False;
Resume;
end;

procedure TFBThread.Execute;
var
db: TIBConnection;
tr: TSQLTransaction;
q: TSQLQuery;
begin
db := TIBConnection.Create(nil);
try
db.DatabaseName := 'localhost:/home/data/Database/ssstst.gdb';
db.LoginPrompt := False;
db.HostName := '';
db.UserName := 'SYSDBA';
db.Password := 'masterkey';
db.Connected := TRUE;
tr := TSQLTransaction.Create(nil);
try
tr.DataBase := db;
tr.Action := caCommit;
tr.Params.Clear;
tr.Params.Add('isc_tpb_read_committed');
tr.Params.Add('isc_tpb_rec_version');
tr.Params.Add('isc_tpb_nowait');
db.Transaction := tr;
tr.Active := True;
q := TSQLQuery.Create(nil);
try
q.Database := db;
q.Transaction := tr;

    try
      Writeln\('select\.\.\.'\);
      // the following line raises an exception because rdb$databases

doesn't exist.
q.SQL.Text := 'select count(*) from rdb$databases';
q.Prepare;
except
Writeln('...exception');
end;
finally
q.Free;
end;
finally
tr.Commit;
tr.Free;
end;
finally
db.Connected := False;
db.Free;
end;
end;

begin
with TFBThread.Create do begin
WaitFor;
Free;
end;
end.
</code>

Commits: e3f9db4 7d0d410 84eedc8

@firebird-automations
Copy link
Collaborator Author

Modified by: @AlexPeshkoff

assignee: Alexander Peshkov [ alexpeshkoff ]

@firebird-automations
Copy link
Collaborator Author

Commented by: @AlexPeshkoff

Thread's destructor function does not behave exactly according to POSIX standard on linux. After destroying associated key function is anyway attempted to be called when thread terminates, which (taken together with aleready unloaded client library) causes segfault. Solution is to use windows-like approach, where we never used thread dtor at all.

@firebird-automations
Copy link
Collaborator Author

Modified by: @AlexPeshkoff

status: Open [ 1 ] => Resolved [ 5 ]

resolution: Fixed [ 1 ]

Fix Version: 2.5.2 [ 10450 ]

@firebird-automations
Copy link
Collaborator Author

Commented by: @AlexPeshkoff

Unfortunately linux (glibc) implementation of pthread_kill() function is unstable and can segfault in case of invalid thread id. Better solution is required here.

@firebird-automations
Copy link
Collaborator Author

Modified by: @AlexPeshkoff

status: Resolved [ 5 ] => Reopened [ 4 ]

resolution: Fixed [ 1 ] =>

Fix Version: 2.5.2 [ 10450 ] =>

@firebird-automations
Copy link
Collaborator Author

Modified by: @AlexPeshkoff

status: Reopened [ 4 ] => Resolved [ 5 ]

resolution: Fixed [ 1 ]

Fix Version: 3.0 Alpha 1 [ 10331 ]

Fix Version: 2.5.2 [ 10450 ]

@firebird-automations
Copy link
Collaborator Author

Modified by: @pcisar

status: Resolved [ 5 ] => Closed [ 6 ]

@firebird-automations
Copy link
Collaborator Author

Modified by: @pavel-zotov

QA Status: No test

@firebird-automations
Copy link
Collaborator Author

Modified by: @pavel-zotov

status: Closed [ 6 ] => Closed [ 6 ]

QA Status: No test => Cannot be tested

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment