Skip to content

Fix memory-leak in CLI web server #9680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

b-viguier
Copy link
Contributor

@b-viguier b-viguier commented Oct 6, 2022

Hi all 👋 🙂 ,
my local test suite is complaining about a memory leak (see valgrind report below)

Valgrind Report
==471513== 112 bytes in 3 blocks are definitely lost in loss record 22 of 30
==471513==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==471513==    by 0x8BBB7B: __zend_malloc (zend_alloc.c:3089)
==471513==    by 0xA79298: zend_string_alloc (zend_string.h:150)
==471513==    by 0xA79338: zend_string_init (zend_string.h:172)
==471513==    by 0xA7DA55: php_cli_server_client_read_request_on_header_field (php_cli_server.c:1706)
==471513==    by 0xA780CC: php_http_parser_execute (php_http_parser.c:1121)
==471513==    by 0xA7DF17: php_cli_server_client_read_request (php_cli_server.c:1843)
==471513==    by 0xA7FCAC: php_cli_server_recv_event_read_request (php_cli_server.c:2557)
==471513==    by 0xA801C3: php_cli_server_do_event_for_each_fd_callback (php_cli_server.c:2661)
==471513==    by 0xA7BAE8: php_cli_server_poller_iter_on_active (php_cli_server.c:909)
==471513==    by 0xA8025D: php_cli_server_do_event_for_each_fd (php_cli_server.c:2681)
==471513==    by 0xA802E9: php_cli_server_do_event_loop (php_cli_server.c:2692)

Based on my research, it has been introduced in Php8.2 in this PR #8605
This is related to the zend_string containing the current header name during parsing.
Then, if your HTTP request doesn't include any header, there is no leak.

I'm not yet usual with the php-src test suite, but if you have an idea how to cover this leak by a test, I can give it a try.

Thanks a lot for your amazing work 🤗

@cmb69
Copy link
Member

cmb69 commented Oct 6, 2022

I'm not yet usual with the php-src test suite, but if you have an idea how to cover this leak by a test, I can give it a try.

I'm surprised that this leak hasn't been reported by at least one of our CI pipelines. I can reproduce that with a simple request to a not found page (using Windows debug heap, not even valgrind).

Anyway, thanks for the PR! I think this patch is good.

/cc @Girgias

@b-viguier b-viguier marked this pull request as ready for review October 6, 2022 09:51
@Girgias
Copy link
Member

Girgias commented Oct 6, 2022

LGTM, @cmb69 I suppose you did the request manually and don't have a test? (as otherwise it would have been flagged already?)

@Girgias Girgias closed this in 58fc34f Oct 6, 2022
@b-viguier b-viguier deleted the cli-fix-memory-leak branch October 6, 2022 12:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants