From f4cdff1b16b86f0e3ca599ea314ce0201b377391 Mon Sep 17 00:00:00 2001 From: twosee Date: Fri, 29 Nov 2019 14:46:27 +0800 Subject: [PATCH] Only free the context after request and response were free'd --- swoole_http2_server.cc | 7 +++---- swoole_http_request.cc | 2 ++ swoole_http_response.cc | 10 ++-------- swoole_http_server.cc | 6 +++--- swoole_websocket_server.cc | 5 ----- 5 files changed, 10 insertions(+), 20 deletions(-) diff --git a/swoole_http2_server.cc b/swoole_http2_server.cc index 07c806baeb8..1c2897c7118 100644 --- a/swoole_http2_server.cc +++ b/swoole_http2_server.cc @@ -41,10 +41,9 @@ http2_stream::http2_stream(int _fd, uint32_t _id) http2_stream::~http2_stream() { - if (ctx) - { - swoole_http_context_free(ctx); - } + ctx->stream = nullptr; + /* it will be free'd when request/response are free'd */ + // swoole_http_context_free(ctx); } void http2_stream::reset(uint32_t error_code) diff --git a/swoole_http_request.cc b/swoole_http_request.cc index b0fc00a2012..44c31c5992d 100644 --- a/swoole_http_request.cc +++ b/swoole_http_request.cc @@ -263,7 +263,9 @@ static void php_swoole_http_request_free_object(zend_object *object) if (ctx) { ctx->request.zobject = NULL; + swoole_http_context_free(ctx); } + zend_object_std_dtor(&request->std); } diff --git a/swoole_http_response.cc b/swoole_http_response.cc index a9a8f1e9a7f..5b492bf65a2 100644 --- a/swoole_http_response.cc +++ b/swoole_http_response.cc @@ -156,8 +156,10 @@ static void php_swoole_http_response_free_object(zend_object *object) } } } + ctx->response.zobject = NULL; swoole_http_context_free(ctx); } + zend_object_std_dtor(&response->std); } @@ -696,10 +698,6 @@ static PHP_METHOD(swoole_http_response, end) #endif { swoole_http_response_end(ctx, zdata, return_value); - if (!ctx->upgrade) - { - swoole_http_context_free(ctx); - } } } @@ -1399,10 +1397,6 @@ static PHP_METHOD(swoole_http_response, redirect) return; } swoole_http_response_end(ctx, nullptr, return_value); - if (!ctx->end) - { - swoole_http_context_free(ctx); - } } static PHP_METHOD(swoole_http_response, __destruct) { } diff --git a/swoole_http_server.cc b/swoole_http_server.cc index 2fd5111309f..73cc346187e 100644 --- a/swoole_http_server.cc +++ b/swoole_http_server.cc @@ -247,11 +247,11 @@ void swoole_http_context_copy(http_context *src, http_context *dst) void swoole_http_context_free(http_context *ctx) { - if (ctx->request.zobject) + /* http context can only be free'd after request and response were free'd */ + if (ctx->request.zobject || ctx->response.zobject) { - php_swoole_http_request_set_context(ctx->request.zobject, NULL); + return; } - php_swoole_http_response_set_context(ctx->response.zobject, NULL); #ifdef SW_USE_HTTP2 if (ctx->stream) { diff --git a/swoole_websocket_server.cc b/swoole_websocket_server.cc index 3bb64e69c3a..316342cad82 100644 --- a/swoole_websocket_server.cc +++ b/swoole_websocket_server.cc @@ -327,7 +327,6 @@ void swoole_websocket_onRequest(http_context *ctx) ctx->send(ctx, (char *) bad_request, strlen(bad_request)); ctx->end = 1; ctx->close(ctx); - swoole_http_context_free(ctx); } void swoole_sha1(const char *str, int _len, unsigned char *digest) @@ -665,10 +664,6 @@ int swoole_websocket_onHandshake(swServer *serv, swListenPort *port, http_contex { serv->close(serv, fd, 1); } - if (!ctx->end) - { - swoole_http_context_free(ctx); - } return SW_OK; }