469,087 Members | 1,277 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,087 developers. It's quick & easy.

Using connection after fork

I'm looking at a database-using program (PostgreSQL/libpq and MySQL) which
does the following: A daemon process opens a database connection, forks
children on request, and those children access the database using that
inherited connection. After one request, the child dies. It seems to me
that this connection sharing cannot work because the processes would get
mixed up, but the author claims that he has tested this successfully. I've
been trying to figure out the semantics from the libc documentation, but I
can't find a definitive answer. Does anyone know what will happen in this
situation, and why it actually appears to work?

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 23 '05 #1
3 1967
Peter Eisentraut <pe*****@gmx.net> writes:
I'm looking at a database-using program (PostgreSQL/libpq and MySQL) which
does the following: A daemon process opens a database connection, forks
children on request, and those children access the database using that
inherited connection. After one request, the child dies. It seems to me
that this connection sharing cannot work because the processes would get
mixed up,
I agree, unless there is some interlock that prevents a new child from
being spawned before the last previous one died. It would seem to work
under light load where that happened anyway.
but the author claims that he has tested this successfully. I've
been trying to figure out the semantics from the libc documentation, but I
can't find a definitive answer. Does anyone know what will happen in this
situation, and why it actually appears to work?


I'll bet lunch that he simply has not stress-tested it hard enough to
have multiple children actually using the connection at the same time.
I can positively guarantee that it won't work if you do.

It would also not work with SSL, since the encryption state would get
out of sync. And some secondary features like tracking the current
transaction state could get out of sync too. But the basic
query-issuing cycle would probably appear to work as long as only one
child actually had a query in progress at any instant.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #2

On Aug 10, 2004, at 4:41 AM, Peter Eisentraut wrote:
does the following: A daemon process opens a database connection,
forks
children on request, and those children access the database using that
inherited connection. After one request, the child dies. It seems to
me
I've been bit by this sort of thing before. When you fork() the child
inherits all the file descriptors , including the one connected to PG.
But when the child dies that FD will be closed, thus it will also be
closed in the parent causing "odd behavior". (At least, that is what
I've seen in my apps)
that this connection sharing cannot work because the processes would
get
mixed up, but the author claims that he has tested this successfully.
I've
been trying to figure out the semantics from the libc documentation,
but I
can't find a definitive answer. Does anyone know what will happen in
this
situation, and why it actually appears to work?


Does it work for sustained periods of time? Is the parent reconnecting
to PG after each child dies? It *shouldn't* work.
--
Jeff Trout <je**@jefftrout.com>
http://www.jefftrout.com/
http://www.stuarthamm.net/
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #3
Jeff <th******@torgo.978.org> writes:
I've been bit by this sort of thing before. When you fork() the child
inherits all the file descriptors , including the one connected to PG.
But when the child dies that FD will be closed, thus it will also be
closed in the parent causing "odd behavior". (At least, that is what
I've seen in my apps)


That would only happen if the child explicitly does PQfinish() before
exiting, so that the backend sees an 'X' (Terminate) command arrive
and therefore cuts the connection from its end.
If the child just exits the pipe or TCP connection will remain open.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Benoit Dejean | last post: by
6 posts views Thread by shellcode | last post: by
11 posts views Thread by ramu | last post: by
1 post views Thread by vduber6er | last post: by
3 posts views Thread by thrillseekersforever | last post: by
5 posts views Thread by JoeW | last post: by
7 posts views Thread by JamesHoward | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by kglaser89 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.