Skip to content

Commit

Permalink
[5.0]Fix bug #5146 (#5147)
Browse files Browse the repository at this point in the history
* Fix bug #5146

* test

* fix comment
  • Loading branch information
NathanFreeman authored Sep 12, 2023
1 parent 1c08246 commit 6ff4d73
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
4 changes: 2 additions & 2 deletions ext-src/php_swoole_http.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@ swoole::http::Context *php_swoole_http_request_get_and_check_context(zval *zobje
swoole::http::Context *php_swoole_http_response_get_and_check_context(zval *zobject);

/**
* using this function can avoid copy elements form old array to new array if the number of elements in the array
* can be sure.
* These class properties cannot be modified by the user before assignment, such as Swoole\\Http\\Request.
* So we can use this function to init property.
*/
static sw_inline zval *swoole_http_init_and_read_property(
zend_class_entry *ce, zval *zobject, zval **zproperty_store_pp, zend_string *name, int size = HT_MIN_SIZE) {
Expand Down
1 change: 1 addition & 0 deletions ext-src/swoole_http_response.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,7 @@ static void php_swoole_http_response_cookie(INTERNAL_FUNCTION_PARAMETERS, const
ZVAL_DOUBLE(&max_age, diff);
convert_to_string(&max_age);
strlcat(cookie, Z_STRVAL_P(&max_age), cookie_size);
zval_ptr_dtor(&max_age);
}
}
if (path_len > 0) {
Expand Down
68 changes: 68 additions & 0 deletions tests/swoole_http_server/bug_5146.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
--TEST--
swoole_http_response: Github#5146 HTTP服务器,添加响应cookie时,如果设置了过期时间会内存泄漏
--SKIPIF--
<?php require __DIR__ . '/../include/skipif.inc'; ?>
--FILE--
<?php
require __DIR__ . '/../include/bootstrap.php';

use Swoole\Http\Server;
use Swoole\Http\Request;
use Swoole\Http\Response;

$pm = new SwooleTest\ProcessManager;
$pm->parentFunc = function () use ($pm) {
Swoole\Coroutine\run(function () use ($pm) {
httpRequest("http://127.0.0.1:{$pm->getFreePort()}");
httpRequest("http://127.0.0.1:{$pm->getFreePort()}");
httpRequest("http://127.0.0.1:{$pm->getFreePort()}");
httpRequest("http://127.0.0.1:{$pm->getFreePort()}");
httpRequest("http://127.0.0.1:{$pm->getFreePort()}");
});
echo "DONE\n";
$pm->kill();
};

$pm->childFunc = function () use ($pm) {
$http = new Server('127.0.0.1', $pm->getFreePort(), SWOOLE_BASE);
$http->set([
'log_file' => '/dev/null',
]);
$http->on('workerStart', function () use ($pm) {
$pm->wakeup();
});
$http->on('request', function (Request $request, Response $response) use ($http) {
$previous = memory_get_usage();
$response->cookie(
'test_cookie',
'hello',
time() + (24 * 60 * 60)
);

global $previous;
global $item;
$current = memory_get_usage();
$stats = [
'id' => $http->getWorkerId(),
'item' => $item++,
'prev_mem' => $previous,
'curr_mem' => $current,
'diff_mem' => $current - $previous,
];
$previous = $current;

echo json_encode($stats), PHP_EOL;
$response->end('test response');
});
$http->start();
};
$pm->childFirst();
$pm->run();
?>
--EXPECTF--
{"id":%d,"item":null,"prev_mem":null,"curr_mem":%d,"diff_mem":%d}
{"id":%d,"item":%d,"prev_mem":%d,"curr_mem":%d,"diff_mem":%d}
{"id":%d,"item":%d,"prev_mem":%d,"curr_mem":%d,"diff_mem":0}
{"id":%d,"item":%d,"prev_mem":%d,"curr_mem":%d,"diff_mem":0}
{"id":%d,"item":%d,"prev_mem":%d,"curr_mem":%d,"diff_mem":0}
DONE

0 comments on commit 6ff4d73

Please sign in to comment.