php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #32220 [PATCH] thread_resources for thread not getting freed when apache kills thread
Submitted: 2005-03-07 15:06 UTC Modified: 2017-10-24 07:50 UTC
Votes:7
Avg. Score:3.7 ± 0.9
Reproduced:2 of 3 (66.7%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: kameshj at fastmail dot fm Assigned:
Status: Open Package: Apache2 related
PHP Version: 5.*, 6CVS (2008-08-15) OS: *
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2005-03-07 15:06 UTC] kameshj at fastmail dot fm
Description:
------------
Run apache2 in a worker thread model with the following settings
StartThreads 3
MaxSpareThreads 4
MaxThreads 10
Loadtests on a very simple php scripts like the one that calls phpinfo() at regular intervals of 5 minutes causes a memory leak(Not really a leak but huge unused memory that gets freed at tsrm_shutdown)

This causes the memory consumed by the apache2 procsss to shoot up.

---Investigation
Apache2 to handle the concurrent requests increases the number of child threads to max of MaxThreads when no more request to server it kills the threads till the number of threads comes to MaxSpareThreads.

In the case of setting given above after the load test of 10 simultaneous requests,
Apache kills 6 threads (As MaxThreads-MaxSpareThreads = 10-4 = 6).
The thread_resources momory associated with these 6 threads still not freed as soon as the corresponding thread is killed.
These kind of spurt loads at regular intervals causes lots of such thread_resources memory to get accumulated not being used for any real purpose.

Only when the sapi module comes down it calls tsrm_shutdown then these thread_resources are getting freed.

Attaching the patch to fix the issue is available at
http://puggy.symonds.net/~kameshj/TSRM.c.patch

This patch has a few clean up to existing ts_free_thread.
This patch registers ts_free_thread as thread cleanup routine.

Reproduce code:
---------------
Run apache2 in a worker thread model with the following settings
StartThreads 3
MaxSpareThreads 4
MaxThreads 10

write a simple test.php
<?php
phpinfo();
?>
Run a load test with ab 
ab -n 40 -c 10
Wait for 5 minutes
ab -n 40 -c 10
Wait for 5 minutes
ab -n 40 -c 10
Wait for 5 minutes
ab -n 40 -c 10


Expected result:
----------------
In the case of setting given above after the load test of 10 simultaneous requests,
Apache kills 6 threads (As MaxThreads-MaxSpareThreads = 10-4 = 6).
The thread_resources momory associated with these 6 threads still not freed as soon as the corresponding thread is killed.
These kind of spurt loads at regular intervals causes lots of such thread_resources memory to get accumulated not being used for any real purpose.

Only when the sapi module comes down it calls tsrm_shutdown then these thread_resources are getting freed.


Actual result:
--------------
thread_resources of a thread that gets killed should get freed as soon as it dies rather than waiting till the tsrm_shutdown

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-11-08 09:50 UTC] dmitry@php.net
I am not able to reproduce this. Seems like a mess in report. Apache2 worker MPM doesn't support StartThreads and MaxThreads directives.
 [2005-11-10 18:36 UTC] sniper@php.net
Yes, but did you check out the patch..?

 [2005-12-01 15:51 UTC] dmitry@php.net
I confirm that PHP doesn't call ts_free_thread() if Web Server (except IIS) kills some thread. As a result all thread resources are not freed before server shutdown.

The proposed patch is invalid and even cannot be compiled on Linux.
 [2005-12-05 09:03 UTC] kameshj at fastmail dot fm
Yes you are correct that only IIS sapi is making use of ts_free_thread. But If you look at my patch you can see that I am registering this ts_free_thread function to pthread_push_cleanup to make sure the pthread subsystem to do the cleanup for us as soon as the thread is getting killed not to wait for the main thread to free the full thread_resources.
 [2008-08-12 16:54 UTC] jani@php.net
The patch is no longer available..?
 [2008-08-13 12:56 UTC] kameshj at fastmail dot fm
It is 2.75 years since I touched PHP code

Anyways from my backup, I copied the patch to.
http://pastebin.ca/1169665
 [2017-10-24 07:50 UTC] kalle@php.net
-Status: Assigned +Status: Open -Assigned To: dmitry +Assigned To:
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 12:01:30 2024 UTC