Skip to content

Commit f2bad2c

Browse files
committed
Dereferencing process-handles no longer waits on those processes.
Implements FR #46487
1 parent 0b0f87e commit f2bad2c

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? 201?, PHP 5.6.0
44

5+
- General improvements:
6+
. Implemented FR #46487 (Dereferencing process-handles no longer waits on those processes). (Jille Timmermans)
7+
58
<<< NOTE: Insert NEWS from last stable release here prior to actual release! >>>

ext/standard/file.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ static ZEND_RSRC_DTOR_FUNC(file_context_dtor)
159159
static void file_globals_ctor(php_file_globals *file_globals_p TSRMLS_DC)
160160
{
161161
FG(pclose_ret) = 0;
162+
FG(pclose_wait) = 0;
162163
FG(user_stream_current_filename) = NULL;
163164
FG(def_chunk_size) = PHP_SOCK_CHUNK_SIZE;
164165
FG(wrapper_errors) = NULL;
@@ -960,7 +961,9 @@ PHP_FUNCTION(pclose)
960961

961962
PHP_STREAM_TO_ZVAL(stream, &arg1);
962963

964+
FG(pclose_wait) = 1;
963965
zend_list_delete(stream->rsrc_id);
966+
FG(pclose_wait) = 0;
964967
RETURN_LONG(FG(pclose_ret));
965968
}
966969
/* }}} */

ext/standard/file.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ typedef struct _php_meta_tags_data {
115115
php_meta_tags_token php_next_meta_token(php_meta_tags_data * TSRMLS_DC);
116116

117117
typedef struct {
118-
int pclose_ret;
118+
int pclose_ret;
119119
size_t def_chunk_size;
120120
long auto_detect_line_endings;
121121
long default_socket_timeout;
@@ -126,6 +126,7 @@ typedef struct {
126126
HashTable *stream_wrappers; /* per-request copy of url_stream_wrappers_hash */
127127
HashTable *stream_filters; /* per-request copy of stream_filters_hash */
128128
HashTable *wrapper_errors; /* key: wrapper address; value: linked list of char* */
129+
int pclose_wait;
129130
} php_file_globals;
130131

131132
#ifdef ZTS

ext/standard/proc_open.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ static void proc_open_rsrc_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
208208
DWORD wstatus;
209209
#elif HAVE_SYS_WAIT_H
210210
int wstatus;
211+
int waitpid_options = 0;
211212
pid_t wait_pid;
212213
#endif
213214

@@ -220,18 +221,27 @@ static void proc_open_rsrc_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
220221
}
221222

222223
#ifdef PHP_WIN32
223-
WaitForSingleObject(proc->childHandle, INFINITE);
224+
if (FG(pclose_wait)) {
225+
WaitForSingleObject(proc->childHandle, INFINITE);
226+
}
224227
GetExitCodeProcess(proc->childHandle, &wstatus);
225-
FG(pclose_ret) = wstatus;
228+
if (wstatus == STILL_ACTIVE) {
229+
FG(pclose_ret) = -1;
230+
} else {
231+
FG(pclose_ret) = wstatus;
232+
}
226233
CloseHandle(proc->childHandle);
227234

228235
#elif HAVE_SYS_WAIT_H
229236

237+
if (!FG(pclose_wait)) {
238+
waitpid_options = WNOHANG;
239+
}
230240
do {
231-
wait_pid = waitpid(proc->child, &wstatus, 0);
241+
wait_pid = waitpid(proc->child, &wstatus, waitpid_options);
232242
} while (wait_pid == -1 && errno == EINTR);
233243

234-
if (wait_pid == -1) {
244+
if (wait_pid <= 0) {
235245
FG(pclose_ret) = -1;
236246
} else {
237247
if (WIFEXITED(wstatus))
@@ -300,7 +310,9 @@ PHP_FUNCTION(proc_close)
300310

301311
ZEND_FETCH_RESOURCE(proc, struct php_process_handle *, &zproc, -1, "process", le_proc_open);
302312

313+
FG(pclose_wait) = 1;
303314
zend_list_delete(Z_LVAL_P(zproc));
315+
FG(pclose_wait) = 0;
304316
RETURN_LONG(FG(pclose_ret));
305317
}
306318
/* }}} */

0 commit comments

Comments
 (0)