Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: only flush temporary unreferenced streams #1351

Merged
merged 4 commits into from
Jan 26, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions frankenphp.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,29 @@ static void frankenphp_destroy_super_globals() {
zend_end_try();
}

/*
* free php_stream resources that are temporary (php_stream_temp_ops)
* streams are globally registered in EG(regular_list), see zend_list.c
* this fixes a leak when reading the body of a request
*/
static void frankenphp_release_temporary_streams() {
zend_resource *val;
int stream_type = php_file_le_stream();
ZEND_HASH_FOREACH_PTR(&EG(regular_list), val) {
/* verify the resource is a stream */
if (val->type == stream_type) {
php_stream *stream = (php_stream *)val->ptr;
if (stream != NULL && stream->ops == &php_stream_temp_ops &&
!stream->is_persistent && stream->__exposed == 0 &&
GC_REFCOUNT(val) == 1) {
zend_list_close(val);
zend_list_delete(val);
}
}
}
ZEND_HASH_FOREACH_END();
}

/* Adapted from php_request_shutdown */
static void frankenphp_worker_request_shutdown() {
/* Flush all output buffers */
Expand Down Expand Up @@ -135,23 +158,6 @@ static void frankenphp_worker_request_shutdown() {
zend_end_try();

zend_set_memory_limit(PG(memory_limit));

/*
* free any php_stream resources that are not php source files
* all resources are stored in EG(regular_list), see zend_list.c
*/
zend_resource *val;
ZEND_HASH_FOREACH_PTR(&EG(regular_list), val) {
/* verify the resource is a stream */
if (val->type == php_file_le_stream()) {
php_stream *stream = (php_stream *)val->ptr;
if (stream != NULL && stream->ops != &php_stream_stdio_ops &&
!stream->is_persistent && GC_REFCOUNT(val) == 1) {
zend_list_delete(val);
}
}
}
ZEND_HASH_FOREACH_END();
}

PHPAPI void get_full_env(zval *track_vars_array) {
Expand Down Expand Up @@ -179,6 +185,7 @@ static int frankenphp_worker_request_startup() {

zend_try {
frankenphp_destroy_super_globals();
frankenphp_release_temporary_streams();
php_output_activate();

/* initialize global variables */
Expand Down
Loading