You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@httpd.apache.org by je...@apache.org on 2011/05/15 13:05:36 UTC
svn commit: r1103315 - /httpd/httpd/trunk/modules/filters/mod_deflate.c
Author: jerenkrantz
Date: Sun May 15 11:05:36 2011
New Revision: 1103315
URL: http://svn.apache.org/viewvc?rev=1103315&view=rev
Log:
Fix memory leak in mod_deflate.c when SetOutputFilter is enabled and DEFLATE
is the first filter in the chain - this is seen with mod_dav + mod_deflate
when the client can not do gzip and we constantly do the filter checks.
* modules/filters/mod_deflate.c
(deflate_ctx): Add a flag as to whether we are successfully initialized.
(deflate_out_filter): Always create filter ctx and set filter_init flag only
after we are successfully initialized.
Modified:
httpd/httpd/trunk/modules/filters/mod_deflate.c
Modified: httpd/httpd/trunk/modules/filters/mod_deflate.c
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/modules/filters/mod_deflate.c?rev=1103315&r1=1103314&r2=1103315&view=diff
==============================================================================
--- httpd/httpd/trunk/modules/filters/mod_deflate.c (original)
+++ httpd/httpd/trunk/modules/filters/mod_deflate.c Sun May 15 11:05:36 2011
@@ -303,6 +303,7 @@ typedef struct deflate_ctx_t
unsigned char *validation_buffer;
apr_size_t validation_buffer_length;
int inflate_init;
+ int filter_init;
} deflate_ctx;
/* Number of validation bytes (CRC and length) after the compressed data */
@@ -445,6 +446,8 @@ static apr_status_t deflate_out_filter(a
char *token;
const char *encoding;
+ ctx = f->ctx = apr_pcalloc(r->pool, sizeof(*ctx));
+
/*
* Only work on main request, not subrequests,
* that are not a 204 response with no content
@@ -563,7 +566,6 @@ static apr_status_t deflate_out_filter(a
*/
if (r->status != HTTP_NOT_MODIFIED) {
- ctx = f->ctx = apr_pcalloc(r->pool, sizeof(*ctx));
ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
ctx->buffer = apr_palloc(r->pool, c->bufferSize);
ctx->libz_end_func = deflateEnd;
@@ -592,6 +594,11 @@ static apr_status_t deflate_out_filter(a
*/
apr_pool_cleanup_register(r->pool, ctx, deflate_ctx_cleanup,
apr_pool_cleanup_null);
+
+ /* Set the filter init flag so subsequent invocations know we are
+ * active.
+ */
+ ctx->filter_init = 1;
}
/*
@@ -629,6 +636,11 @@ static apr_status_t deflate_out_filter(a
/* initialize deflate output buffer */
ctx->stream.next_out = ctx->buffer;
ctx->stream.avail_out = c->bufferSize;
+ } else if (!ctx->filter_init) {
+ /* Hmm. We've run through the filter init before as we have a ctx,
+ * but we never initialized. We probably have a dangling ref. Bail.
+ */
+ return ap_pass_brigade(f->next, bb);
}
while (!APR_BRIGADE_EMPTY(bb))
RE: svn commit: r1103315 -
/httpd/httpd/trunk/modules/filters/mod_deflate.c
Posted by Yehezkel Horowitz <ho...@checkpoint.com>.
I think it is only problem with content filters (as mod_deflate filter), not with request filters.
Regards
Yehezkel
-----Original Message-----
From: Ruediger Pluem [mailto:rpluem@apache.org]
Sent: Tuesday, May 24, 2011 9:09 AM
To: dev@httpd.apache.org
Subject: Re: svn commit: r1103315 - /httpd/httpd/trunk/modules/filters/mod_deflate.c
On 05/23/2011 06:11 PM, Justin Erenkrantz wrote:
> On Mon, May 23, 2011 at 6:00 AM, Graham Leggett <mi...@sharp.fm> wrote:
>> On 23 May 2011, at 5:57 PM, Justin Erenkrantz wrote:
>>
>>> mod_dav uses r->output_filters - but, the pointer never gets updated
>>> when it is the first one in the chain. Hence, we call mod_deflate all
>>> the time even on a request that can't support it - so we have to avoid
>>> repeated memory allocations in that case. -- justin
>> This sounds like a general problem though, are other modules potentially
>> affected?
>
> Yes - this why I said in my earlier post that we'd likely need to
> re-architect filters to deal with it. -- justin
>
>
Indeed this sounds like a general problem, but
ap_remove_output_filter does adjust r->output_filters if f->r is set and the
filter is the first one in the chain.
Which responses exactly suffer from this behaviour?
Regards
Rüdiger
Re: svn commit: r1103315 - /httpd/httpd/trunk/modules/filters/mod_deflate.c
Posted by Ruediger Pluem <rp...@apache.org>.
On 05/27/2011 09:50 AM, Greg Stein wrote:
> On Wed, May 25, 2011 at 02:53, Ruediger Pluem <rp...@apache.org> wrote:
>> ...
>> Thanks. IMHO this is a design flaw in the DAV provider API in conjunction
>> with the current filter API:
>>
>> dav_error * (*deliver)(const dav_resource *resource,
>> ap_filter_t *output);
>>
>> deliver is usually called with r->output_filters as a second parameter and
>> thus makes a copy of the contents of r->output_filters and hence does not
>> notice if r->output_filters changes during the process of pushing content thru
>> the filter chain by multiple calls to ap_pass_brigade.
>
> Yup...
>
>> My idea would be to change the DAV API to either
>>
>> dav_error * (*deliver)(const dav_resource *resource,
>> request_rec *r);
>>
>> and using r->output_filters in the implementations of the deliver method in
>
> I chose not to do it this way because I wanted to isolate the DAV
> providers as much as possible from the request_rec. The idea was to
> keep them relatively independent of the fact that the provider is
> operating within an HTTP server. Passing r->output_filter was a much
> "narrower" view than passing the entire request_rec. The request_rec
> has got a LOT of exposure to the innards of the server, and I didn't
> want providers to worry about that, and certainly not to muck around
> with it and do funny things.
Hence my second idea of
dav_error * (*deliver)(const dav_resource *resource,
ap_filter_t **output);
and using *output in the implementations of the deliver method in
the dav_providers and calling deliver with &(r->output_filters) as second parameter.
>
> Obviously, that approach has now caused a problem where you have
> filter that want to remove themselves. I would posit that even if you
> solve the mod_dav case, that the problem would *still* exist. Other
> content generators might grab r->output_filters at the beginning of
> their operation, and then never notice a change in the request_rec.
> mod_dav does this, but how do we know that others do not?
We don't and you are correct that this only solves the mod_dav issue.
So your idea would be to change the filter API in a way that r->output_filters
remains constant between ap_pass_brigade calls?
The easiest, but maybe ugly, idea that comes to my mind here is to add a dummy
filter as the very first filter that only passes the brigade and never gets removed
during ap_pass_brigade calls.
Regards
Rüdiger
Re: svn commit: r1103315 - /httpd/httpd/trunk/modules/filters/mod_deflate.c
Posted by Greg Stein <gs...@gmail.com>.
On Wed, May 25, 2011 at 02:53, Ruediger Pluem <rp...@apache.org> wrote:
>...
> Thanks. IMHO this is a design flaw in the DAV provider API in conjunction
> with the current filter API:
>
> dav_error * (*deliver)(const dav_resource *resource,
> ap_filter_t *output);
>
> deliver is usually called with r->output_filters as a second parameter and
> thus makes a copy of the contents of r->output_filters and hence does not
> notice if r->output_filters changes during the process of pushing content thru
> the filter chain by multiple calls to ap_pass_brigade.
Yup...
> My idea would be to change the DAV API to either
>
> dav_error * (*deliver)(const dav_resource *resource,
> request_rec *r);
>
> and using r->output_filters in the implementations of the deliver method in
I chose not to do it this way because I wanted to isolate the DAV
providers as much as possible from the request_rec. The idea was to
keep them relatively independent of the fact that the provider is
operating within an HTTP server. Passing r->output_filter was a much
"narrower" view than passing the entire request_rec. The request_rec
has got a LOT of exposure to the innards of the server, and I didn't
want providers to worry about that, and certainly not to muck around
with it and do funny things.
Obviously, that approach has now caused a problem where you have
filter that want to remove themselves. I would posit that even if you
solve the mod_dav case, that the problem would *still* exist. Other
content generators might grab r->output_filters at the beginning of
their operation, and then never notice a change in the request_rec.
mod_dav does this, but how do we know that others do not?
Cheers,
-g
Re: svn commit: r1103315 - /httpd/httpd/trunk/modules/filters/mod_deflate.c
Posted by Ruediger Pluem <rp...@apache.org>.
On 05/27/2011 06:11 AM, Justin Erenkrantz wrote:
> On Tue, May 24, 2011 at 8:53 PM, Ruediger Pluem <rp...@apache.org> wrote:
>> Thanks. IMHO this is a design flaw in the DAV provider API in conjunction
>> with the current filter API:
>>
>> dav_error * (*deliver)(const dav_resource *resource,
>> ap_filter_t *output);
>>
>> deliver is usually called with r->output_filters as a second parameter and
>> thus makes a copy of the contents of r->output_filters and hence does not
>> notice if r->output_filters changes during the process of pushing content thru
>> the filter chain by multiple calls to ap_pass_brigade.
>> My idea would be to change the DAV API to either
>>
>> dav_error * (*deliver)(const dav_resource *resource,
>> request_rec *r);
>>
>> and using r->output_filters in the implementations of the deliver method in
>> the dav_providers or
>>
>> dav_error * (*deliver)(const dav_resource *resource,
>> ap_filter_t **output);
>>
>> and using *output in the implementations of the deliver method in
>> the dav_providers and calling deliver with &(r->output_filters) as second parameter.
>>
>> BTW: Same thing applies to deliver_report and merge.
>
> Sure - changing APIs like this is one way to solve it; but that's not
> a legitimate option for 2.2.x. When this was discussed on
That's the beauty of your solution: It can be backported to 2.2.x.
But it only solves the issue for mod_deflate and not for other filters.
But in the light of an upcoming 2.4.x which makes these kind of API changes
impossible again for a long time I wanted to start a discussion about
how to fix this in a general way for all filters.
> dev@subversion, I know Greg was reluctant to see the mod_dav APIs
> revved after so long. =) -- justin
I am open for other proposals, e.g. to fix the filter API, but I haven't
found an approach that I like yet.
Regards
Rüdiger
Re: svn commit: r1103315 - /httpd/httpd/trunk/modules/filters/mod_deflate.c
Posted by Justin Erenkrantz <ju...@erenkrantz.com>.
On Tue, May 24, 2011 at 8:53 PM, Ruediger Pluem <rp...@apache.org> wrote:
> On 05/24/2011 07:41 PM, Justin Erenkrantz wrote:
>> On Mon, May 23, 2011 at 8:08 PM, Ruediger Pluem <rp...@apache.org> wrote:
>>> Indeed this sounds like a general problem, but
>>> ap_remove_output_filter does adjust r->output_filters if f->r is set and the
>>> filter is the first one in the chain.
>>> Which responses exactly suffer from this behaviour?
>>
>> Use "SetOutputFilter DEFLATE" and issue a GET request for a file w/o
>> Accept-Encoding headers to Subversion (via mod_dav). You'll see that
>> mod_deflate tries to remove itself, but mod_dav keeps invoking it.
>> It's at the point where most companies who support Subversion refuse
>> to support mod_deflate/gzip due to this issue.
>
> Thanks. IMHO this is a design flaw in the DAV provider API in conjunction
> with the current filter API:
>
> dav_error * (*deliver)(const dav_resource *resource,
> ap_filter_t *output);
>
> deliver is usually called with r->output_filters as a second parameter and
> thus makes a copy of the contents of r->output_filters and hence does not
> notice if r->output_filters changes during the process of pushing content thru
> the filter chain by multiple calls to ap_pass_brigade.
> My idea would be to change the DAV API to either
>
> dav_error * (*deliver)(const dav_resource *resource,
> request_rec *r);
>
> and using r->output_filters in the implementations of the deliver method in
> the dav_providers or
>
> dav_error * (*deliver)(const dav_resource *resource,
> ap_filter_t **output);
>
> and using *output in the implementations of the deliver method in
> the dav_providers and calling deliver with &(r->output_filters) as second parameter.
>
> BTW: Same thing applies to deliver_report and merge.
Sure - changing APIs like this is one way to solve it; but that's not
a legitimate option for 2.2.x. When this was discussed on
dev@subversion, I know Greg was reluctant to see the mod_dav APIs
revved after so long. =) -- justin
Re: svn commit: r1103315 - /httpd/httpd/trunk/modules/filters/mod_deflate.c
Posted by Ruediger Pluem <rp...@apache.org>.
On 05/24/2011 07:41 PM, Justin Erenkrantz wrote:
> On Mon, May 23, 2011 at 8:08 PM, Ruediger Pluem <rp...@apache.org> wrote:
>> Indeed this sounds like a general problem, but
>> ap_remove_output_filter does adjust r->output_filters if f->r is set and the
>> filter is the first one in the chain.
>> Which responses exactly suffer from this behaviour?
>
> Use "SetOutputFilter DEFLATE" and issue a GET request for a file w/o
> Accept-Encoding headers to Subversion (via mod_dav). You'll see that
> mod_deflate tries to remove itself, but mod_dav keeps invoking it.
> It's at the point where most companies who support Subversion refuse
> to support mod_deflate/gzip due to this issue.
Thanks. IMHO this is a design flaw in the DAV provider API in conjunction
with the current filter API:
dav_error * (*deliver)(const dav_resource *resource,
ap_filter_t *output);
deliver is usually called with r->output_filters as a second parameter and
thus makes a copy of the contents of r->output_filters and hence does not
notice if r->output_filters changes during the process of pushing content thru
the filter chain by multiple calls to ap_pass_brigade.
My idea would be to change the DAV API to either
dav_error * (*deliver)(const dav_resource *resource,
request_rec *r);
and using r->output_filters in the implementations of the deliver method in
the dav_providers or
dav_error * (*deliver)(const dav_resource *resource,
ap_filter_t **output);
and using *output in the implementations of the deliver method in
the dav_providers and calling deliver with &(r->output_filters) as second parameter.
BTW: Same thing applies to deliver_report and merge.
Regards
Rüdiger
Re: svn commit: r1103315 - /httpd/httpd/trunk/modules/filters/mod_deflate.c
Posted by Justin Erenkrantz <ju...@erenkrantz.com>.
On Mon, May 23, 2011 at 8:08 PM, Ruediger Pluem <rp...@apache.org> wrote:
> Indeed this sounds like a general problem, but
> ap_remove_output_filter does adjust r->output_filters if f->r is set and the
> filter is the first one in the chain.
> Which responses exactly suffer from this behaviour?
Use "SetOutputFilter DEFLATE" and issue a GET request for a file w/o
Accept-Encoding headers to Subversion (via mod_dav). You'll see that
mod_deflate tries to remove itself, but mod_dav keeps invoking it.
It's at the point where most companies who support Subversion refuse
to support mod_deflate/gzip due to this issue.
HTH. -- justin
Re: svn commit: r1103315 - /httpd/httpd/trunk/modules/filters/mod_deflate.c
Posted by Ruediger Pluem <rp...@apache.org>.
On 05/23/2011 06:11 PM, Justin Erenkrantz wrote:
> On Mon, May 23, 2011 at 6:00 AM, Graham Leggett <mi...@sharp.fm> wrote:
>> On 23 May 2011, at 5:57 PM, Justin Erenkrantz wrote:
>>
>>> mod_dav uses r->output_filters - but, the pointer never gets updated
>>> when it is the first one in the chain. Hence, we call mod_deflate all
>>> the time even on a request that can't support it - so we have to avoid
>>> repeated memory allocations in that case. -- justin
>> This sounds like a general problem though, are other modules potentially
>> affected?
>
> Yes - this why I said in my earlier post that we'd likely need to
> re-architect filters to deal with it. -- justin
>
>
Indeed this sounds like a general problem, but
ap_remove_output_filter does adjust r->output_filters if f->r is set and the
filter is the first one in the chain.
Which responses exactly suffer from this behaviour?
Regards
Rüdiger
Re: svn commit: r1103315 - /httpd/httpd/trunk/modules/filters/mod_deflate.c
Posted by Justin Erenkrantz <ju...@erenkrantz.com>.
On Mon, May 23, 2011 at 6:00 AM, Graham Leggett <mi...@sharp.fm> wrote:
> On 23 May 2011, at 5:57 PM, Justin Erenkrantz wrote:
>
>> mod_dav uses r->output_filters - but, the pointer never gets updated
>> when it is the first one in the chain. Hence, we call mod_deflate all
>> the time even on a request that can't support it - so we have to avoid
>> repeated memory allocations in that case. -- justin
>
> This sounds like a general problem though, are other modules potentially
> affected?
Yes - this why I said in my earlier post that we'd likely need to
re-architect filters to deal with it. -- justin
Re: svn commit: r1103315 - /httpd/httpd/trunk/modules/filters/mod_deflate.c
Posted by Graham Leggett <mi...@sharp.fm>.
On 23 May 2011, at 5:57 PM, Justin Erenkrantz wrote:
> mod_dav uses r->output_filters - but, the pointer never gets updated
> when it is the first one in the chain. Hence, we call mod_deflate all
> the time even on a request that can't support it - so we have to avoid
> repeated memory allocations in that case. -- justin
This sounds like a general problem though, are other modules
potentially affected?
Regards,
Graham
--
Re: svn commit: r1103315 - /httpd/httpd/trunk/modules/filters/mod_deflate.c
Posted by Justin Erenkrantz <ju...@erenkrantz.com>.
On Mon, May 23, 2011 at 4:54 AM, Ruediger Pluem <rp...@apache.org> wrote:
> Hm, why is it not sufficient that we call ap_remove_output_filter and remove the
> filter from the chain if we do not initialize?
> Does ap_remove_output_filter not remove the filter when it is the first one
> in the chain?
mod_dav uses r->output_filters - but, the pointer never gets updated
when it is the first one in the chain. Hence, we call mod_deflate all
the time even on a request that can't support it - so we have to avoid
repeated memory allocations in that case. -- justin
Re: svn commit: r1103315 - /httpd/httpd/trunk/modules/filters/mod_deflate.c
Posted by Ruediger Pluem <rp...@apache.org>.
On 05/15/2011 01:05 PM, jerenkrantz@apache.org wrote:
> Author: jerenkrantz
> Date: Sun May 15 11:05:36 2011
> New Revision: 1103315
>
> URL: http://svn.apache.org/viewvc?rev=1103315&view=rev
> Log:
> Fix memory leak in mod_deflate.c when SetOutputFilter is enabled and DEFLATE
> is the first filter in the chain - this is seen with mod_dav + mod_deflate
> when the client can not do gzip and we constantly do the filter checks.
>
> * modules/filters/mod_deflate.c
> (deflate_ctx): Add a flag as to whether we are successfully initialized.
> (deflate_out_filter): Always create filter ctx and set filter_init flag only
> after we are successfully initialized.
>
> Modified:
> httpd/httpd/trunk/modules/filters/mod_deflate.c
Hm, why is it not sufficient that we call ap_remove_output_filter and remove the
filter from the chain if we do not initialize?
Does ap_remove_output_filter not remove the filter when it is the first one
in the chain?
Regards
Rüdiger