From c4c83190426ec4abf6850d38389f507c6ac091e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Sun, 23 Oct 2022 10:18:52 +0200 Subject: [PATCH] fix: memory leak in worker mode (#56) * fix: memory leak in worker mode * fix tests --- frankenphp.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/frankenphp.c b/frankenphp.c index cf54911..8a93f6e 100644 --- a/frankenphp.c +++ b/frankenphp.c @@ -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; iworker && 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;