fix: memory leak in worker mode (#56)

* fix: memory leak in worker mode

* fix tests
This commit is contained in:
Kévin Dunglas 2022-10-23 10:18:52 +02:00 committed by GitHub
parent 084decfe97
commit c4c8319042
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -45,6 +45,7 @@ int frankenphp_check_version() {
}
typedef struct frankenphp_server_context {
bool worker;
uintptr_t current_request;
uintptr_t main_request; /* Only available during worker initialization */
char *cookie_data;
@ -76,6 +77,16 @@ static void frankenphp_worker_request_shutdown(uintptr_t current_request) {
if (current_request != 0) go_frankenphp_worker_handle_request_end(current_request);
/* Destroy super-globals */
zend_try {
int i;
for (i=0; i<NUM_TRACK_VARS; i++) {
zval_ptr_dtor(&PG(http_globals)[i]);
}
} zend_end_try();
zend_set_memory_limit(PG(memory_limit));
}
@ -263,10 +274,15 @@ uintptr_t frankenphp_clean_server_context() {
uintptr_t frankenphp_request_shutdown()
{
php_request_shutdown((void *) 0);
frankenphp_server_context *ctx = SG(server_context);
if (ctx->worker && ctx->current_request) {
// Unclean worker shutdown, re-create the superglobals to prevent a segfault
php_hash_environment();
}
php_request_shutdown((void *) 0);
free(ctx->cookie_data);
((frankenphp_server_context*) SG(server_context))->cookie_data = NULL;
uintptr_t rh = frankenphp_clean_server_context();
@ -295,6 +311,7 @@ int frankenphp_create_server_context()
frankenphp_server_context *ctx = calloc(1, sizeof(frankenphp_server_context));
if (ctx == NULL) return FAILURE;
ctx->worker = false;
ctx->current_request = 0;
ctx->main_request = 0;
ctx->cookie_data = NULL;
@ -323,6 +340,8 @@ void frankenphp_update_server_context(
ctx->main_request = main_request;
ctx->current_request = current_request;
if (ctx->main_request) ctx->worker = true;
SG(request_info).auth_password = auth_password;
SG(request_info).auth_user = auth_user;
SG(request_info).request_method = request_method;