|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #27007 Failed pg_connect leaves Network Socket in CLOSE_WAIT State
Submitted: 2004-01-22 12:16 UTC Modified: 2004-01-28 21:49 UTC
From: bty at requesttech dot com Assigned:
Status: Closed Package: PostgreSQL related
PHP Version: 4.3.4 OS: RedHat 9
Private report: No CVE-ID: None
 [2004-01-22 12:16 UTC] bty at requesttech dot com
When a database connection to postgres fails the assocaited socket is not closed by PHP but kept in a CLOSE_WAIT state.  When doing a tcpdump if the PostgreSQL sends a FIN first the final FIN packet is not Sent by PHP leaving the socket in CLOSE_WAIT.

I have checked bug reports 9252 and 16356.

This IS PHPs problem as it does not correctly send the FIN packet (probably a if( GOT_FIN ) close( socket ) missing from the code) when the PostgreSQL server (or any service) sends FIN first.

This is a serious issue as the only way to remove the CLOSE_WAIT sockets is to restart the web server.  It is obvious that PHP is not doing something correct.

Reproduce code:
$resource_id = pg_connect( "host=good dbname=good user=good password=good" );
pg_close( $resource_id ); 

//This will put the socket in a TIME_WAIT state and will
//close in a matter of minutes

for( $i=0; $i < 1024; $i++ ) {	
	$resource_id = pg_connect( "host=good dbname=good user=good password=bad" );
        if( $resource_id )
	    pg_close( $resource_id );	

//Towards the end of this loop, connections will be refused
//because too many sockets are open.
//The sockets remain in the CLOSE_WAIT state until the web
//server is rebooted

Expected result:
Sockets are closed by the final sending of a FIN packet from PHP.

Actual result:
Sockets are left in CLOSE_WAIT until the web server is rebooted.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2004-01-28 21:38 UTC] bty at requesttech dot com
What I am saying is that PHP is NOT calling client subroutine to close the socket when the database connection fails.  I believe that programmers thought that cleanup would be automatic on a failed login, but it is not the case.

We have telephony applications based on the same postgreSQL libraries that do not have this problem.
 [2004-01-28 21:49 UTC]
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
Thank you for the report, and for helping us make PHP better.

PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 01 22:01:28 2024 UTC