php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #41291 FastCGI does not set SO_REUSEADDR
Submitted: 2007-05-04 19:13 UTC Modified: 2007-05-10 15:22 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: fmajid at kefta dot com Assigned: dmitry (profile)
Status: Closed Package: CGI/CLI related
PHP Version: 5.2.2 OS: Solaris
Private report: No CVE-ID: None
 [2007-05-04 19:13 UTC] fmajid at kefta dot com
Description:
------------
The CGI/FastCGI SAPI does not set the SO_REUSEADDR option. This can prevent the FastCGI server from being restarted if it exits with sockets in the TIME_WAIT state.

The patch below fixes this.

That said, a much better option would be to refactor the fastcgi.c code to use php_network_bind_socket_to_local_addr instead of reinventing the square wheel.

*** php-5.2.1/sapi/cgi/fastcgi.c~       Mon Jan  1 01:36:12 2007
--- php-5.2.1/sapi/cgi/fastcgi.c        Fri May  4 11:45:39 2007
***************
*** 253,258 ****
--- 253,261 ----
        int       listen_socket;
        sa_t      sa;
        socklen_t sock_len;
+ #ifdef SO_REUSEADDR
+       int       val = 1;
+ #endif

        if ((s = strchr(path, ':'))) {
                port = atoi(s+1);
***************
*** 308,313 ****
--- 311,319 ----

        /* Create, bind socket and start listen on it */
        if ((listen_socket = socket(sa.sa.sa_family, SOCK_STREAM, 0)) < 0 ||
+ #ifdef SO_REUSEADDR
+               setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, (char*)&val, sizeof(val)) < 0 ||
+ #endif
            bind(listen_socket, (struct sockaddr *) &sa, sock_len) < 0 ||
            listen(listen_socket, backlog) < 0) {



Reproduce code:
---------------
Run a FastCGI web server and start the FastCGI SAPI using php -b.
Kill the FastCGI process.
Attempt to restart it.

Expected result:
----------------
FastCGI server restarting normally

Actual result:
--------------
alamut ~>php-cgi -b 127.0.0.1:8888 -c /home/majid/web/conf
Cannot bind/listen socket - [125] Address already in use.
Couldn't create FastCGI listen socket on port 127.0.0.1:8888


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-05-10 15:22 UTC] dmitry@php.net
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
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 09:01:32 2024 UTC