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

Commit cf4cc78

Browse files
committed
Improve postmaster's behavior if an accept() call fails. Because the server
socket is still read-ready, the code was a tight loop, wasting lots of CPU. We can't do anything to clear the failure, other than wait, but we should give other processes more chance to finish and release FDs; so insert a small sleep. Also, avoid bogus "close(-1)" in this case. Per report from Jim Nasby.
1 parent b9c65ae commit cf4cc78

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/backend/libpq/pqcomm.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
3131
* Portions Copyright (c) 1994, Regents of the University of California
3232
*
33-
* $PostgreSQL: pgsql/src/backend/libpq/pqcomm.c,v 1.189 2007/01/05 22:19:29 momjian Exp $
33+
* $PostgreSQL: pgsql/src/backend/libpq/pqcomm.c,v 1.190 2007/02/13 19:18:53 tgl Exp $
3434
*
3535
*-------------------------------------------------------------------------
3636
*/
@@ -528,7 +528,7 @@ Setup_AF_UNIX(void)
528528

529529
/*
530530
* StreamConnection -- create a new connection with client using
531-
* server port.
531+
* server port. Set port->sock to the FD of the new connection.
532532
*
533533
* ASSUME: that this doesn't need to be non-blocking because
534534
* the Postmaster uses select() to tell when the server master
@@ -548,6 +548,14 @@ StreamConnection(int server_fd, Port *port)
548548
ereport(LOG,
549549
(errcode_for_socket_access(),
550550
errmsg("could not accept new connection: %m")));
551+
/*
552+
* If accept() fails then postmaster.c will still see the server
553+
* socket as read-ready, and will immediately try again. To avoid
554+
* uselessly sucking lots of CPU, delay a bit before trying again.
555+
* (The most likely reason for failure is being out of kernel file
556+
* table slots; we can do little except hope some will get freed up.)
557+
*/
558+
pg_usleep(100000L); /* wait 0.1 sec */
551559
return STATUS_ERROR;
552560
}
553561

src/backend/postmaster/postmaster.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*
3838
*
3939
* IDENTIFICATION
40-
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.520 2007/02/11 11:59:25 mha Exp $
40+
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.521 2007/02/13 19:18:54 tgl Exp $
4141
*
4242
* NOTES
4343
*
@@ -1710,7 +1710,8 @@ ConnCreate(int serverFd)
17101710

17111711
if (StreamConnection(serverFd, port) != STATUS_OK)
17121712
{
1713-
StreamClose(port->sock);
1713+
if (port->sock >= 0)
1714+
StreamClose(port->sock);
17141715
ConnFree(port);
17151716
port = NULL;
17161717
}

0 commit comments

Comments
 (0)