php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53430 ts_free_thread() frees only temporary in multiple parallel threads
Submitted: 2010-11-30 20:39 UTC Modified: 2010-11-30 21:20 UTC
From: 128bitencrypted at gmail dot com Assigned:
Status: Not a bug Package: Other web server
PHP Version: Irrelevant OS: Windows
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: 128bitencrypted at gmail dot com
New email:
PHP Version: OS:

 

 [2010-11-30 20:39 UTC] 128bitencrypted at gmail dot com
Description:
------------
I created my own php sapi 1 year ago and it was only single threaded.
Now I tried to make it multi-threaded by using TSRM.

I got 1 thread starting up and shutting down the TSRM and my module and many other threads executing requests. You can take a look at this abstract example here:

DWORD WINAPI test_thread(void*)
{
    TSRMLS_FETCH() // a lot of memory is allocated here
    
    // execute the request (just core functions written here)
    php_request_startup(TSRMLS_C);
    php_execute_script(fh, TSRMLS_C);
    php_request_shutdown(TSRMLS_C);
	
    ts_free_thread(); // I tried to free the memory allocated through TSRMLS_FETCH() here
}

int main()
{
    tsrm_startup(1, 1, 0, NULL);
    sapi_startup(&my_php_sapi);
    php_module_startup(&my_php_sapi, NULL, 0);
    sapi_activate(TSRMLS_C);
	
    // start 25 threads running "test_thread()" here
	
    // call module/sapi shutdown functions
    tsrm_shutdown(); // all memory gets freed here
}

The request threads work fine but I noticed that all the copies allocated through TSRMLS_FETCH() last in the memory until tsrm_shutdown() gets executed. So I browsed the web and found that ts_free_thread() should solve it. But now here's the problem:
When I'm executing only 1 request thread at a time, everything works fine and ts_free_thread() frees all resources. But when I run multiple threads at the same time the memory gets up and down until all threads finished. But after all threads exited the memory grows up enormous.

This is what happens:
- start 25 threads                -- memory: ~25.000 k
- threads finish one by one       -- memory: ~25.000 k --> ~4.000 k
- all threads finished            -- memory: ~60.000 k

I was using php 5.2.9 and now tried the same with the latest stable 5.3.3 release but the problem still resists.

I couldn't really locate the problem yet.

Expected result:
----------------
memory stays at ~4.000 k when all threads finish

Actual result:
--------------
memory grows suddenly about more that 100 percent

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-11-30 21:20 UTC] johannes@php.net
-Status: Open +Status: Bogus
 [2010-11-30 21:20 UTC] johannes@php.net
This is no Bug but a request for support. Please ask on pecl-dev or maye the internals-win list.

A few remarks anyways: I don't know Windows threading models, in general you have to make sure that PHP and the environment use compatible threading models. PHP has some true global data which is shared across all threads. So having higher memory usage till PHP is shut down completely is expected.

In general TSRM isn't encouraged that much as it causes quite some overhead .. but as said: The bug tracker is no place to discuss your custom SAPIs.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 08:01:28 2024 UTC