lpereira / lwan

Experimental, scalable, high performance HTTP server
https://lwan.ws
GNU General Public License v2.0
5.94k stars 548 forks source link

There has memory leak when call lwan_request_get_post_param() #283

Closed fangxinmiao closed 4 years ago

fangxinmiao commented 4 years ago

There has memory leak when call lwan_request_get_post_param()

lpereira commented 4 years ago

I can't find the leak, how did you find it?

That function will allocate an array the first time it's called, which will eventually be freed. It's not always freed right after the request has been handled, but it will if either the connection closes, or after a few requests have been handled by the same connection. This might appear as a memory leak until it's freed but it's by design.

On Wed, Jul 8, 2020, 02:02 Fang Xin Miao notifications@github.com wrote:

There has memory leak when call lwan_request_get_post_param()。

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/lpereira/lwan/issues/283, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAADVGJARGOTS6YJ3J7A3U3R2QY2NANCNFSM4OULI7JA .

fangxinmiao commented 4 years ago

I use wrk to test it. The handler is:

static enum lwan_http_status foobar(struct lwan_request *request,
        struct lwan_response *response, void *param)
{
    const char *value __attribute__((unused)) = 
            lwan_request_get_post_param(request, "key");
    lwan_strbuf_set_static(response->buffer, "foobar", 6);
    return HTTP_OK;
}

The lua script is:

wrk.method = "POST"
wrk.body   = "key=12345678901234567890123456789012345678901234567890123456789012345678901234567890"
wrk.headers["Content-Type"] = "application/x-www-form-urlencoded"

Now run the wrk benchmark:


wrk -t 2 -c 100 -d 24h --script=foobar.lua http://localhost:8080/foobar
``

You will find that the memory of the service will continue to rise.
lpereira commented 4 years ago

Thanks! I'll take a look at this closely with Massif.

lpereira commented 4 years ago

I was able to reproduce it here. This is very odd indeed!

Screenshot from 2020-07-10 09-15-35

lpereira commented 4 years ago

This is interesting. It's also the cause of a performance regression I'm seeing in the TechEmpower Web Framework benchmarks. Looking into this now.

lpereira commented 4 years ago

It seems that I've found and fixed the issue. Can you please confirm if it still persists?