Thread: BUG #1958: Postmaster doesn't close open file handles on startup

BUG #1958: Postmaster doesn't close open file handles on startup

From
"Menno Smits"
Date:
The following bug has been logged online:

Bug reference:      1958
Logged by:          Menno Smits
Email address:      menno@netboxblue.com
PostgreSQL version: 8.0.3
Operating system:   Linux (Fedora Core 3)
Description:        Postmaster doesn't close open file handles on startup
Details:

The postmaster process doesn't close open file handles/sockets when it
daemonises. This means it inherits all open file handles that the parent
process had. This can lead to strange side effects because postgres will
hold sockets and files that it really shouldn't.

The following short Python script demonstrates the problem. It starts
listening on TCP port 4444 and then restarts postgresql. Run with "python
<script>.py".

Postgresql should be running before the script is run.
-----------------------------------------------------
import os
import socket

# Start listening on TCP port 444
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('127.0.0.1', 4444))
s.listen(5)

# Restart postgres
os.system('/etc/init.d/postgresql restart')

s.close()
-----------------------------------------------------

After the script is run use the following command to see what sockets
postmaster has:
    netstat -tnlp | grep postmaster

The output will be something like:
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN
   5813/postmaster
tcp        0      0 127.0.0.1:4444          0.0.0.0:*               LISTEN
   5813/postmaster

Port 5432 makes sense but clearly it shouldn't be listening on 4444.

Rerunning the python script will fail because postmaster still has the
socket open:
------------------------------------------------------
Traceback (most recent call last):
  File "postgres-bug.py", line 6, in ?
    s.bind(('127.0.0.1', 4444))
  File "<string>", line 1, in bind
socket.error: (98, 'Address already in use')
------------------------------------------------------

It is considered good UNIX programming practise to close open file handles
when daemonising. See
http://www.enderunix.org/docs/eng/daemon.php

Re: BUG #1958: Postmaster doesn't close open file handles on startup

From
Tom Lane
Date:
"Menno Smits" <menno@netboxblue.com> writes:
> The postmaster process doesn't close open file handles/sockets when it
> daemonises. This means it inherits all open file handles that the parent
> process had. This can lead to strange side effects because postgres will
> hold sockets and files that it really shouldn't.

I think this is a bug in whatever you are using to launch the
postmaster, not a bug in the postmaster.

            regards, tom lane

Re: BUG #1958: Postmaster doesn't close open file handles on

From
Bruce Momjian
Date:
I guess we could, though I have rarely seen programs do this.  They
usually just to the std* descriptors.

---------------------------------------------------------------------------

Menno Smits wrote:
>
> The following bug has been logged online:
>
> Bug reference:      1958
> Logged by:          Menno Smits
> Email address:      menno@netboxblue.com
> PostgreSQL version: 8.0.3
> Operating system:   Linux (Fedora Core 3)
> Description:        Postmaster doesn't close open file handles on startup
> Details:
>
> The postmaster process doesn't close open file handles/sockets when it
> daemonises. This means it inherits all open file handles that the parent
> process had. This can lead to strange side effects because postgres will
> hold sockets and files that it really shouldn't.
>
> The following short Python script demonstrates the problem. It starts
> listening on TCP port 4444 and then restarts postgresql. Run with "python
> <script>.py".
>
> Postgresql should be running before the script is run.
> -----------------------------------------------------
> import os
> import socket
>
> # Start listening on TCP port 444
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> s.bind(('127.0.0.1', 4444))
> s.listen(5)
>
> # Restart postgres
> os.system('/etc/init.d/postgresql restart')
>
> s.close()
> -----------------------------------------------------
>
> After the script is run use the following command to see what sockets
> postmaster has:
>     netstat -tnlp | grep postmaster
>
> The output will be something like:
> tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN
>    5813/postmaster
> tcp        0      0 127.0.0.1:4444          0.0.0.0:*               LISTEN
>    5813/postmaster
>
> Port 5432 makes sense but clearly it shouldn't be listening on 4444.
>
> Rerunning the python script will fail because postmaster still has the
> socket open:
> ------------------------------------------------------
> Traceback (most recent call last):
>   File "postgres-bug.py", line 6, in ?
>     s.bind(('127.0.0.1', 4444))
>   File "<string>", line 1, in bind
> socket.error: (98, 'Address already in use')
> ------------------------------------------------------
>
> It is considered good UNIX programming practise to close open file handles
> when daemonising. See
> http://www.enderunix.org/docs/eng/daemon.php
>
> ---------------------------(end of broadcast)---------------------------
> TIP 9: In versions below 8.0, the planner will ignore your desire to
>        choose an index scan if your joining column's datatypes do not
>        match
>

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073