Export to GitHub

modwsgi - issue #132

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


Posted on Feb 14, 2009 by Quick Rhino

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-&gt;connection-&gt;bucket_alloc);
APR_BRIGADE_INSERT_TAIL(bb, b);

Py_BEGIN_ALLOW_THREADS
rv = ap_pass_brigade(r-&gt;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-&gt;connection-&gt;bucket_alloc);
APR_BRIGADE_INSERT_TAIL(bb, b);

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

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

Comment #1

Posted on Feb 14, 2009 by Quick Rhino

Fix commited to revision 1197 of trunk for 3.0.

Comment #2

Posted on Feb 14, 2009 by Quick Rhino

Also backport to 2.X branch for 2.4 at revision 1199.

Comment #3

Posted on Apr 11, 2009 by Quick Rhino

Version 2.4 of mod_wsgi now released.

Comment #4

Posted on Apr 14, 2009 by Quick Rhino

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

Comment #5

Posted on May 14, 2009 by Quick Rhino

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.

Status: Fixed

Labels:
Type-Defect Priority-Critical Milestone-Release2.4