pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
ECPG still having thread problems on Linux
3+ messages / 2 participants
[nested] [flat]

* ECPG still having thread problems on Linux
@ 2003-06-24 08:25  Philip Yarra <philip@utiba.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Philip Yarra @ 2003-06-24 08:25 UTC (permalink / raw)
  To: pgsql-interfaces

Hi all, it looks like Lee's ECPG (and libpq) thread-safety patches
have been applied, and configure --with-threads is also added. I
have been doing some testing.

On FreeBSD 4.8, the attached sample app runs without a problem.

However, I still encounter a threading problem on Linux (RedHat 7.3).

I have done the following:
1) cvs update
2) ./configure --with-threads && make && su -c "make install"
3) compiled cn.pgc as follows:
        a) ecpg -t cn.pgc
        b) gcc -I/usr/local/pgsql/include -L/usr/local/pgsql/lib \
                -lecpg -lpgtypes -pthread cn.c
4) ./a.out - one thread runs to completion (inserts 5 records),
        the other hangs (manages one insert, then blocks forever)

Using gdb, I attached to the thread that has locked up, and the backtrace
looks like this:

(gdb) backtrace
#0  0x420e0187 in poll () from /lib/i686/libc.so.6
#1  0x4007d8cc in pqSocketPoll () from /usr/local/pgsql/lib/libpq.so.3
#2  0x4007d7ed in pqSocketCheck () from /usr/local/pgsql/lib/libpq.so.3
#3  0x4007d71f in pqWaitTimed () from /usr/local/pgsql/lib/libpq.so.3
#4  0x4007d6f5 in pqWait () from /usr/local/pgsql/lib/libpq.so.3
#5  0x4007bb53 in PQgetResult () from /usr/local/pgsql/lib/libpq.so.3
#6  0x4007bcbb in PQexec () from /usr/local/pgsql/lib/libpq.so.3
#7  0x40026d81 in ECPGexecute () from /usr/local/pgsql/lib/libecpg.so.4
#8  0x4002724c in ECPGdo () from /usr/local/pgsql/lib/libecpg.so.4
#9  0x08048927 in ins2 ()
#10 0x40043faf in pthread_start_thread () from /lib/i686/libpthread.so.0

Can anyone shed some light on why the behaviour differs between these two 
platforms? 

Also, perhaps someone other there with access to a different Linux setup 
(maybe a more recent build than RedHat 7.3, or a different distro) could try 
this app themselves to help verify if this is something that's stuffed on 
that release. I think I can rule out this problem being a quirk of my 
particular setup, as 3 different machines (all running RH7.3) give identical 
results.

Build env:
Linux 2.4.18-3
gcc version 2.96 20000731 (Red Hat Linux 7.3 2.96-113)

Regards, Philip Yarra.

Attachments:

  [text/x-csrc] cn.pgc (1.4K, ../../200306241825.32527.philip@utiba.com/2-cn.pgc)
  download | inline:
/* on linux, you can compile with:
ecpg cn.pgc&& gcc -I/usr/local/pgsql/include -L/usr/local/pgsql/lib -lpgtypes -lecpg -lpthread cn.c

and you'll need this table:

CREATE TABLE foo (
    message character(40)
);

*/


#include <pthread.h>

int main();
void ins1();
void ins2();

int main()
{
	pthread_t thread1, thread2;
	pthread_create(&thread1, NULL, (void *) ins1, NULL);
	pthread_create(&thread2, NULL, (void *) ins2, NULL);
	pthread_join(thread1, NULL);
	pthread_join(thread2, NULL);
	printf("Done!");
	return 0;
}

void ins1()
{
	int i;
	EXEC SQL BEGIN DECLARE SECTION;
	char* cs = "test@host";
	char* bar = "one!";
	EXEC SQL END DECLARE SECTION;
	EXEC SQL WHENEVER sqlerror sqlprint;
	EXEC SQL CONNECT TO :cs AS test1;
	EXEC SQL SET AUTOCOMMIT TO ON;
	for (i = 0; i < 5; i++)
	{
		printf("thread1 inserting\n");
		EXEC SQL INSERT INTO foo VALUES(:bar);
		printf("==>thread1 insert done\n");
	}
	EXEC SQL DISCONNECT test1;
	printf("done!\n");
}


void ins2()
{
	int i;
	EXEC SQL BEGIN DECLARE SECTION;
	char* cs = "test@host";
	char* bar = "two!";
	EXEC SQL END DECLARE SECTION;
	EXEC SQL WHENEVER sqlerror sqlprint;
	EXEC SQL CONNECT TO :cs AS test2;
	EXEC SQL SET AUTOCOMMIT TO ON;
	for (i = 0; i < 5; i++)
	{
		printf("thread2 inserting\n");
		EXEC SQL INSERT INTO foo VALUES(:bar);
		printf("==>thread2 insert done\n");
	}
	EXEC SQL DISCONNECT test2;
	printf("done!\n");
}

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* ECPG still having thread problems on Linux
@ 2003-06-25 09:35  Lee Kindness <lkindness@csl.co.uk>
  parent: Philip Yarra <philip@utiba.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Lee Kindness @ 2003-06-25 09:35 UTC (permalink / raw)
  To: Philip Yarra <philip@utiba.com>; +Cc: Lee Kindness <lkindness@csl.co.uk>; pgsql-interfaces

Philip, both your SELECTs are using the same database connection (and
it's undefined which one it is) without any locking. You need to add
"AT clauses" to specify an explicit connection. See attached diff.

However, i've not tried it... I'll try and get some time!

L.

Philip Yarra writes:
 > Hi all, it looks like Lee's ECPG (and libpq) thread-safety patches
 > have been applied, and configure --with-threads is also added. I
 > have been doing some testing.
 > 
 > On FreeBSD 4.8, the attached sample app runs without a problem.
 > 
 > However, I still encounter a threading problem on Linux (RedHat 7.3).
 > 
 > I have done the following:
 > 1) cvs update
 > 2) ./configure --with-threads && make && su -c "make install"
 > 3) compiled cn.pgc as follows:
 >         a) ecpg -t cn.pgc
 >         b) gcc -I/usr/local/pgsql/include -L/usr/local/pgsql/lib \
 >                 -lecpg -lpgtypes -pthread cn.c
 > 4) ./a.out - one thread runs to completion (inserts 5 records),
 >         the other hangs (manages one insert, then blocks forever)
*** cn.pgc	2003-06-25 10:29:55.000000000 +0100
--- cn.pgc.new	2003-06-25 10:29:45.000000000 +0100
***************
*** 36,46 ****
  	EXEC SQL END DECLARE SECTION;
  	EXEC SQL WHENEVER sqlerror sqlprint;
  	EXEC SQL CONNECT TO :cs AS test1;
! 	EXEC SQL SET AUTOCOMMIT TO ON;
  	for (i = 0; i < 5; i++)
  	{
  		printf("thread1 inserting\n");
! 		EXEC SQL INSERT INTO foo VALUES(:bar);
  		printf("==>thread1 insert done\n");
  	}
  	EXEC SQL DISCONNECT test1;
--- 36,46 ----
  	EXEC SQL END DECLARE SECTION;
  	EXEC SQL WHENEVER sqlerror sqlprint;
  	EXEC SQL CONNECT TO :cs AS test1;
! 	EXEC SQL AT test1 SET AUTOCOMMIT TO ON;
  	for (i = 0; i < 5; i++)
  	{
  		printf("thread1 inserting\n");
! 		EXEC SQL AT test1 INSERT INTO foo VALUES(:bar);
  		printf("==>thread1 insert done\n");
  	}
  	EXEC SQL DISCONNECT test1;
***************
*** 57,67 ****
  	EXEC SQL END DECLARE SECTION;
  	EXEC SQL WHENEVER sqlerror sqlprint;
  	EXEC SQL CONNECT TO :cs AS test2;
! 	EXEC SQL SET AUTOCOMMIT TO ON;
  	for (i = 0; i < 5; i++)
  	{
  		printf("thread2 inserting\n");
! 		EXEC SQL INSERT INTO foo VALUES(:bar);
  		printf("==>thread2 insert done\n");
  	}
  	EXEC SQL DISCONNECT test2;
--- 57,67 ----
  	EXEC SQL END DECLARE SECTION;
  	EXEC SQL WHENEVER sqlerror sqlprint;
  	EXEC SQL CONNECT TO :cs AS test2;
! 	EXEC SQL AT test2 SET AUTOCOMMIT TO ON;
  	for (i = 0; i < 5; i++)
  	{
  		printf("thread2 inserting\n");
! 		EXEC SQL AT test2 INSERT INTO foo VALUES(:bar);
  		printf("==>thread2 insert done\n");
  	}
  	EXEC SQL DISCONNECT test2;

Attachments:

  [text/plain] cn.pgc.diff (1.5K, ../../16121.27884.841077.431930@kelvin.csl.co.uk/2-cn.pgc.diff)
  download | inline diff:
*** cn.pgc	2003-06-25 10:29:55.000000000 +0100
--- cn.pgc.new	2003-06-25 10:29:45.000000000 +0100
***************
*** 36,46 ****
  	EXEC SQL END DECLARE SECTION;
  	EXEC SQL WHENEVER sqlerror sqlprint;
  	EXEC SQL CONNECT TO :cs AS test1;
! 	EXEC SQL SET AUTOCOMMIT TO ON;
  	for (i = 0; i < 5; i++)
  	{
  		printf("thread1 inserting\n");
! 		EXEC SQL INSERT INTO foo VALUES(:bar);
  		printf("==>thread1 insert done\n");
  	}
  	EXEC SQL DISCONNECT test1;
--- 36,46 ----
  	EXEC SQL END DECLARE SECTION;
  	EXEC SQL WHENEVER sqlerror sqlprint;
  	EXEC SQL CONNECT TO :cs AS test1;
! 	EXEC SQL AT test1 SET AUTOCOMMIT TO ON;
  	for (i = 0; i < 5; i++)
  	{
  		printf("thread1 inserting\n");
! 		EXEC SQL AT test1 INSERT INTO foo VALUES(:bar);
  		printf("==>thread1 insert done\n");
  	}
  	EXEC SQL DISCONNECT test1;
***************
*** 57,67 ****
  	EXEC SQL END DECLARE SECTION;
  	EXEC SQL WHENEVER sqlerror sqlprint;
  	EXEC SQL CONNECT TO :cs AS test2;
! 	EXEC SQL SET AUTOCOMMIT TO ON;
  	for (i = 0; i < 5; i++)
  	{
  		printf("thread2 inserting\n");
! 		EXEC SQL INSERT INTO foo VALUES(:bar);
  		printf("==>thread2 insert done\n");
  	}
  	EXEC SQL DISCONNECT test2;
--- 57,67 ----
  	EXEC SQL END DECLARE SECTION;
  	EXEC SQL WHENEVER sqlerror sqlprint;
  	EXEC SQL CONNECT TO :cs AS test2;
! 	EXEC SQL AT test2 SET AUTOCOMMIT TO ON;
  	for (i = 0; i < 5; i++)
  	{
  		printf("thread2 inserting\n");
! 		EXEC SQL AT test2 INSERT INTO foo VALUES(:bar);
  		printf("==>thread2 insert done\n");
  	}
  	EXEC SQL DISCONNECT test2;

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: ECPG still having thread problems on Linux
@ 2003-06-26 01:19  Philip Yarra <philip@utiba.com>
  parent: Lee Kindness <lkindness@csl.co.uk>
  0 siblings, 0 replies; 3+ messages in thread

From: Philip Yarra @ 2003-06-26 01:19 UTC (permalink / raw)
  To: Lee Kindness <lkindness@csl.co.uk>; +Cc: pgsql-interfaces

On Wed, 25 Jun 2003 07:35 pm, Lee Kindness wrote:
> Philip, both your SELECTs are using the same database connection (and
> it's undefined which one it is) without any locking. You need to add
> "AT clauses" to specify an explicit connection. See attached diff.

Ah, that'd be it. I spent some time debugging last night, and I'd realised the 
problem lay in the fact that the preproc was outputting NULL as the 
connection name, but was unsure why. Your changes allowed both threads to 
complete their inserts, which is great news for us!

I'll add that "AT" clause to my list of updates for the documentation - it 
might be important. It's kinda.... absent... from the manual. 

I might also add a section on using pthreads with ECPG, since people porting 
from Informix or Sybase might require such info up front.

> However, i've not tried it... I'll try and get some time!

That'd be great if you could... there appears to still be a problem occurring 
at "EXEC SQL DISCONNECT con_name". I'll look into it tonight if I can.

All this does kinda raise the interesting question of why it worked at all on 
FreeBSD... probably different scheduling and blind luck, I suppose.

Thanks for the reponse - I'm a happy man. By 7.4, we should be able to start 
porting our apps to Postgres in earnest.

Regards, Philip.



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2003-06-26 01:19 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-06-24 08:25 ECPG still having thread problems on Linux Philip Yarra <philip@utiba.com>
2003-06-25 09:35 ` Lee Kindness <lkindness@csl.co.uk>
2003-06-26 01:19   ` Philip Yarra <philip@utiba.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox