Thread: Libpq and multithreading

Libpq and multithreading

From
Asia
Date:
Hello,

I am trying to use libpq in two threads, the issue is that I am getting access violation after several successful
connections.
Each thread connects to different db and disconnects immediately after making a conenction.

So my question is if this is supported by libpq? Is it possible to use it in more than one thread and make connections
atthe same time? 

Kind regards,
Joanna


Re: Libpq and multithreading

From
Bruce Momjian
Date:
On Fri, Jan 11, 2013 at 04:27:42PM +0100, Asia wrote:
> Hello,
>
> I am trying to use libpq in two threads, the issue is that I am getting access violation after several successful
connections.
> Each thread connects to different db and disconnects immediately after making a conenction.
>
> So my question is if this is supported by libpq? Is it possible to use it in more than one thread and make
connectionsat the same time? 

Each connection can be created and accessed from only one thread.

--
  Bruce Momjian  <bruce@momjian.us>        http://momjian.us
  EnterpriseDB                             http://enterprisedb.com

  + It's impossible for everything to be true. +


Re: Libpq and multithreading

From
Asia
Date:
I am using 2 threads, each declares seperate PGconn conenction object.

It connects e.g. 60 times, one connection from one thread, the other connection from the other thread, usually one
aftereach other. 
And it fails at 61'st connection with access violation.

I already tried with PQconnect and PQsetdbLogin. I am using mututal SSL authentication with this connections, maybe
thisis the case? 

Kind regards,
Joanna




Re: Libpq and multithreading

From
Alban Hertroys
Date:
An access violation means that you're trying to access memory that doesn't belong to your process. I'm not sure where it's originating, that could be the server but I suspect the issue is at the client-side.

You're probably just forgetting to free memory somewhere.


On 14 January 2013 13:50, Asia <asia123321@op.pl> wrote:
I am using 2 threads, each declares seperate PGconn conenction object.

It connects e.g. 60 times, one connection from one thread, the other connection from the other thread, usually one after each other.
And it fails at 61'st connection with access violation.

I already tried with PQconnect and PQsetdbLogin. I am using mututal SSL authentication with this connections, maybe this is the case?

Kind regards,
Joanna




--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general



--
If you can't see the forest for the trees,
Cut the trees and you'll see there is no forest.

Re: Libpq and multithreading

From
Merlin Moncure
Date:
On Mon, Jan 14, 2013 at 8:28 AM, Alban Hertroys <haramrae@gmail.com> wrote:
> An access violation means that you're trying to access memory that doesn't
> belong to your process. I'm not sure where it's originating, that could be
> the server but I suspect the issue is at the client-side.
>
> You're probably just forgetting to free memory somewhere.

Or freeing the connection and then another thread attempts to use it.
This might even "work" a couple of times without crashing.

merlin


Re: Libpq and multithreading

From
Asia
Date:
It is not possible because connection is local variable in both thread functions, no common variables are used.

I checked that it also crashes without SSL. Two threads connecting to the same server, different databases.

Joanna


Re: Libpq and multithreading

From
Merlin Moncure
Date:
On Mon, Jan 14, 2013 at 9:57 AM, Asia <asia123321@op.pl> wrote:
> It is not possible because connection is local variable in both thread functions, no common variables are used.
>
> I checked that it also crashes without SSL. Two threads connecting to the same server, different databases.

hm, where is your libpq coming from -- is it possible you are using
the non thread-safe variant?

are you 100% sure there is no thread interplay on the connection itself?

is it possible to get a backtrace for the access violation?

merlin


Re: Libpq and multithreading

From
Asia
Date:
I am sure that I am using seperate threads with seperate connection objects and libpq is compiled to be threadsafe.
I get access violation both with ssl and without it (without ssl it seems to be more stable, however afer several
hundredconnects/disconnects it fails). 

J.


Re: Libpq and multithreading

From
Ashesh Vashi
Date:
A back-trace would definitely be helpful to analyze the issue.

--

Thanks & Regards,

Ashesh Vashi
EnterpriseDB INDIA:
Enterprise PostgreSQL Company

 

http://www.linkedin.com/in/asheshvashi



On Fri, Jan 18, 2013 at 7:35 PM, Asia <asia123321@op.pl> wrote:
I am sure that I am using seperate threads with seperate connection objects and libpq is compiled to be threadsafe.
I get access violation both with ssl and without it (without ssl it seems to be more stable, however afer several hundred connects/disconnects it fails).

J.


--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general