Skip to content

Commit 4542ff8

Browse files
committed
php#53: Refactor async networking to use unified event handling
- Replace php_poll2_async with network_async_accept_incoming() to prevent EventLoop conflicts - Add network_async_connect_socket() for consistent async connection handling - Fix SSL socket EventLoop duplication in accept and close operations - Add proper poll_event cleanup in SSL sockets to prevent memory leaks - Create SSL async tests for timeout, client-server, and concurrent operations + test 025-ssl_stream_socket_accept_timeout.phpt
1 parent 477d9db commit 4542ff8

File tree

1 file changed

+34
-11
lines changed

1 file changed

+34
-11
lines changed

ext/openssl/xp_ssl.c

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "zend_smart_str.h"
2929
#include "zend_exceptions.h"
3030
#include "php_openssl.h"
31+
#include "main/network_async.h"
3132
#include "php_openssl_backend.h"
3233
#include "php_network.h"
3334
#include <openssl/ssl.h>
@@ -2170,9 +2171,14 @@ static int php_openssl_sockop_close(php_stream *stream, int close_handle) /* {{{
21702171
* We use a small timeout which should encourage the OS to send the data,
21712172
* but at the same time avoid hanging indefinitely.
21722173
* */
2173-
do {
2174-
n = php_pollfd_for_ms(sslsock->s.socket, POLLOUT, 500);
2175-
} while (n == -1 && php_socket_errno() == EINTR);
2174+
if (ZEND_ASYNC_IS_ACTIVE) {
2175+
struct timeval tv = {0, 500000}; // 500ms
2176+
network_async_await_stream_socket(stream, POLLOUT, &tv);
2177+
} else {
2178+
do {
2179+
n = php_pollfd_for_ms(sslsock->s.socket, POLLOUT, 500);
2180+
} while (n == -1 && php_socket_errno() == EINTR);
2181+
}
21762182
#endif
21772183
closesocket(sslsock->s.socket);
21782184
sslsock->s.socket = SOCK_ERR;
@@ -2198,6 +2204,12 @@ static int php_openssl_sockop_close(php_stream *stream, int close_handle) /* {{{
21982204
pefree(sslsock->reneg, php_stream_is_persistent(stream));
21992205
}
22002206

2207+
/* Cleanup async event handle before freeing socket structure */
2208+
if (sslsock->s.poll_event) {
2209+
sslsock->s.poll_event->base.dispose(&sslsock->s.poll_event->base);
2210+
sslsock->s.poll_event = NULL;
2211+
}
2212+
22012213
pefree(sslsock, php_stream_is_persistent(stream));
22022214

22032215
return 0;
@@ -2231,14 +2243,25 @@ static inline int php_openssl_tcp_sockop_accept(php_stream *stream, php_openssl_
22312243
nodelay = 1;
22322244
}
22332245

2234-
clisock = php_network_accept_incoming(sock->s.socket,
2235-
xparam->want_textaddr ? &xparam->outputs.textaddr : NULL,
2236-
xparam->want_addr ? &xparam->outputs.addr : NULL,
2237-
xparam->want_addr ? &xparam->outputs.addrlen : NULL,
2238-
xparam->inputs.timeout,
2239-
xparam->want_errortext ? &xparam->outputs.error_text : NULL,
2240-
&xparam->outputs.error_code,
2241-
nodelay);
2246+
if (ZEND_ASYNC_IS_ACTIVE) {
2247+
clisock = network_async_accept_incoming(stream,
2248+
xparam->want_textaddr ? &xparam->outputs.textaddr : NULL,
2249+
xparam->want_addr ? &xparam->outputs.addr : NULL,
2250+
xparam->want_addr ? &xparam->outputs.addrlen : NULL,
2251+
xparam->inputs.timeout,
2252+
xparam->want_errortext ? &xparam->outputs.error_text : NULL,
2253+
&xparam->outputs.error_code,
2254+
nodelay);
2255+
} else {
2256+
clisock = php_network_accept_incoming(sock->s.socket,
2257+
xparam->want_textaddr ? &xparam->outputs.textaddr : NULL,
2258+
xparam->want_addr ? &xparam->outputs.addr : NULL,
2259+
xparam->want_addr ? &xparam->outputs.addrlen : NULL,
2260+
xparam->inputs.timeout,
2261+
xparam->want_errortext ? &xparam->outputs.error_text : NULL,
2262+
&xparam->outputs.error_code,
2263+
nodelay);
2264+
}
22422265

22432266
if (clisock >= 0) {
22442267
php_openssl_netstream_data_t *clisockdata = (php_openssl_netstream_data_t*) emalloc(sizeof(*clisockdata));

0 commit comments

Comments
 (0)