Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 897a895

Browse files
committed
When launching a child backend, take care to close file descriptors for
any other client connections that may exist (which would only happen if another client is currently in the authentication cycle). This avoids wastage of open descriptors in a child. It might also explain peculiar behaviors like not closing connections when expected, since the kernel will probably not signal EOF as long as some other backend is randomly holding open a reference to the connection, even if the client went away long since ...
1 parent 608ddb7 commit 897a895

File tree

1 file changed

+53
-17
lines changed

1 file changed

+53
-17
lines changed

src/backend/postmaster/postmaster.c

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* IDENTIFICATION
14-
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.204 2001/01/27 00:05:31 tgl Exp $
14+
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.205 2001/02/08 00:35:10 tgl Exp $
1515
*
1616
* NOTES
1717
*
@@ -202,6 +202,7 @@ extern void GetRedoRecPtr(void);
202202
static void pmdaemonize(int argc, char *argv[]);
203203
static Port *ConnCreate(int serverFd);
204204
static void ConnFree(Port *port);
205+
static void ClosePostmasterPorts(Port *myConn);
205206
static void reset_shared(unsigned short port);
206207
static void SIGHUP_handler(SIGNAL_ARGS);
207208
static void pmdie(SIGNAL_ARGS);
@@ -1285,6 +1286,51 @@ ConnFree(Port *conn)
12851286
free(conn);
12861287
}
12871288

1289+
/*
1290+
* ClosePostmasterPorts -- close all the postmaster's open sockets
1291+
*
1292+
* This is called during child process startup to release file descriptors
1293+
* that are not needed by that child process. All descriptors other than
1294+
* the one for myConn (if it's not null) are closed.
1295+
*
1296+
* Note that closing the child's descriptor does not destroy the client
1297+
* connection prematurely, since the parent (postmaster) process still
1298+
* has the socket open.
1299+
*/
1300+
static void
1301+
ClosePostmasterPorts(Port *myConn)
1302+
{
1303+
Dlelem *curr;
1304+
1305+
/* Close the listen sockets */
1306+
if (NetServer)
1307+
StreamClose(ServerSock_INET);
1308+
ServerSock_INET = INVALID_SOCK;
1309+
#ifdef HAVE_UNIX_SOCKETS
1310+
StreamClose(ServerSock_UNIX);
1311+
ServerSock_UNIX = INVALID_SOCK;
1312+
#endif
1313+
1314+
/* Close any sockets for other clients, and release memory too */
1315+
curr = DLGetHead(PortList);
1316+
1317+
while (curr)
1318+
{
1319+
Port *port = (Port *) DLE_VAL(curr);
1320+
Dlelem *next = DLGetSucc(curr);
1321+
1322+
if (port != myConn)
1323+
{
1324+
StreamClose(port->sock);
1325+
DLRemove(curr);
1326+
ConnFree(port);
1327+
DLFreeElem(curr);
1328+
}
1329+
1330+
curr = next;
1331+
}
1332+
}
1333+
12881334

12891335
/*
12901336
* reset_shared -- reset shared memory and semaphores
@@ -1918,21 +1964,15 @@ DoBackend(Port *port)
19181964
* Signal handlers setting is moved to tcop/postgres...
19191965
*/
19201966

1921-
/* Close the postmaster sockets */
1922-
if (NetServer)
1923-
StreamClose(ServerSock_INET);
1924-
ServerSock_INET = INVALID_SOCK;
1925-
#ifdef HAVE_UNIX_SOCKETS
1926-
StreamClose(ServerSock_UNIX);
1927-
ServerSock_UNIX = INVALID_SOCK;
1928-
#endif
1929-
19301967
/* Save port etc. for ps status */
19311968
MyProcPort = port;
19321969

19331970
/* Reset MyProcPid to new backend's pid */
19341971
MyProcPid = getpid();
19351972

1973+
/* Close the postmaster's other sockets */
1974+
ClosePostmasterPorts(port);
1975+
19361976
/*
19371977
* Don't want backend to be able to see the postmaster random number
19381978
* generator state. We have to clobber the static random_seed *and*
@@ -2225,13 +2265,9 @@ SSDataBase(int xlop)
22252265
/* Lose the postmaster's on-exit routines and port connections */
22262266
on_exit_reset();
22272267

2228-
if (NetServer)
2229-
StreamClose(ServerSock_INET);
2230-
ServerSock_INET = INVALID_SOCK;
2231-
#ifdef HAVE_UNIX_SOCKETS
2232-
StreamClose(ServerSock_UNIX);
2233-
ServerSock_UNIX = INVALID_SOCK;
2234-
#endif
2268+
/* Close the postmaster's sockets */
2269+
ClosePostmasterPorts(NULL);
2270+
22352271

22362272
av[ac++] = "postgres";
22372273

0 commit comments

Comments
 (0)