For FILE buckets, the behaviour of apr_bucket_setaside() is to take
*full* ownership of the underlying apr_file, that is: allocate/move
the file/cleanup to the new pool AND set the old file's fd to -1 (see
apr_file_setaside, [1]).

This can be problematic in an output filters chain whenever a FILE
bucket gets splitted. Suppose that an FTYPE_RESOURCE output filter
splits a FILE bucket in two and sends the first one through the chain,
and then due to network congestion the core output filter needs to set
the FILE bucket aside. It happens that the second FILE bucket becomes
a dangling bucket (with fd == -1).
The case I'm facing is a bit different but similar. A handler is
saving large bodies into a file and inserts an input filter for
mod_proxy to use the file bucket as request body (much like
mod_request). The file is still needed/used after mod_proxy (in an
output filter), so I don't expect the file to be invalidated by
mod_proxy's output filtering.
It seems to be an issue for mod_http2 too, judging by this workaround [2].

I don't think the caller should have to handle this kind of things,
and for the output filters splitting case it's really too much to ask.

I can only think of one way to address this: move file buckets from
the user brigade to the setaside brigade in
ap_filter_setaside_brigade(), assuming correct lifetime for the bucket
and apr_file (like we do for buckets with length == -1 already).

After a look at our codebase, this would work as is, all of the FILE
buckets we pass through output filters are from r->pool, so the
lifetime is within the EOR scope (I'm not totally sure about the http2
beam case, Stefan?).

So, how about we do that?
Would look like the attached patch (which also removes the misleading
AP_BUCKET_IS_MORPHING() macro, as discussed previouly, and
talks/comments about opaque buckets for ->length == -1, and morphing
buckets for both opaque and FILE buckets, as an attempt to clarify
things..).

Regards,
Yann.

[1] https://github.com/apache/apr/blob/trunk/file_io/unix/filedup.c#L155
[2] 
https://github.com/apache/httpd/blob/trunk/modules/http2/h2_bucket_beam.c#L1019

Reply via email to