You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@apr.apache.org by Sander Striker <s....@striker.nl> on 2008/07/08 12:15:20 UTC

apr_pool_create_core_ex, WAS: Re: svn commit: r647384 - in /apr/apr/trunk: CHANGES include/apr_pools.h memory/unix/apr_pools.c

On Mon, Jul 7, 2008 at 8:07 AM, Mladen Turk <mt...@apache.org> wrote:
> Sander Striker wrote:
[...]
>> I checked on the archives, as I stumbled over the code and wondered
>> what the heck it was for.  I don't seem to find a response as to what
>> this functionality is actually needed for.  In case I missed it, could you
>> point me to it?  If I didn't miss it, because there is nothing there,
>> could
>> you please follow up to Joe's earlier question?
>>
>
> Sorry for the late answer. I was on the vacation ;)

Vacation goodness; can't argue with that.

> I think I already explained the purpose and usage of create_core_ex.

Like I said, I couldn't find it.  But thanks for taking the time to do so again.

> Unlike traditional pool it creates standalone pool (and allocator if
> provided at creation time).

If *not* provided at creation time.  Why not use the global allocator
in that case?  If the user really needs a separate allocator, it's two
extra lines.  The allocator declaration and a call to
apr_allocator_create, which makes it explicit.

> The usage addresses two main common concepts:
> 1. Temporary memory pools that are commonly used inside loops
>   and are used as create/alloc*n/destroy
>   Since create_core_ex doesn't lock the parent (or global) pool
>   during create/destroy this is performance upgrade.

Well, the idea would be to have the create and destroy outside the
loop, and alloc*n within.  In constructs with nested loops, throw in
an apr_pool_clear.

> 2. Using pools with high level languages like Java that have
>   strict garbage collection. APR Objects in that case can be
>   directly bound to the java object without the need to sync the
>   GC with the pool maintenance. Since those languages ensure that
>   the object destroy method will be called, but in random order
>   there is no need to create any back reference to parent pool.

But it doesn't hurt, apart from locking.

> Just to give you an overview of the problems with that in heavily
> multithreaded environment like JVM where GC and occur in parallel,
> are the long JNI calls in which case the apr_terminate needs to be
> protected by the sync method so that it gets called *only* when all
> functions exits. In any other case we might end up with zombie
> memory (child pool) in the middle of the function call.

apr_terminate should always be ensured to be called last.  Orphaning
pools may work around your immediate problems, today, but for any
operation post apr_terminate() all bets are off.  It could well break
in the future.

> We have that problem in Tomcat Native, so a simple high CPU usage
> can lead to a JVM core during connector shutdown/restart. JVM can
> decide to cleanup the our apr socket object at some later time when
> the parent was already destroyed. To solve this issue we would need
> to create internal tree that would map apr_pool parent/child dependency
> 1:1, protect the access and maintain it, as well as having an atomic counter
> of number of JNI functions currently executing.
> With detached pools we don't need that and the memory will still
> be released by calling pool_destroy in objects finalize method.

At the cost of having an allocator per pool.

> This breaks the single root pool presumption, so the API has to be used
> only in well defined environments where for each create_core_ex the
> corresponding destroy will be always called.

The thing that throws me is the choice of naming.  'orphaned' and
'unmanaged', would have been more descriptive than 'core'.

At the very least the docstring should be updated with a big fat
warning saying not to use it unless you know what you are doing.  And
maybe mention the typical use case of being in an environment with GC.
 Right now, there is nothing in the docstring to protect an
unsuspected user that thinks 'core' is a cool term and a better (more
convenient) API.

Cheers,

Sander

Re: apr_pool_create_core_ex, WAS: Re: svn commit: r647384 - in /apr/apr/trunk: CHANGES include/apr_pools.h memory/unix/apr_pools.c

Posted by Mladen Turk <mt...@apache.org>.
Sander Striker wrote:
> On Tue, Jul 8, 2008 at 3:26 PM, William A. Rowe, Jr.
> <wr...@rowe-clan.net> wrote:
>> Mladen Turk wrote:
>>> Again, the global allocator might get destroyed before the
>>> child pool since for it the owner is global pool.
>> Interesting observation, hinging on being a bug itself.
> 
> All pools are guaranteed to live as long as the global pool, at least
> under the previous implementation.  That ensures that the global
> allocator lives as long as any pool.  The global pool will destroy it
> once it goes away on apr_pool_terminate(), which is called by
> apr_terminate().

Of course. I wasn't talking about that.
Everything about that is clear and it works like it should.

However in some use cases this behavior requires syncing the
pool and use case object model making things very complex.
So it's just one of the pool usages where the user is responsible
for maintaining the pool's life cycle, and that's it.


Regards
-- 
^(TM)

Re: apr_pool_create_core_ex, WAS: Re: svn commit: r647384 - in /apr/apr/trunk: CHANGES include/apr_pools.h memory/unix/apr_pools.c

Posted by Sander Striker <s....@striker.nl>.
On Tue, Jul 8, 2008 at 3:26 PM, William A. Rowe, Jr.
<wr...@rowe-clan.net> wrote:
> Mladen Turk wrote:
>>
>> Again, the global allocator might get destroyed before the
>> child pool since for it the owner is global pool.
>
> Interesting observation, hinging on being a bug itself.

All pools are guaranteed to live as long as the global pool, at least
under the previous implementation.  That ensures that the global
allocator lives as long as any pool.  The global pool will destroy it
once it goes away on apr_pool_terminate(), which is called by
apr_terminate().

>> Making sure that apr_terminate is called as last makes the need
>> for tracking the child pools, and it breaks the OO language paradigm.
>> The point is that one can call apr_terminate at some exit point, but
>> that would require to wait for all objects to detach itself, and make
>> event mechanism for breaking the long running functions (like file or
>> network).
>
> Nope.  As in most object paradigms, init/terminate is reference counted
> and final destruction is held for refcount reaching zero.

Indeed.  After the last apr_terminate(), behavior is undefined.

Cheers,

Sander

Re: apr_pool_create_core_ex, WAS: Re: svn commit: r647384 - in /apr/apr/trunk: CHANGES include/apr_pools.h memory/unix/apr_pools.c

Posted by "William A. Rowe, Jr." <wr...@rowe-clan.net>.
Mladen Turk wrote:
> 
> Again, the global allocator might get destroyed before the
> child pool since for it the owner is global pool.

Interesting observation, hinging on being a bug itself.

> Making sure that apr_terminate is called as last makes the need
> for tracking the child pools, and it breaks the OO language paradigm.
> The point is that one can call apr_terminate at some exit point, but
> that would require to wait for all objects to detach itself, and make
> event mechanism for breaking the long running functions (like file or
> network).

Nope.  As in most object paradigms, init/terminate is reference counted
and final destruction is held for refcount reaching zero.

Re: apr_pool_create_core_ex, WAS: Re: svn commit: r647384 - in /apr/apr/trunk: CHANGES include/apr_pools.h memory/unix/apr_pools.c

Posted by Mladen Turk <mt...@apache.org>.
Sander Striker wrote:
> 
>> Unlike traditional pool it creates standalone pool (and allocator if
>> provided at creation time).
> 
> If *not* provided at creation time.  Why not use the global allocator
> in that case?  If the user really needs a separate allocator, it's two
> extra lines.  The allocator declaration and a call to
> apr_allocator_create, which makes it explicit.
>

Again, the global allocator might get destroyed before the
child pool since for it the owner is global pool.

Making sure that apr_terminate is called as last makes the need
for tracking the child pools, and it breaks the OO language paradigm.
The point is that one can call apr_terminate at some exit point, but
that would require to wait for all objects to detach itself, and make
event mechanism for breaking the long running functions (like file or
network).

> 
>> This breaks the single root pool presumption, so the API has to be used
>> only in well defined environments where for each create_core_ex the
>> corresponding destroy will be always called.
> 
> The thing that throws me is the choice of naming.  'orphaned' and
> 'unmanaged', would have been more descriptive than 'core'.
>

Agreed, the core is not very well chosen name.
For trunk it should be easy, but will there be any problems
if renamed in 1.3 as well?

And of course, I'll make some capital letter sentences :)


Regards
-- 
^(TM)

Re: apr_pool_create_core_ex, WAS: Re: svn commit: r647384 - in /apr/apr/trunk: CHANGES include/apr_pools.h memory/unix/apr_pools.c

Posted by Sander Striker <s....@striker.nl>.
On Tue, Jul 8, 2008 at 5:24 PM, Mladen Turk <mt...@apache.org> wrote:
> William A. Rowe, Jr. wrote:
>>
>> Mladen Turk wrote:
>>>
>>> Any problems renaming the API in 1.3.x as well?
>>
>> Yup, it shipped.  So we can @deprecate your original choice of name
>> and add any new name that's appropriate.
>>
>
> Does depreciation means the function must be implemented?

Yes.

> I suppose it should, so it'll simply call the renamed one.

Correct.

> What a mess ?!# :(

Cheers,

Sander

Re: apr_pool_create_core_ex, WAS: Re: svn commit: r647384 - in /apr/apr/trunk: CHANGES include/apr_pools.h memory/unix/apr_pools.c

Posted by Bojan Smojver <bo...@rexursive.com>.
On Wed, 2008-07-09 at 08:51 +0200, Mladen Turk wrote:

> Hmm, trunk should be development version, so the API is not fixed
> unlike once when branched.

In which case we'll have to remember to put that back in if we ever
decide to branch 1.4.0 from the trunk. What's the harm in keeping it for
now?

-- 
Bojan


Re: apr_pool_create_core_ex, WAS: Re: svn commit: r647384 - in /apr/apr/trunk: CHANGES include/apr_pools.h memory/unix/apr_pools.c

Posted by Sander Striker <s....@striker.nl>.
On Wed, Jul 9, 2008 at 9:21 AM, Mladen Turk <mt...@apache.org> wrote:
[...]
> OK, clear enough.
> I'll rename and update the docs both in trunk and 1.3

Thx,

Sander

Re: apr_pool_create_core_ex, WAS: Re: svn commit: r647384 - in /apr/apr/trunk: CHANGES include/apr_pools.h memory/unix/apr_pools.c

Posted by Mladen Turk <mt...@apache.org>.
Sander Striker wrote:
> On Wed, Jul 9, 2008 at 8:51 AM, Mladen Turk <mt...@apache.org> wrote:
>> Bojan Smojver wrote:
>>> On Wed, 2008-07-09 at 07:27 +0200, Mladen Turk wrote:
>>>
>>>> What about trunk? Do we need depreciation there as well.
>>>> I suppose not, cause it wasn't released.
>>> Trunk is currently marked 1.4.0, so I guess yes - you should have
>>> deprecated function there too. If/when trunk officially goes 2.0, then
>>> we can get rid of the deprecated function.
>>>
>> Hmm, trunk should be development version, so the API is not fixed
>> unlike once when branched.
> 
> Bojan is right.  Once trunk becomes 2.x.y, then the deprecated
> functions can be removed.  Until then, they need to remain.
> 

OK, clear enough.
I'll rename and update the docs both in trunk and 1.3

Regards
-- 
^(TM)

Re: apr_pool_create_core_ex, WAS: Re: svn commit: r647384 - in /apr/apr/trunk: CHANGES include/apr_pools.h memory/unix/apr_pools.c

Posted by Sander Striker <s....@striker.nl>.
On Wed, Jul 9, 2008 at 8:51 AM, Mladen Turk <mt...@apache.org> wrote:
> Bojan Smojver wrote:
>>
>> On Wed, 2008-07-09 at 07:27 +0200, Mladen Turk wrote:
>>
>>> What about trunk? Do we need depreciation there as well.
>>> I suppose not, cause it wasn't released.
>>
>> Trunk is currently marked 1.4.0, so I guess yes - you should have
>> deprecated function there too. If/when trunk officially goes 2.0, then
>> we can get rid of the deprecated function.
>>
>
> Hmm, trunk should be development version, so the API is not fixed
> unlike once when branched.

Bojan is right.  Once trunk becomes 2.x.y, then the deprecated
functions can be removed.  Until then, they need to remain.

Cheers,

Sander

Re: apr_pool_create_core_ex, WAS: Re: svn commit: r647384 - in /apr/apr/trunk: CHANGES include/apr_pools.h memory/unix/apr_pools.c

Posted by Mladen Turk <mt...@apache.org>.
Bojan Smojver wrote:
> On Wed, 2008-07-09 at 07:27 +0200, Mladen Turk wrote:
> 
>> What about trunk? Do we need depreciation there as well.
>> I suppose not, cause it wasn't released.
> 
> Trunk is currently marked 1.4.0, so I guess yes - you should have
> deprecated function there too. If/when trunk officially goes 2.0, then
> we can get rid of the deprecated function.
> 

Hmm, trunk should be development version, so the API is not fixed
unlike once when branched.

Regards
-- 
^(TM)

Re: apr_pool_create_core_ex, WAS: Re: svn commit: r647384 - in /apr/apr/trunk: CHANGES include/apr_pools.h memory/unix/apr_pools.c

Posted by Bojan Smojver <bo...@rexursive.com>.
On Wed, 2008-07-09 at 07:27 +0200, Mladen Turk wrote:

> What about trunk? Do we need depreciation there as well.
> I suppose not, cause it wasn't released.

Trunk is currently marked 1.4.0, so I guess yes - you should have
deprecated function there too. If/when trunk officially goes 2.0, then
we can get rid of the deprecated function.

-- 
Bojan


Re: Could this be a bug in file_io\win32\dir.c ?

Posted by "William A. Rowe, Jr." <wr...@rowe-clan.net>.
Friedrich Dominicus wrote:
> #if APR_HAS_ANSI_FS
>     ELSE_WIN_OS_IS_ANSI
>     {
>         /* This code path is always be invoked by apr_dir_open or 
>          * apr_dir_rewind, so return without filling out the finfo.
>          */
>         if (thedir->dirhand == INVALID_HANDLE_VALUE) {
>             /* '/' terminated, so add the '*' and pop it when we finish */
>             char *eop = strchr(thedir->dirname, '\0');
>             eop[0] = '*';
>             eop[1] = '\0';
>             thedir->dirhand = FindFirstFileA(thedir->dirname, 
>                                              thedir->n.entry);
>             eop[0] = '\0';
>             if (thedir->dirhand == INVALID_HANDLE_VALUE) {
>                 return apr_get_os_error();
>             }
>             thedir->bof = 1;
>             return APR_SUCCESS;
>         }
>         else if (thedir->bof) {
>             /* Noop - we already called FindFirstFileW from
>              * either apr_dir_open or apr_dir_rewind ... use
>              * that first record.
>              */
>             thedir->bof = 0; 
>         }
>         else if (!FindNextFile(thedir->dirhand, thedir->n.entry)) {
>             return apr_get_os_error();
>         }
>         while (thedir->rootlen &&
>                thedir->rootlen + strlen(thedir->n.entry->cFileName) >= MAX_PATH)
>         {
>             if (!FindNextFileW(thedir->dirhand, thedir->w.entry)) {
>                                                         ^ shouldn't
> that be n?

Not enough, does the attached patch work for you?



Could this be a bug in file_io\win32\dir.c ?

Posted by Friedrich Dominicus <fr...@q-software-solutions.de>.
#if APR_HAS_ANSI_FS
    ELSE_WIN_OS_IS_ANSI
    {
        /* This code path is always be invoked by apr_dir_open or 
         * apr_dir_rewind, so return without filling out the finfo.
         */
        if (thedir->dirhand == INVALID_HANDLE_VALUE) {
            /* '/' terminated, so add the '*' and pop it when we finish */
            char *eop = strchr(thedir->dirname, '\0');
            eop[0] = '*';
            eop[1] = '\0';
            thedir->dirhand = FindFirstFileA(thedir->dirname, 
                                             thedir->n.entry);
            eop[0] = '\0';
            if (thedir->dirhand == INVALID_HANDLE_VALUE) {
                return apr_get_os_error();
            }
            thedir->bof = 1;
            return APR_SUCCESS;
        }
        else if (thedir->bof) {
            /* Noop - we already called FindFirstFileW from
             * either apr_dir_open or apr_dir_rewind ... use
             * that first record.
             */
            thedir->bof = 0; 
        }
        else if (!FindNextFile(thedir->dirhand, thedir->n.entry)) {
            return apr_get_os_error();
        }
        while (thedir->rootlen &&
               thedir->rootlen + strlen(thedir->n.entry->cFileName) >= MAX_PATH)
        {
            if (!FindNextFileW(thedir->dirhand, thedir->w.entry)) {
                                                        ^ shouldn't
that be n?
                return apr_get_os_error();


Regards
Friedrich

Re: apr_pool_create_core_ex, WAS: Re: svn commit: r647384 - in /apr/apr/trunk: CHANGES include/apr_pools.h memory/unix/apr_pools.c

Posted by Mladen Turk <mt...@apache.org>.
William A. Rowe, Jr. wrote:
> Mladen Turk wrote:
>> William A. Rowe, Jr. wrote:
>>> Mladen Turk wrote:
>>>>
>>>> Any problems renaming the API in 1.3.x as well?
>>>
>>> Yup, it shipped.  So we can @deprecate your original choice of name
>>> and add any new name that's appropriate.
>>>
>>
>> Does depreciation means the function must be implemented?
>> I suppose it should, so it'll simply call the renamed one.
> 
> Call the well-named function from the deprecated one, yes.  It's the
> pattern that is repeated throughout the API.  When the deprecated fn
> disappears in 2.0, it has no impact on the legitimate one.
> 

What about trunk? Do we need depreciation there as well.
I suppose not, cause it wasn't released.

Regards
-- 
^(TM)

Re: apr_pool_create_core_ex, WAS: Re: svn commit: r647384 - in /apr/apr/trunk: CHANGES include/apr_pools.h memory/unix/apr_pools.c

Posted by "William A. Rowe, Jr." <wr...@rowe-clan.net>.
Mladen Turk wrote:
> William A. Rowe, Jr. wrote:
>> Mladen Turk wrote:
>>>
>>> Any problems renaming the API in 1.3.x as well?
>>
>> Yup, it shipped.  So we can @deprecate your original choice of name
>> and add any new name that's appropriate.
>>
> 
> Does depreciation means the function must be implemented?
> I suppose it should, so it'll simply call the renamed one.

Call the well-named function from the deprecated one, yes.  It's the
pattern that is repeated throughout the API.  When the deprecated fn
disappears in 2.0, it has no impact on the legitimate one.

> What a mess ?!# :(

That's called strict versioning, and that's why I *begged* for careful
review before 1.3.0 was released.

Re: apr_pool_create_core_ex, WAS: Re: svn commit: r647384 - in /apr/apr/trunk: CHANGES include/apr_pools.h memory/unix/apr_pools.c

Posted by Mladen Turk <mt...@apache.org>.
William A. Rowe, Jr. wrote:
> Mladen Turk wrote:
>>
>> Any problems renaming the API in 1.3.x as well?
> 
> Yup, it shipped.  So we can @deprecate your original choice of name
> and add any new name that's appropriate.
>

Does depreciation means the function must be implemented?
I suppose it should, so it'll simply call the renamed one.

What a mess ?!# :(

Regards
-- 
^(TM)

Re: apr_pool_create_core_ex, WAS: Re: svn commit: r647384 - in /apr/apr/trunk: CHANGES include/apr_pools.h memory/unix/apr_pools.c

Posted by "William A. Rowe, Jr." <wr...@rowe-clan.net>.
Mladen Turk wrote:
> 
> Any problems renaming the API in 1.3.x as well?

Yup, it shipped.  So we can @deprecate your original choice of name
and add any new name that's appropriate.

Re: apr_pool_create_core_ex, WAS: Re: svn commit: r647384 - in /apr/apr/trunk: CHANGES include/apr_pools.h memory/unix/apr_pools.c

Posted by Mladen Turk <mt...@apache.org>.
William A. Rowe, Jr. wrote:
> Sander Striker wrote:
>>
>> At the very least the docstring should be updated with a big fat
>> warning saying not to use it unless you know what you are doing.  And
>> maybe mention the typical use case of being in an environment with GC.
>>  Right now, there is nothing in the docstring to protect an
>> unsuspected user that thinks 'core' is a cool term and a better (more
>> convenient) API.
> 
> ++1; especially note that apr-util objects such as apr_ldap, apr_dbd etc
> may not be compatible with such a detached pool.
> 

OK, so the core->unmanaged should be fine I suppose like
Sander suggested?

Any problems renaming the API in 1.3.x as well?

Regards
-- 
^(TM)

Re: apr_pool_create_core_ex, WAS: Re: svn commit: r647384 - in /apr/apr/trunk: CHANGES include/apr_pools.h memory/unix/apr_pools.c

Posted by "William A. Rowe, Jr." <wr...@rowe-clan.net>.
Sander Striker wrote:
> 
> At the very least the docstring should be updated with a big fat
> warning saying not to use it unless you know what you are doing.  And
> maybe mention the typical use case of being in an environment with GC.
>  Right now, there is nothing in the docstring to protect an
> unsuspected user that thinks 'core' is a cool term and a better (more
> convenient) API.

++1; especially note that apr-util objects such as apr_ldap, apr_dbd etc
may not be compatible with such a detached pool.