jvandal / modwsgi

Automatically exported from code.google.com/p/modwsgi
0 stars 0 forks source link

Truncated response with using wsgi.file_wrapper when data > 255 and < ~8000. #132

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Per discussion in:

  http://groups.google.com/group/modwsgi/browse_frm/thread/e7111816e70d236a

Apache implements an optimisation when keep alive is requested, which results 
in file buckets 
referencing a file containing > 255 and < ~8000 bytes, not being immediately 
flushed out but 
held over to when request actually ends or subsequent request on same 
connection occurs.

This causes problems for mod_wsgi as the file descriptor wrapped by 
wsgi.file_wrapper could 
have been closed by the time that Apache tries to use it. The end result is a 
completely empty 
response and an error on the Apache error logs (but only if LogLevel is debug) 
of:

  (9)Bad file descriptor: core_output_filter: writing data to the network

The affected code in mod_wsgi is in Adapter_output_file():

    APR_BRIGADE_INSERT_TAIL(bb, b);

    b = apr_bucket_eos_create(r->connection->bucket_alloc);
    APR_BRIGADE_INSERT_TAIL(bb, b);

    Py_BEGIN_ALLOW_THREADS
    rv = ap_pass_brigade(r->output_filters, bb);
    Py_END_ALLOW_THREADS

As the optimisation only occurs in Apache when EOS bucket is detected 
immediately after the file 
bucket, fix is to insert a flush bucket.

    APR_BRIGADE_INSERT_TAIL(bb, b);

    b = apr_bucket_flush_create(r->connection->bucket_alloc);
    APR_BRIGADE_INSERT_TAIL(bb, b);

    b = apr_bucket_eos_create(r->connection->bucket_alloc);
    APR_BRIGADE_INSERT_TAIL(bb, b);

    Py_BEGIN_ALLOW_THREADS
    rv = ap_pass_brigade(r->output_filters, bb);
    Py_END_ALLOW_THREADS

Original issue reported on code.google.com by Graham.Dumpleton@gmail.com on 14 Feb 2009 at 10:31

GoogleCodeExporter commented 8 years ago
Fix commited to revision 1197 of trunk for 3.0.

Original comment by Graham.Dumpleton@gmail.com on 14 Feb 2009 at 10:33

GoogleCodeExporter commented 8 years ago
Also backport to 2.X branch for 2.4 at revision 1199.

Original comment by Graham.Dumpleton@gmail.com on 14 Feb 2009 at 11:10

GoogleCodeExporter commented 8 years ago
Version 2.4 of mod_wsgi now released.

Original comment by Graham.Dumpleton@gmail.com on 11 Apr 2009 at 10:25

GoogleCodeExporter commented 8 years ago
If cannot upgrade, disabling KeepAlive in Apache should prevent this problem 
from occurring as Apache 
optimisation only applied in that situation.

Original comment by Graham.Dumpleton@gmail.com on 14 Apr 2009 at 11:34

GoogleCodeExporter commented 8 years ago
Note that this issue should only by rights apply to UNIX systems and shouldn't 
occur on Windows as file 
optimisation not done on Windows.

Original comment by Graham.Dumpleton@gmail.com on 14 May 2009 at 11:47