swoole / swoole-src

🚀 Coroutine-based concurrency library for PHP
https://www.swoole.com
Apache License 2.0
18.45k stars 3.16k forks source link

HTTP服务器,添加响应cookie时,如果设置了过期时间会内存泄漏 #5146

Closed doooocli closed 1 year ago

doooocli commented 1 year ago

下面是复现环境和过程,不知道是不是测试方式有问题,感谢指正~

测试环境

[root@DESKTOP-IEJTHJH ~]# uname -a & php -v & gcc -v
[1] 2515
[2] 2516
Linux DESKTOP-IEJTHJH 5.10.102.1-microsoft-standard-WSL2 #1 SMP Wed Mar 2 00:30:59 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
PHP 8.1.19 (cli) (built: Aug  3 2023 22:45:32) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.19, Copyright (c) Zend Technologies
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/8/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl --disable-libmpx --enable-offload-targets=nvptx-none --without-cuda-driver --enable-gnu-indirect-function --enable-cet --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 8.5.0 20210514 (Red Hat 8.5.0-20) (GCC)
[1]-  Done                    uname -a
[2]+  Done                    php -v
[root@DESKTOP-IEJTHJH ~]#
[root@DESKTOP-IEJTHJH ~]# php --ri swoole

swoole

Swoole => enabled
Author => Swoole Team <team@swoole.com>
Version => 5.0.3
Built => Sep  1 2023 17:05:11
coroutine => enabled with boost asm context
epoll => enabled
eventfd => enabled
signalfd => enabled
cpu_affinity => enabled
spinlock => enabled
rwlock => enabled
http2 => enabled
json => enabled
pcre => enabled
zlib => 1.2.11
mutex_timedlock => enabled
pthread_barrier => enabled
futex => enabled
async_redis => enabled

Directive => Local Value => Master Value
swoole.enable_coroutine => On => On
swoole.enable_library => On => On
swoole.enable_preemptive_scheduler => Off => Off
swoole.display_errors => On => On
swoole.use_shortname => On => On
swoole.unixsock_buffer_size => 8388608 => 8388608

测试代码

<?php
$server = new Swoole\Http\Server('0.0.0.0', 9501);

$server->on('Request', function ($request, $response) use($server) {

    if ($request->server['path_info'] == '/favicon.ico' || $request->server['request_uri'] == '/favicon.ico') {
        $response->end();
        return;
    }

    $response->cookie(
        'test_cookie',
        'hello',
        time() + (24 * 60 * 60)
    );

    global $previous;
    global $item;
    $current = memory_get_usage();
    $stats = [
        'id' => $server->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');
});

$server->start();

测试结果

image

注释掉过期时间

    $response->cookie(
        'test_cookie',
        'hello',
        // time() + (24 * 60 * 60)
    );

测试结果

image

NathanFreeman commented 1 year ago

谢谢你的反馈 已经修复了,详情可看https://github.com/swoole/swoole-src/pull/5147