Skip to content

Commit 0bcd2cc

Browse files
committed
A much better probable fix for php#16114.
1 parent bc0948b commit 0bcd2cc

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

main/network.c

+14-1
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,8 @@ static size_t php_sockop_read(php_stream *stream, char *buf, size_t count TSRMLS
849849
static int php_sockop_close(php_stream *stream, int close_handle TSRMLS_DC)
850850
{
851851
php_netstream_data_t *sock = (php_netstream_data_t*)stream->abstract;
852+
fd_set wrfds, efds;
853+
int n;
852854

853855
if (close_handle) {
854856
#if HAVE_OPENSSL_EXT
@@ -862,7 +864,18 @@ static int php_sockop_close(php_stream *stream, int close_handle TSRMLS_DC)
862864
}
863865
#endif
864866

865-
/* shutdown(sock->socket, 0); */
867+
/* prevent more data from coming in */
868+
shutdown(sock->socket, SHUT_RD);
869+
870+
/* make sure that the OS sends all data before we close the connection */
871+
do {
872+
FD_ZERO(&wrfds);
873+
FD_SET(sock->socket, &wrfds);
874+
efds = wrfds;
875+
876+
n = select(sock->socket + 1, NULL, &wrfds, &efds, NULL);
877+
} while (n == -1 && php_socket_errno() == EINTR);
878+
866879
closesocket(sock->socket);
867880

868881
}

main/php_network.h

+11
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
# undef FD_SETSIZE
2828
# include "arpa/inet.h"
2929
# define socklen_t unsigned int
30+
# define SHUT_RD SD_RECEIVE
31+
# define SHUT_WR SD_SEND
32+
# define SHUT_RDWR SD_BOTH
3033
#else
3134
# undef closesocket
3235
# define closesocket close
@@ -62,6 +65,14 @@ PHPAPI char *php_socket_strerror(long err, char *buf, size_t bufsize);
6265
#include <sys/socket.h>
6366
#endif
6467

68+
/* These are here, rather than with the win32 counterparts above,
69+
* since <sys/socket.h> defines them. */
70+
#ifndef SHUT_RD
71+
# define SHUT_RD 0
72+
# define SHUT_WR 1
73+
# define SHUT_RDWR 2
74+
#endif
75+
6576
#ifdef HAVE_SYS_TIME_H
6677
#include <sys/time.h>
6778
#endif

0 commit comments

Comments
 (0)