You are viewing a plain text version of this content. The canonical link for it is here.
Posted to apreq-dev@httpd.apache.org by "Edward L. Abrams" <ab...@bitscrap.com> on 2006/06/21 03:38:46 UTC

Newbie: Question

Hello,

I apologize for my brief intrusion on this list for a question that may 
or may not be within the boundaries of the list's purpose.  I have been 
using libapreq to do file uploads for some time.  Lately, I've noticed 
that I have memory leaks.  I don't know whether it's my newbie use of 
mostly-undocumented code, or the code itself (I favor the former!).

If this is the wrong place to ask what I am doing wrong in my code, 
where is the right place to ask?  The modules@ list was unresponsive, 
and I could find no other close candidate.  If this is not the wrong 
place to ask, then let me know, and I'll show you something that I 
believe you will be able to help me correct very easily.

Again, sorry, and thank you.

Eddie

Re: Newbie: Question

Posted by Joe Schaefer <jo...@sunstarsys.com>.
"Edward L. Abrams" <ab...@bitscrap.com> writes:

> Thank you very much.  Is the following too loose:
>
>      for (b = APR_BRIGADE_FIRST(bb);
>           b != APR_BRIGADE_SENTINEL(bb);
>           b = APR_BUCKET_NEXT(b))
>        {
>          if (APR_BUCKET_IS_EOS(b))
>            {
>              break;
>            }

I don't think we have eos buckets in the upload brigade.

>          else if (apr_bucket_read(b, &buf, &bytes, APR_BLOCK_READ) ==
> APR_SUCCESS)
>            {
>              // We have a bucket full of raw data
>              apr_file_write(f, buf, &bytes);
>              filesize += bytes;
>              apr_bucket_delete(b);

I don't like calling APR_BUCKET_NEXT on a deleted bucket.
IMO deleting a bucket is like calling free on it, so it
should be considered a dead pointer at this point.

> Also, a last question: when dealing with file buckets, I understand
> that heap buckets are created as the file is read.  That closely
> corresponds with what you say below.  So my question is: does this
> mean there is more cleanup that has to be done wrt the brigade
> file->upload, the file bucket brigade? 


apreq cleans up its brigades after the request is over.
It shouldn't leak anything comparable to the upload size,
since we use spool (essentially file) buckets to hold that
data.

-- 
Joe Schaefer


Re: Newbie: Question

Posted by "Edward L. Abrams" <ab...@bitscrap.com>.
Thank you very much.  Is the following too loose:

      for (b = APR_BRIGADE_FIRST(bb);
           b != APR_BRIGADE_SENTINEL(bb);
           b = APR_BUCKET_NEXT(b))
        {
          if (APR_BUCKET_IS_EOS(b))
            {
              break;
            }
          else if (apr_bucket_read(b, &buf, &bytes, APR_BLOCK_READ) == 
APR_SUCCESS)
            {
              // We have a bucket full of raw data
              apr_file_write(f, buf, &bytes);
              filesize += bytes;
              apr_bucket_delete(b);
            }
        }
    apr_file_close(f);

      if (bb != NULL)
      {
         apr_brigade_cleanup(bb);
      }


Also, a last question: when dealing with file buckets, I understand that 
heap buckets are created as the file is read.  That closely corresponds 
with what you say below.  So my question is: does this mean there is 
more cleanup that has to be done wrt the brigade file->upload, the file 
bucket brigade?  Sometimes it seems to me like the code above leaks too 
-- that may be because of the difference between it and your suggestion 
(which I don't really see a significant difference) or because of 
something having specifically to do with cleaning up after filebody.

Anyway, thank you!

Eddie




Joe Schaefer wrote:
> "Edward L. Abrams" <ab...@bitscrap.com> writes:
>
>   
>>   // dump the values in this brigade:
>>   for (b = APR_BRIGADE_FIRST(bb);
>>        b != APR_BRIGADE_SENTINEL(bb);
>>        b = APR_BUCKET_NEXT(b))
>>     
>
> This is the source of your leak.
> What you need to do is copy the upload brigade
> and iterate over it using something like
>
>
>     while (!APR_BRIGADE_EMPTY(bb)) {
>        apr_bucket_t *e = APR_BRIGADE_FIRST(bb);
>
>        ...read from the bucket, do something with data...
>
>        apr_bucket_delete(e);
>     }
>
>
> ie, destroy the buckets as you read from them.
> reading from file buckets tends to convert those buckets
> into heap buckets, and unless you delete them as you go,
> you wind up copying the entire spool file into RAM.
>
>   


Re: Newbie: Question

Posted by Joe Schaefer <jo...@sunstarsys.com>.
"Edward L. Abrams" <ab...@bitscrap.com> writes:

>   // dump the values in this brigade:
>   for (b = APR_BRIGADE_FIRST(bb);
>        b != APR_BRIGADE_SENTINEL(bb);
>        b = APR_BUCKET_NEXT(b))

This is the source of your leak.
What you need to do is copy the upload brigade
and iterate over it using something like


    while (!APR_BRIGADE_EMPTY(bb)) {
       apr_bucket_t *e = APR_BRIGADE_FIRST(bb);

       ...read from the bucket, do something with data...

       apr_bucket_delete(e);
    }


ie, destroy the buckets as you read from them.
reading from file buckets tends to convert those buckets
into heap buckets, and unless you delete them as you go,
you wind up copying the entire spool file into RAM.

-- 
Joe Schaefer


Re: Newbie: Question

Posted by "Edward L. Abrams" <ab...@bitscrap.com>.
Thank you for your reply.  Unfortunately, my module is written in C, so 
I suspect the modperl list might not be as helpful.

Here is the skeleton of my upload code.  The function given is the 
content handler for this particular request.  There are no significant 
prior input filters, and no significant posterior output filters.  When 
I POST an upload, this content handler saves the files in the multipart 
POST, and returns a trivial response (not fully shown).  In my testing, 
it tends to leak memory proportional (but not equal) to the size of the 
total upload.

I initialize mod_apreq in the way offered in the documentation: by 
LoadModule'ing it, and expecting it to insert itself into the latest 
possible input filter position (which should not be difficult, since I 
have no other custom input filters).

I apologize for the length of this email.  Thank you for helping out!

Eddie

__snip__

// a content handler
static int do_post(request_rec *r)
{
 apreq_handle_t *handle;
 const apreq_module_t *module;
 apreq_param_t *filebody, *testfilebody;
 apr_bucket_brigade *bb;
 apr_bucket *b;
 char *loopfilename, *myip, *disk, *tmpfile, *newfilename, 
*extensionfilename, *lastdot;
 char *localorigfilename_raw, *localorigfilename, *nameindex = NULL;
 const char *buf = NULL;
 apr_file_t *f = NULL;
 unsigned int md_len, copies, filesize = 0, any_processed = 0;
 apr_size_t bytes;
 int i, lresult = 0;

 handle = (apreq_handle_t *)apreq_handle_apache2(r);
 apreq_read_limit_set(handle, READ_LIMIT);
 module = handle->module;

 localorigfilename_raw = (char *)apr_pcalloc(r->pool, 512);
 loopfilename = (char *)apr_pcalloc(r->pool, 16);

 for (i = 0; i < MAX_FILES_PROCESSED; i++)
 {
   filesize = 0;
   sprintf(loopfilename, "%s%d", DEFAULT_FILE_PARAM_NAME, i);

   // i do typically test whether this mulitpart part is a file -- for 
the sake of this skeleton, that code is omitted
   filebody = module->body_get(handle, loopfilename);
   memcpy(localorigfilename_raw, (filebody->v).data, 
strlen((filebody->v).data));
   bb = filebody->upload;

   tmpfile = create_temp_path(disk, (char *)localorigfilename);

   if (apr_file_open(&f, 
tmpfile,APR_WRITE|APR_CREATE|APR_BINARY|APR_TRUNCATE,APR_UREAD|APR_UWRITE|APR_UEXECUTE, 
r->pool) != APR_SUCCESS)
   {
      return HTTP_FORBIDDEN;
   }

   // dump the values in this brigade:
   for (b = APR_BRIGADE_FIRST(bb);
        b != APR_BRIGADE_SENTINEL(bb);
        b = APR_BUCKET_NEXT(b))
   {         if (APR_BUCKET_IS_EOS(b))
        {
          break;
        }
        else if (apr_bucket_read(b, &buf, &bytes, APR_BLOCK_READ) == 
APR_SUCCESS)
        {
          // We have a bucket full of raw data
          apr_file_write(f, buf, &bytes);
          filesize += bytes;
        }
   }

   apr_file_close(f);
   if (bb != NULL)
   {
      apr_brigade_destroy(bb);
     // apr_brigade_cleanup(bb);
   }
 } // for i in 0 to MAX_FILES_PROCESSED

 if (any_processed > 0)
 {
   return OK;
 }
 else
 {
     return HTTP_FORBIDDEN;
  }
}

__snip__




Philip M. Gollucci wrote:
> Edward L. Abrams wrote:
>> Hello,
>>
>> I apologize for my brief intrusion on this list for a question that 
>> may or may not be within the boundaries of the list's purpose.  I 
>> have been using libapreq to do file uploads for some time.  Lately, 
>> I've noticed that I have memory leaks.  I don't know whether it's my 
>> newbie use of mostly-undocumented code, or the code itself (I favor 
>> the former!).
> This is generally for developement of libapreq itself, but sometimes 
> user questions are on topic.
>
> You might try modperl (at) perl (dot) apache (dot) org
>
>> If this is the wrong place to ask what I am doing wrong in my code, 
>> where is the right place to ask?  The modules@ list was unresponsive, 
>> and I could find no other close candidate.  If this is not the wrong 
>> place to ask, then let me know, and I'll show you something that I 
>> believe you will be able to help me correct very easily.
> Can you show some code ?
>
>


Re: Newbie: Question

Posted by "Philip M. Gollucci" <pg...@p6m7g8.com>.
Edward L. Abrams wrote:
> Hello,
> 
> I apologize for my brief intrusion on this list for a question that may 
> or may not be within the boundaries of the list's purpose.  I have been 
> using libapreq to do file uploads for some time.  Lately, I've noticed 
> that I have memory leaks.  I don't know whether it's my newbie use of 
> mostly-undocumented code, or the code itself (I favor the former!).
This is generally for developement of libapreq itself, but sometimes 
user questions are on topic.

You might try modperl (at) perl (dot) apache (dot) org

> If this is the wrong place to ask what I am doing wrong in my code, 
> where is the right place to ask?  The modules@ list was unresponsive, 
> and I could find no other close candidate.  If this is not the wrong 
> place to ask, then let me know, and I'll show you something that I 
> believe you will be able to help me correct very easily.
Can you show some code ?


-- 
------------------------------------------------------------------------
Philip M. Gollucci (pgollucci@p6m7g8.com) 323.219.4708
Consultant / http://p6m7g8.net/Resume/resume.shtml
Senior Software Engineer - TicketMaster - http://ticketmaster.com
1024D/A79997FA F357 0FDD 2301 6296 690F  6A47 D55A 7172 A799 97F

"It takes a minute to have a crush on someone, an hour to like someone,
and a day to love someone, but it takes a lifetime to forget someone..."