Skip to content

Commit 6aa47a4

Browse files
committed
php#53: * fixes for php_netstream_get_async_event
1 parent 27d31cd commit 6aa47a4

File tree

3 files changed

+30
-38
lines changed

3 files changed

+30
-38
lines changed

main/network_async.c

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,9 @@ ZEND_API int network_async_await_stream_socket(php_netstream_data_t *netdata, sh
251251
}
252252

253253
zend_ulong async_events = poll2_events_to_async(events);
254-
zend_async_poll_event_t *poll_event = php_netstream_get_async_event(netdata, async_events);
254+
zend_async_poll_proxy_t *poll_event = php_netstream_get_async_event(netdata, async_events);
255255

256-
if (UNEXPECTED(EG(exception) != NULL)) {
256+
if (UNEXPECTED(poll_event == NULL)) {
257257
handle_exception_and_errno();
258258
return -1;
259259
}
@@ -876,21 +876,21 @@ static void async_stream_callback_resolve(
876876

877877
if (EXPECTED(coroutine->waker != NULL)) {
878878
async_stream_callback_t *stream_callback = (async_stream_callback_t *)callback;
879-
879+
880880
zend_async_poll_proxy_t *poll_event = (zend_async_poll_proxy_t *)event;
881-
881+
882882
// Immediately add ready stream to appropriate result array with preserved key
883883
zval stream_zval;
884884
php_stream_to_zval(stream_callback->stream, &stream_zval);
885-
885+
886886
if (stream_callback->read_streams != NULL && poll_event->triggered_events & ASYNC_READABLE) {
887887
add_stream_to_array(stream_callback->read_streams, &stream_callback->key, &stream_zval);
888888
}
889-
889+
890890
if (stream_callback->write_streams != NULL && poll_event->triggered_events & ASYNC_WRITABLE) {
891891
add_stream_to_array(stream_callback->write_streams, &stream_callback->key, &stream_zval);
892892
}
893-
893+
894894
if (stream_callback->except_streams != NULL && poll_event->triggered_events & ASYNC_PRIORITIZED) {
895895
add_stream_to_array(stream_callback->except_streams, &stream_callback->key, &stream_zval);
896896
}
@@ -1046,7 +1046,7 @@ ZEND_API int network_async_stream_select(zval *read_streams, zval *write_streams
10461046
}
10471047

10481048
int result = 0;
1049-
1049+
10501050
// Calculate timeout in milliseconds
10511051
zend_ulong timeout = 0;
10521052
if (tv != NULL) {
@@ -1095,7 +1095,7 @@ ZEND_API int network_async_stream_select(zval *read_streams, zval *write_streams
10951095
zval_ptr_dtor(&cb->key);
10961096
} ZEND_HASH_FOREACH_END();
10971097
}
1098-
1098+
10991099
zend_async_waker_clean(coroutine);
11001100
return result;
11011101
}
@@ -1107,14 +1107,14 @@ ZEND_API int network_async_stream_select(zval *read_streams, zval *write_streams
11071107
/**
11081108
* Async version of php_network_accept_incoming
11091109
* Accepts an incoming connection on a server socket using the modern async system
1110-
*
1110+
*
11111111
* @param stream Server socket stream
1112-
* @param textaddr Output: text representation of client address
1112+
* @param textaddr Output: text representation of client address
11131113
* @param addr Output: client socket address structure
11141114
* @param addrlen Output: length of client address structure
11151115
* @param timeout Accept timeout
11161116
* @param error_string Output: error message string
1117-
* @param error_code Output: error code
1117+
* @param error_code Output: error code
11181118
* @param tcp_nodelay Whether to set TCP_NODELAY on accepted socket
11191119
* @return Client socket fd, or -1 on error
11201120
*/
@@ -1204,7 +1204,7 @@ ZEND_API php_socket_t network_async_accept_incoming(php_netstream_data_t *netdat
12041204
/**
12051205
* Async version of php_network_connect_socket
12061206
* Connects to a remote address using the modern async system
1207-
*
1207+
*
12081208
* @param stream Socket stream
12091209
* @param sockfd Socket file descriptor (from stream)
12101210
* @param addr Remote socket address to connect to
@@ -1282,7 +1282,7 @@ ZEND_API int network_async_connect_socket(php_netstream_data_t *netdata, php_soc
12821282

12831283
// Use the modern async await mechanism instead of php_pollfd_for loop
12841284
n = network_async_await_stream_socket(netdata, events, timeout);
1285-
1285+
12861286
if (n < 0) {
12871287
error = errno;
12881288
ret = -1;
@@ -1540,7 +1540,7 @@ ZEND_API struct hostent* php_network_gethostbyname_async(const char *name)
15401540
hints.ai_socktype = SOCK_STREAM;
15411541

15421542
struct addrinfo *result = NULL;
1543-
1543+
15441544
if (php_network_getaddrinfo_async(name, NULL, &hints, &result) != 0) {
15451545
return NULL;
15461546
}
@@ -1676,7 +1676,7 @@ ZEND_API zend_string* php_network_gethostbyaddr_async(const char *ip)
16761676

16771677
/**
16781678
* Asynchronous network address resolution implementation for coroutine contexts.
1679-
*
1679+
*
16801680
* This function resolves a hostname to multiple socket addresses, similar to
16811681
* the standard getaddrinfo() but compatible with the async coroutine system.
16821682
*/
@@ -1757,16 +1757,16 @@ ZEND_API int php_network_getaddresses_async(const char *host, int socktype, stru
17571757
*
17581758
* @param netdata Network stream data structure
17591759
* @param events Event mask (ASYNC_READABLE, ASYNC_WRITABLE, etc.)
1760-
* @return Poll event handle, or NULL on error
1760+
* @return Poll-proxy event handle, or NULL on error
17611761
*/
1762-
ZEND_API zend_async_poll_event_t* php_netstream_get_async_event(php_netstream_data_t *netdata, zend_ulong events)
1762+
ZEND_API zend_async_poll_proxy_t* php_netstream_get_async_event(php_netstream_data_t *netdata, async_poll_event events)
17631763
{
17641764
ZEND_ASSERT(netdata != NULL && "netdata must not be NULL");
17651765

17661766
// Create base poll event if needed
17671767
if (netdata->poll_event == NULL) {
17681768
netdata->poll_event = ZEND_ASYNC_NEW_SOCKET_EVENT(netdata->socket, 0);
1769-
if (UNEXPECTED(EG(exception) != NULL)) {
1769+
if (UNEXPECTED(netdata->poll_event == NULL)) {
17701770
return NULL;
17711771
}
17721772

@@ -1784,29 +1784,29 @@ ZEND_API zend_async_poll_event_t* php_netstream_get_async_event(php_netstream_da
17841784

17851785
// Return cached read event if it matches
17861786
if (netdata->read_event && (events & READ_EVENT_MASK) == events) {
1787-
return (zend_async_poll_event_t*)netdata->read_event;
1787+
return netdata->read_event;
17881788
}
17891789

17901790
// Return cached write event if it matches
1791-
if (events == ASYNC_WRITABLE && netdata->write_event) {
1792-
return (zend_async_poll_event_t*)netdata->write_event;
1791+
if (netdata->write_event && events == ASYNC_WRITABLE) {
1792+
return netdata->write_event;
17931793
}
17941794

17951795
// Create new proxy for any events
17961796
zend_async_poll_proxy_t *proxy = ZEND_ASYNC_NEW_POLL_PROXY_EVENT(netdata->poll_event, events);
1797-
if (UNEXPECTED(EG(exception) != NULL)) {
1797+
if (UNEXPECTED(proxy == NULL)) {
17981798
return NULL;
17991799
}
18001800

18011801
// Cache proxy for reuse
1802-
if ((events & READ_EVENT_MASK) == events) {
1802+
if (netdata->read_event == NULL && (events & READ_EVENT_MASK) == events) {
18031803
netdata->read_event = proxy;
1804-
} else if (events == ASYNC_WRITABLE) {
1804+
} else if (netdata->write_event == NULL && events == ASYNC_WRITABLE) {
18051805
netdata->write_event = proxy;
18061806
} else {
18071807
// Don't cache non-standard event combinations
18081808
proxy->base.ref_count = 0;
18091809
}
18101810

1811-
return (zend_async_poll_event_t*)proxy;
1812-
}
1811+
return proxy;
1812+
}

main/network_async.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ ZEND_API void network_async_set_socket_blocking(php_socket_t socket, bool blocki
2424
ZEND_API bool network_async_ensure_socket_nonblocking(php_socket_t socket);
2525
ZEND_API void network_async_wait_socket(php_socket_t socket, const zend_ulong events, const zend_ulong timeout);
2626
ZEND_API int network_async_await_stream_socket(php_netstream_data_t *netdata, short events, struct timeval *timeout);
27-
ZEND_API zend_async_poll_event_t* php_netstream_get_async_event(php_netstream_data_t *netdata, zend_ulong events);
27+
ZEND_API zend_async_poll_proxy_t* php_netstream_get_async_event(php_netstream_data_t *netdata, async_poll_event events);
2828

2929
ZEND_API int php_poll2_async(php_pollfd *ufds, unsigned int nfds, int timeout);
3030
ZEND_API int php_select_async(php_socket_t max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *tv);
@@ -45,4 +45,4 @@ ZEND_API int php_network_getaddresses_async(const char *host, int socktype, stru
4545

4646
END_EXTERN_C()
4747

48-
#endif //NETWORK_ASYNC_H
48+
#endif //NETWORK_ASYNC_H

main/streams/xp_socket.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ static ssize_t php_sockop_write(php_stream *stream, const char *buf, size_t coun
9292
sock->timeout_event = false;
9393

9494
if (ZEND_ASYNC_IS_ACTIVE) {
95-
retval = network_async_await_stream_socket(sock, POLLOUT, ptimeout);
95+
retval = network_async_await_stream_socket(sock, POLLOUT, ptimeout);
9696

9797
if (retval == 0) {
9898
sock->timeout_event = true;
@@ -138,14 +138,6 @@ static ssize_t php_sockop_write(php_stream *stream, const char *buf, size_t coun
138138
php_stream_notify_progress_increment(PHP_STREAM_CONTEXT(stream), didwrite, 0);
139139
}
140140

141-
if (ZEND_ASYNC_IS_ACTIVE && sock->is_blocked && !sock->nonblocking_applied) {
142-
network_async_set_socket_blocking(sock->socket, true, sock);
143-
if (UNEXPECTED(EG(exception) != NULL)) {
144-
/* If we are in async context and an exception was thrown, we should not continue. */
145-
return -1;
146-
}
147-
}
148-
149141
return didwrite;
150142
}
151143

0 commit comments

Comments
 (0)