Skip to content

Commit 02cecd9

Browse files
committed
php#53: + Add new tests for stream_select()
1 parent 09dbef1 commit 02cecd9

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

ext/standard/streamsfuncs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ PHP_FUNCTION(stream_select)
746746
ZEND_PARSE_PARAMETERS_END();
747747

748748
// Early async select path - avoid all fd_set processing
749-
if(ZEND_ASYNC_IS_ACTIVE) {
749+
if(ZEND_ASYNC_IS_ACTIVE && (sec > 0 || usec > 0)) {
750750
struct timeval tv_async, *tv_p_async = NULL;
751751
if (!secnull) {
752752
tv_async.tv_sec = sec;

main/network_async.c

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -793,15 +793,23 @@ typedef struct async_stream_callback_s {
793793
struct async_stream_callback_s *next; // For linked list
794794
} async_stream_callback_t;
795795

796-
static zend_always_inline void add_stream_to_array(zval *array, zval *key, zval *stream_zval) {
797-
if (array == NULL) return;
798-
796+
static zend_always_inline void add_stream_to_array(zval *array, zval *key, zval *stream_zval)
797+
{
798+
if (array == NULL) {
799+
return;
800+
}
801+
802+
if (Z_REFCOUNT_P(array) > 1) {
803+
SEPARATE_ARRAY(array);
804+
}
805+
799806
zval *dest_elem;
800807
if (Z_TYPE_P(key) == IS_STRING) {
801808
dest_elem = zend_hash_add(Z_ARR_P(array), Z_STR_P(key), stream_zval);
802809
} else {
803810
dest_elem = zend_hash_index_add(Z_ARR_P(array), Z_LVAL_P(key), stream_zval);
804811
}
812+
805813
if (dest_elem) zval_add_ref(dest_elem);
806814
}
807815

@@ -869,7 +877,7 @@ static zend_always_inline bool process_stream_array(zval *streams, async_poll_ev
869877
php_stream_from_zval_no_verify(stream, z_stream);
870878

871879
if (stream == NULL) {
872-
continue;
880+
return false;
873881
}
874882

875883
// Try to get async event handle from socket streams first
@@ -948,6 +956,10 @@ static zend_always_inline bool process_stream_array(zval *streams, async_poll_ev
948956
);
949957
} ZEND_HASH_FOREACH_END();
950958

959+
if (Z_REFCOUNT_P(streams) > 1) {
960+
SEPARATE_ARRAY(streams);
961+
}
962+
951963
// Now clean up the input array to prepare for results
952964
zend_hash_clean(Z_ARR_P(streams));
953965

@@ -1014,16 +1026,6 @@ ZEND_API int network_async_stream_select(zval *read_streams, zval *write_streams
10141026
// Initialize result counter
10151027
ZVAL_LONG(&coroutine->waker->result, 0);
10161028

1017-
if (read_streams != NULL && Z_REFCOUNT_P(read_streams) > 1) {
1018-
SEPARATE_ARRAY(read_streams);
1019-
}
1020-
if (write_streams != NULL && Z_REFCOUNT_P(write_streams) > 1) {
1021-
SEPARATE_ARRAY(write_streams);
1022-
}
1023-
if (except_streams != NULL && Z_REFCOUNT_P(except_streams) > 1) {
1024-
SEPARATE_ARRAY(except_streams);
1025-
}
1026-
10271029
// Process all stream arrays using the helper function
10281030
if (UNEXPECTED(!process_stream_array(read_streams, ASYNC_READABLE, coroutine, read_streams, write_streams, except_streams, &result))) {
10291031
goto cleanup;
@@ -1035,6 +1037,10 @@ ZEND_API int network_async_stream_select(zval *read_streams, zval *write_streams
10351037
goto cleanup;
10361038
}
10371039

1040+
if (coroutine->waker->events.nNumOfElements == 0) {
1041+
goto cleanup;
1042+
}
1043+
10381044
// Suspend until events occur or timeout
10391045
ZEND_ASYNC_SUSPEND();
10401046
IF_EXCEPTION_GOTO_ERROR;

win32/select.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ PHPAPI int php_select(php_socket_t max_fd, fd_set *rfds, fd_set *wfds, fd_set *e
9696
if (n_handles == 0) {
9797
/* plain sockets only - let winsock handle the whole thing */
9898
// For win32 we support only sockets in this function
99-
if (ZEND_ASYNC_IS_ACTIVE) {
99+
if (ZEND_ASYNC_IS_ACTIVE && (tv->tv_sec > 0 || tv->tv_usec > 0)) {
100100
return php_select_async(max_fd, rfds, wfds, efds, tv);
101101
}
102102
return select(-1, rfds, wfds, efds, tv);

0 commit comments

Comments
 (0)