X-Original-To: pgsql-interfaces-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id 5476AD1C9F7 for ; Mon, 4 Aug 2003 15:29:30 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 22575-06 for ; Mon, 4 Aug 2003 12:28:13 -0300 (ADT) Received: from gatefox.foxi.nl (a194-109-246-192.adsl.xs4all.nl [194.109.246.192]) by svr1.postgresql.org (Postfix) with ESMTP id 3BA3FD1C92F for ; Mon, 4 Aug 2003 12:29:18 -0300 (ADT) Received: from techfox.foxi (techfox.foxi [192.168.0.79]) by gatefox.foxi.nl (8.10.2/8.10.2) with ESMTP id h74FTDL09682 for ; Mon, 4 Aug 2003 17:29:14 +0200 From: Frank van Vugt Organization: Foxi Consultants in Technology BV Subject: Multi-threaded user app segfaults when using libpq with separate connections Date: Mon, 4 Aug 2003 17:29:13 +0200 User-Agent: KMail/1.5.1 To: pgsql-interfaces@postgresql.org MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Message-Id: <200308041729.13212.ftm.van.vugt@foxi.nl> X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200308/8 X-Sequence-Number: 4362 Your name : Frank van Vugt Your email address : ftm.van.vugt@foxi.nl System Configuration --------------------- Architecture : Intel Pentium III / Amd Athlon XP+ Operating System : Linux 2.4.20 with glibc 2.3.2 (SuSE v8.2) PostgreSQL version : PostgreSQL-7.3.4 Compiler used : gcc v3.3 Please enter a FULL description of your problem: ------------------------------------------------ A small multi-threaded demo/poc-program using Qt (including its SQL-module= =20 which connects to PostgreSQL using libpq) is segfaulting while multiple=20 connections are used. The code creates 10 new threads, each of which basically: - locks a mutex - adds/opens a new database connection - unlocks the mutex - using the newly created/opened connection - starts a transaction - sets transaction isolation level to SERIALIZABLE - performs some select-only queries - commits the transaction - closes the database connection Out of 10 consecutive runs, the code completes three times successfully and= =20 segfaults the other seven times. I have recompiled libpq with debugging and= =20 without optimization, the backtraces of the coredumps are included below. Important : if the unlocking of the mutex is moved to AFTER the database=20 close, then the code seems to run without any problem each and every time Please describe a way to repeat the problem. Please try to provide a concise reproducible example, if at all possible: ---------------------------------------------------------------------- I'm not sure how to proceed from here. Obviously I'm using Qt for handling= =20 PostgreSQL connections as well as multi-threading, so rewriting my 'demo-code' in plain C++ using libpthread and libpq would probably take too= =20 much time. Maybe someone is willing to have a go at the Qt-code (just let m= e=20 know and I'll mail the source), or maybe (hopefully) the information provid= ed=20 here will prove sufficient a clue to pinpoint the problem. The only thing I found in the mailinglist archives which *may* have some=20 bearing to this problem is: http://archives.postgresql.org/pgsql-interfaces/2002-09/msg00042.php I'm not sure it's relevant, but please be advised that the PostgreSQL drive= r=20 in Qt has the following code included in order to allow compilation: // PostgreSQL header included by redefines DEBU= G. #if defined(DEBUG) # undef DEBUG #endif #include #include // PostgreSQL header redefines errno erroneously. #if defined(errno) # undef errno #endif #define errno qt_psql_errno #include #undef errno Furthermore, I found that the 7.4devdocs talk about the --enable-thread-saf= ety=20 option for ./configure, but I don't think that applies to my platform? =3D> backtraces of coredumps included below Best, Frank. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D The results of ten sequentially executed runs: -+-+-+- #0 0x40cc1b2b in realloc () from /lib/libc.so.6 (gdb) where #0 0x40cc1b2b in realloc () from /lib/libc.so.6 #1 0x40d8c7ac in pqReadData (conn=3D0x80c0fc0) at fe-misc.c:483 #2 0x40d8a335 in PQgetResult (conn=3D0x80c0fc0) at fe-exec.c:1337 #3 0x40d8a511 in PQexec (conn=3D0x80c0fc0, query=3D0x80cb638 "set transact= ion=20 isolation level SERIALIZABLE") at fe-exec.c:1441 #4 0x4063aaa7 in QPSQLResult::reset(QString const&) (this=3D0x80cb418,=20 query=3D@0xbf7ffa9c) at sql/drivers/psql/qsql_psql.cpp:473 #5 0x4060dd34 in QSqlQuery::exec(QString const&) (this=3D0xbf7ffa8c,=20 query=3D@0xbf7ffa9c) at sql/qsqlquery.cpp:358 #6 0x08050401 in PrintEngine::run() (this=3D0x80a72e0) at printengine.cpp:= 85 #7 0x402b4be6 in QThreadInstance::start(void*) (_arg=3D0x80a7374) at=20 kernel/qthread_unix.cpp:120 #8 0x40b1dc60 in pthread_start_thread () from /lib/libpthread.so.0 (gdb) -+-+-+- #0 0x4061390d in QSqlDatabase::transaction() (this=3D0x80cd2e8) at=20 sql/qsqldatabase.cpp:760 760 if ( !d->driver->hasFeature( QSqlDriver::Transactions ) ) (gdb) where #0 0x4061390d in QSqlDatabase::transaction() (this=3D0x80cd2e8) at=20 sql/qsqldatabase.cpp:760 #1 0x080503cb in PrintEngine::run() (this=3D0x80c0b98) at printengine.cpp:= 83 #2 0x402b4be6 in QThreadInstance::start(void*) (_arg=3D0x80c0bdc) at=20 kernel/qthread_unix.cpp:120 #3 0x40b1dc60 in pthread_start_thread () from /lib/libpthread.so.0 (gdb) -+-+-+- Third run without problems. -+-+-+- Fourth run without problems. -+-+-+- #0 0x40caf675 in fflush () from /lib/libc.so.6 (gdb) where #0 0x40caf675 in fflush () from /lib/libc.so.6 #1 0x40d8cc16 in pqSendSome (conn=3D0x80c10f8) at fe-misc.c:746 #2 0x40d8cc46 in pqFlush (conn=3D0x80c10f8) at fe-misc.c:764 #3 0x40d896e6 in PQsendQuery (conn=3D0x80c10f8, query=3D0x80cb3d0 "set=20 transaction isolation level SERIALIZABLE") at fe-exec.c:799 #4 0x40d8a4f3 in PQexec (conn=3D0x80c10f8, query=3D0x80cb3d0 "set transact= ion=20 isolation level SERIALIZABLE") at fe-exec.c:1429 #5 0x4063aaa7 in QPSQLResult::reset(QString const&) (this=3D0x80cb0f8,=20 query=3D@0xbf7ffa9c) at sql/drivers/psql/qsql_psql.cpp:473 #6 0x4060dd34 in QSqlQuery::exec(QString const&) (this=3D0xbf7ffa8c,=20 query=3D@0xbf7ffa9c) at sql/qsqlquery.cpp:358 #7 0x08050401 in PrintEngine::run() (this=3D0x80a72d0) at printengine.cpp:= 85 #8 0x402b4be6 in QThreadInstance::start(void*) (_arg=3D0x80a7364) at=20 kernel/qthread_unix.cpp:120 #9 0x40b1dc60 in pthread_start_thread () from /lib/libpthread.so.0 (gdb) -+-+-+- #0 0x4061391e in QSqlDatabase::transaction() (this=3D0x80cbcb0) at=20 sql/qsqldatabase.cpp:760 760 if ( !d->driver->hasFeature( QSqlDriver::Transactions ) ) (gdb) where #0 0x4061391e in QSqlDatabase::transaction() (this=3D0x80cbcb0) at=20 sql/qsqldatabase.cpp:760 #1 0x080503cb in PrintEngine::run() (this=3D0x80bd358) at printengine.cpp:= 83 #2 0x402b4be6 in QThreadInstance::start(void*) (_arg=3D0x80c0a74) at=20 kernel/qthread_unix.cpp:120 #3 0x40b1dc60 in pthread_start_thread () from /lib/libpthread.so.0 (gdb) -+-+-+- #0 0x403352a1 in QObject::connect(QObject const*, char const*, QObject=20 const*, char const*) (sender=3D0x80c0bf8, signal=3D0x80c0b58 "2destroyed()"= ,=20 receiver=3D0x80c7660, member=3D0x80c0c68 "1slotResultDestroyed()") at kernel/qobject.cpp:1737 1737 QMetaObject *smeta =3D sender->metaObject(); (gdb) where #0 0x403352a1 in QObject::connect(QObject const*, char const*, QObject=20 const*, char const*) (sender=3D0x80c0bf8, signal=3D0x80c0b58 "2destroyed()"= ,=20 receiver=3D0x80c7660, member=3D0x80c0c68 "1slotResultDestroyed()") at kernel/qobject.cpp:1737 #1 0x4060d07d in QSqlResultShared (this=3D0x80c7660, result=3D0x80cb080) a= t=20 sql/qsqlquery.cpp:57 #2 0x4060d42f in QSqlQuery (this=3D0xbf7ff94c, r=3D0x80cb080) at=20 sql/qsqlquery.cpp:213 #3 0x4063c03b in QPSQLDriver::createQuery() const (this=3D0x80c0bf8) at=20 sql/drivers/psql/qsql_psql.cpp:687 #4 0x4060d874 in QSqlQuery::init(QString const&, QSqlDatabase*)=20 (this=3D0xbf7ffa8c, query=3D@0x40a26a88, db=3D0x80c0ae8) at sql/qsqlquery.c= pp:272 #5 0x4060d762 in QSqlQuery (this=3D0xbf7ffa8c, db=3D0x80c0ae8) at=20 sql/qsqlquery.cpp:259 #6 0x080503dd in PrintEngine::run() (this=3D0x80a72e0) at printengine.cpp:= 84 #7 0x402b4be6 in QThreadInstance::start(void*) (_arg=3D0x80a7374) at=20 kernel/qthread_unix.cpp:120 #8 0x40b1dc60 in pthread_start_thread () from /lib/libpthread.so.0 (gdb) -+-+-+- #0 0x4061396d in QSqlDatabase::commit() (this=3D0x80c0ae8) at=20 sql/qsqldatabase.cpp:775 775 if ( !d->driver->hasFeature( QSqlDriver::Transactions ) ) (gdb) where #0 0x4061396d in QSqlDatabase::commit() (this=3D0x80c0ae8) at=20 sql/qsqldatabase.cpp:775 #1 0x08050b9d in PrintEngine::run() (this=3D0x80a72e0) at printengine.cpp:= 118 #2 0x402b4be6 in QThreadInstance::start(void*) (_arg=3D0x80a7374) at=20 kernel/qthread_unix.cpp:120 #3 0x40b1dc60 in pthread_start_thread () from /lib/libpthread.so.0 (gdb) -+-+-+- Nineth run without problems -+-+-+- #0 0x40cc6a01 in strlen () from /lib/libc.so.6 (gdb) where #0 0x40cc6a01 in strlen () from /lib/libc.so.6 #1 0x4071bf33 in internalLatin1ToUnicode (str=3D0xbf7ff89c "\b\021\f\b=20 .=A1@=C4=F8\177=BF=EA=C0n@ .=A1@=FC=F8\177=BFD=F9\177=BF9=ACc@=FC=F8\177=BF= ,=F9\177=BF\002", len=3D0x83ffbbd8,=20 maxlen=3D3212834996) at tools/qstring.cpp:1164 #2 0x4071cb0b in QString (this=3D0xbf7ff89c, str=3D0x83ffbbd8
) at tools/qstring.cpp:1473 #3 0x40638d4f in qMakeError (err=3D@0xbf7ff92c, type=3D2, p=3D0x80ca2d0) a= t=20 sql/drivers/psql/qsql_psql.cpp:118 #4 0x4063ac39 in QPSQLResult::reset(QString const&) (this=3D0x80ca5d8,=20 query=3D@0xbf7ffa9c) at sql/drivers/psql/qsql_psql.cpp:489 #5 0x4060dd34 in QSqlQuery::exec(QString const&) (this=3D0xbf7ffa8c,=20 query=3D@0xbf7ffa9c) at sql/qsqlquery.cpp:358 #6 0x08050401 in PrintEngine::run() (this=3D0x80a72e0) at printengine.cpp:= 85 #7 0x402b4be6 in QThreadInstance::start(void*) (_arg=3D0x80a7374) at=20 kernel/qthread_unix.cpp:120 #8 0x40b1dc60 in pthread_start_thread () from /lib/libpthread.so.0 (gdb) -+-+-+-