You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@apr.apache.org by Jeff Trawick <tr...@gmail.com> on 2006/09/07 17:22:35 UTC
API to prevent further allocations from a pool?
Occasionally I see problems caused by Apache modules using per-process
pools on a request processing thread. Aside from the thread-safety
issues, they had no business doing that anyway.
It would be cool for the MPM to be able to lock the pool once it is
ready to start serving requests so that if any errant module tries to
use pconf, pchild, etc. it will crash immediately instead of
introducing memory corruption under load. (Even if the module author
is "clever" and uses a mutex around the pool use, that assumes she is
the only person in the world to have that idea.)
Some other multi-threaded APR apps probably have the same concern,
especially if they allow plug-ins from third parties to be loaded.
apr_pool_lock() - cause abort() on subsequent allocation request
apr_pool_unlock() - allow it to be used again
Re: API to prevent further allocations from a pool?
Posted by "William A. Rowe, Jr." <wr...@rowe-clan.net>.
Jeff Trawick wrote:
> Occasionally I see problems caused by Apache modules using per-process
> pools on a request processing thread. Aside from the thread-safety
> issues, they had no business doing that anyway.
>
> It would be cool for the MPM to be able to lock the pool once it is
> ready to start serving requests so that if any errant module tries to
> use pconf, pchild, etc. it will crash immediately instead of
> introducing memory corruption under load. (Even if the module author
> is "clever" and uses a mutex around the pool use, that assumes she is
> the only person in the world to have that idea.)
+1 - this existed until the pool/allocator was refactored, and didn't
survive both iterations. I'm sure the code that was trashed (entirely
demotivating me from writing it -again-) is still in history, it's not
all that difficult. The win32 code was pretty trivial, the unix code
relied on locking an mmap which seems silly in this day and age.
There is still a call in httpd main.c...
apr_pool_lock(pconf, 1);
ap_run_optional_fn_retrieve();
if (ap_mpm_run(pconf, plog, server_conf))
break;
apr_pool_lock(pconf, 0);
just waiting for at least some platforms to support apr_pool_lock.
Bill
Re: API to prevent further allocations from a pool?
Posted by Jeff Trawick <tr...@gmail.com>.
On 9/8/06, Joe Orton <jo...@redhat.com> wrote:
> On Fri, Sep 08, 2006 at 08:09:35AM -0400, Jeff Trawick wrote:
> > On 9/8/06, Joe Orton <jo...@redhat.com> wrote:
> >
> > >But to make a strong API guarantee I think this would need to use atomic
> > >operations anyway, which means the performance hit (mutexes on some
> > >platforms) would indeed be way too high for the pool allocation critical
> > >path, I think.
> >
> > I don't see the need for thread-safety here, though perhaps we're
> > thinking of two different things. (apr_pool_freeze() may be a better
> > name for what I'm thinking of.)
>
> That makes sense; those names are definitely good too. But the question
> remains on why this needs to go in the "production" code not just the
> debug build. This is certainly a more intrusive feature than _join,
> which has been happily consigned to the _DEBUG build forever. (for some
> definition of forever which includes its legacy in 1.3 anyway ;)
In my case, I see memory corruption problems only with somebody else's
production environment, which often has third-party code loaded which
I don't have access to, and as long as there is no noticeable
performance hit I'd like to be able to rule out certain common
blunders as the cause of the corruption.
Even with all of your own code, aliasing of pool pointer names within
the application and code paths that only execute under rare
configurations may make it impractical to find all such misuses by
inspection.
If there is a noticeable performance hit, then I agree that it
shouldn't be activated in the default build. But lumping it into the
existing pool debug settings may be too drastic.
Re: API to prevent further allocations from a pool?
Posted by Joe Orton <jo...@redhat.com>.
On Fri, Sep 08, 2006 at 08:09:35AM -0400, Jeff Trawick wrote:
> On 9/8/06, Joe Orton <jo...@redhat.com> wrote:
>
> >But to make a strong API guarantee I think this would need to use atomic
> >operations anyway, which means the performance hit (mutexes on some
> >platforms) would indeed be way too high for the pool allocation critical
> >path, I think.
>
> I don't see the need for thread-safety here, though perhaps we're
> thinking of two different things. (apr_pool_freeze() may be a better
> name for what I'm thinking of.)
That makes sense; those names are definitely good too. But the question
remains on why this needs to go in the "production" code not just the
debug build. This is certainly a more intrusive feature than _join,
which has been happily consigned to the _DEBUG build forever. (for some
definition of forever which includes its legacy in 1.3 anyway ;)
Regards,
joe
Re: API to prevent further allocations from a pool?
Posted by Jeff Trawick <tr...@gmail.com>.
On 9/8/06, Joe Orton <jo...@redhat.com> wrote:
> But to make a strong API guarantee I think this would need to use atomic
> operations anyway, which means the performance hit (mutexes on some
> platforms) would indeed be way too high for the pool allocation critical
> path, I think.
I don't see the need for thread-safety here, though perhaps we're
thinking of two different things. (apr_pool_freeze() may be a better
name for what I'm thinking of.)
The freeze operation doesn't have to be thread-safe because pools
themselves aren't thread-safe. (If there are multiple threads
modifying the pool before freezing the app is already broken.)
The unfreeze operation doesn't have to be thread-safe because an
unfrozen pool isn't thread-safe (i.e., if multiple threads are
modifying the pool after unfreezing then the app is already broken),
and by unfreezing the pool in a multithreaded environment the app
loses the guarantee possible with a frozen pool.
Re: API to prevent further allocations from a pool?
Posted by Joe Orton <jo...@redhat.com>.
On Thu, Sep 07, 2006 at 12:39:57PM -0400, Jeff Trawick wrote:
> On 9/7/06, Joe Orton <jo...@redhat.com> wrote:
> >On Thu, Sep 07, 2006 at 11:22:35AM -0400, Jeff Trawick wrote:
> >> It would be cool for the MPM to be able to lock the pool once it is
> >> ready to start serving requests so that if any errant module tries to
> >> use pconf, pchild, etc. it will crash immediately instead of
> >> introducing memory corruption under load. (Even if the module author
> >> is "clever" and uses a mutex around the pool use, that assumes she is
> >> the only person in the world to have that idea.)
> >
> >If done "#if APR_POOL_DEBUG", I think this would be useful.
>
> People who don't know pools don't know APR_POOL_DEBUG either ;)
*You* can still run the suspect modules through a stress test on a
debug-enabled server build, right?
> Is it just a performance concern which leads you to APR_POOL_DEBUG?
Simply that this is a debugging aid and as such it seems sufficient to
add it to the debug code and leave the production code alone.
But to make a strong API guarantee I think this would need to use atomic
operations anyway, which means the performance hit (mutexes on some
platforms) would indeed be way too high for the pool allocation critical
path, I think.
Regards,
joe
Re: API to prevent further allocations from a pool?
Posted by Jeff Trawick <tr...@gmail.com>.
On 9/7/06, Joe Orton <jo...@redhat.com> wrote:
> On Thu, Sep 07, 2006 at 11:22:35AM -0400, Jeff Trawick wrote:
> > It would be cool for the MPM to be able to lock the pool once it is
> > ready to start serving requests so that if any errant module tries to
> > use pconf, pchild, etc. it will crash immediately instead of
> > introducing memory corruption under load. (Even if the module author
> > is "clever" and uses a mutex around the pool use, that assumes she is
> > the only person in the world to have that idea.)
>
> If done "#if APR_POOL_DEBUG", I think this would be useful.
People who don't know pools don't know APR_POOL_DEBUG either ;)
Is it just a performance concern which leads you to APR_POOL_DEBUG?
> Would need
> to be clear about whether other ways of manipulating the pool (userdata,
> cleanups, clear, destroy?) are covered or really just apr_p*alloc.
Userdata and cleanups are allocations and pool-internal data structure
manipulations, so they'd have to be disallowed to allow locking to
guarantee for some application that use of a certain pool has no
thread-safety issues or no initialization vs. steady-state issues
(e.g., memory leak).
Clear and destroy likely already result in sufficient fireworks if
they are misused, so those aren't so important to disallow.
> > apr_pool_lock() - cause abort() on subsequent allocation request
>
> [un-implemented, un-documented apr_pool_lock() already in-tree!]
(blush)
Re: API to prevent further allocations from a pool?
Posted by "William A. Rowe, Jr." <wr...@rowe-clan.net>.
Joe Orton wrote:
> On Thu, Sep 07, 2006 at 11:22:35AM -0400, Jeff Trawick wrote:
>> It would be cool for the MPM to be able to lock the pool once it is
>> ready to start serving requests so that if any errant module tries to
>> use pconf, pchild, etc. it will crash immediately instead of
>> introducing memory corruption under load. (Even if the module author
>> is "clever" and uses a mutex around the pool use, that assumes she is
>> the only person in the world to have that idea.)
>
> If done "#if APR_POOL_DEBUG", I think this would be useful. Would need
> to be clear about whether other ways of manipulating the pool (userdata,
> cleanups, clear, destroy?) are covered or really just apr_p*alloc.
Well... some additional pools could (at some significant cost) be stressed
out under APR_POOL_DEBUG, frequently unlocking and relocking them.
But as far as pconf which should be a static for the lifetime of the server
(and therefore not frequently unlocked/relocked) the code makes sense in
production, as well.
apr_pool_[un]lock absolutely should not be conditioned on APR_POOL_DEBUG.
Leave it to the user to use them explicitly or conditionally, as best
suits their purposes.
Re: API to prevent further allocations from a pool?
Posted by Joe Orton <jo...@redhat.com>.
On Thu, Sep 07, 2006 at 11:22:35AM -0400, Jeff Trawick wrote:
> It would be cool for the MPM to be able to lock the pool once it is
> ready to start serving requests so that if any errant module tries to
> use pconf, pchild, etc. it will crash immediately instead of
> introducing memory corruption under load. (Even if the module author
> is "clever" and uses a mutex around the pool use, that assumes she is
> the only person in the world to have that idea.)
If done "#if APR_POOL_DEBUG", I think this would be useful. Would need
to be clear about whether other ways of manipulating the pool (userdata,
cleanups, clear, destroy?) are covered or really just apr_p*alloc.
> Some other multi-threaded APR apps probably have the same concern,
> especially if they allow plug-ins from third parties to be loaded.
>
> apr_pool_lock() - cause abort() on subsequent allocation request
[un-implemented, un-documented apr_pool_lock() already in-tree!]
joe
Re: API to prevent further allocations from a pool?
Posted by Jeff Trawick <tr...@gmail.com>.
On 9/7/06, Nick Kew <ni...@webthing.com> wrote:
> On Thursday 07 September 2006 16:22, Jeff Trawick wrote:
> > It would be cool for the MPM to be able to lock the pool once it is
> > ready to start serving requests so that if any errant module tries to
> > use pconf, pchild, etc. it will crash immediately instead of
> > introducing memory corruption under load. (Even if the module author
> > is "clever" and uses a mutex around the pool use, that assumes she is
> > the only person in the world to have that idea.)
>
> There are cases where using pchild can be the lesser evil.
> Example: managing a persistent resource that supports infrequent
> delete or timeout. If a module creates a private pool at child_init,
> that'll make a slow leak.
allocating from pchild is also a slow leak
allocating from pchild is not threadsafe unless you mutex around your
alloc calls AND no other module in the server had the same idea
a private pool is the only solution short of avoiding pools altogether
Re: API to prevent further allocations from a pool?
Posted by Nick Kew <ni...@webthing.com>.
On Thursday 07 September 2006 16:22, Jeff Trawick wrote:
> Occasionally I see problems caused by Apache modules using per-process
> pools on a request processing thread. Aside from the thread-safety
> issues, they had no business doing that anyway.
>
> It would be cool for the MPM to be able to lock the pool once it is
> ready to start serving requests so that if any errant module tries to
> use pconf, pchild, etc. it will crash immediately instead of
> introducing memory corruption under load. (Even if the module author
> is "clever" and uses a mutex around the pool use, that assumes she is
> the only person in the world to have that idea.)
There are cases where using pchild can be the lesser evil.
Example: managing a persistent resource that supports infrequent
delete or timeout. If a module creates a private pool at child_init,
that'll make a slow leak. A workaround hack for that is to add a
timer or counter, and periodically tear down the module's pool and
renew it by allocating a new one from pchild.
> Some other multi-threaded APR apps probably have the same concern,
> especially if they allow plug-ins from third parties to be loaded.
>
> apr_pool_lock() - cause abort() on subsequent allocation request
> apr_pool_unlock() - allow it to be used again
Another thought (separate from, not instead of, yours): thread-safe
pools? A pool with a thread mutex attached, as a builtin option?
That would be ideal for modules doing the kind of thing I suggested.
--
Nick Kew
Application Development with Apache - the Apache Modules Book
http://www.prenhallprofessional.com/title/0132409674
Re: API to prevent further allocations from a pool?
Posted by Mladen Turk <mt...@apache.org>.
Jeff Trawick wrote:
> Occasionally I see problems caused by Apache modules using per-process
> pools on a request processing thread. Aside from the thread-safety
> issues, they had no business doing that anyway.
>
> apr_pool_lock() - cause abort() on subsequent allocation request
> apr_pool_unlock() - allow it to be used again
>
Although I'm +1 on the subject, the real thing would be
to provide the pool as part of module loading and modle API,
(or two of them) with the same lifetime as pconf, pchild.
Think this would be more safe then forcing users to rewrite
all their modules.
Regards,
Mladen.