使用 tidy 渲染视图时出现问题(Django + Apache mod_wsgi)
当在内置开发服务器下运行时,一切运行良好。
但是,当在 Apache 中的 mod_wsgi 下运行时,以下代码(django.shortcuts.render_to_response 的替换方法)会导致内部服务器错误:
# Tidy and render
def render_tidy_response(*args, **kwargs):
httpresponse_kwargs = {
'mimetype': kwargs.pop('mimetype', None)
}
raw_template = loader.render_to_string(*args, **kwargs)
request = kwargs.get('request', None)
output_options = dict(
output_xhtml=1,
tidy_mark=0,
numeric_entities=1,
output_encoding='utf8',
drop_empty_paras=1,
escape_cdata=1,
clean=1,
wrap=0,
merge_divs=0,
output_bom=0
)
if (request != None):
request.session['current_user'] = request.user
tidy_output = str(tidy.parseString(raw_template, **output_options))
return HttpResponse(tidy_output, **httpresponse_kwargs)
问题似乎出在对 tidy 库的调用上,因此以下更改可以正常工作
.. as above
#tidy_output = str(tidy.parseString(raw_template, **output_options))
return HttpResponse(raw_template, **httpresponse_kwargs)
:有人知道问题可能是什么吗?
[额外] 以下是与错误请求相关的日志条目,使用调试日志记录,但我似乎无法找出导致段错误的原因:
主日志(片段)
[Tue Oct 26 21:35:14 2010] [info] Shared memory session cache initialised
[Tue Oct 26 21:35:14 2010] [info] Init: Initializing (virtual) servers for SSL
[Tue Oct 26 21:35:14 2010] [info] mod_ssl/2.2.14 compiled against Server: Apache/2.2.14, Library: OpenSSL/0.9.8k
[Tue Oct 26 21:35:14 2010] [info] mod_wsgi (pid=3110): Initializing Python.
[Tue Oct 26 21:35:14 2010] [debug] mod_wsgi.c(8104): mod_wsgi (pid=3110): Socket for 'test' is '/var/run/apache2/wsgi.3110.3.1.sock'.
[Tue Oct 26 21:35:14 2010] [info] mod_wsgi (pid=3948): Starting process 'test' with uid=33, gid=33 and threads=25.
[Tue Oct 26 21:35:14 2010] [notice] Apache/2.2.14 (Ubuntu) mod_ssl/2.2.14 OpenSSL/0.9.8k mod_wsgi/2.8 Python/2.6.5 configured -- resuming normal operations
[Tue Oct 26 21:35:14 2010] [info] Server built: Sep 28 2010 12:54:21
[Tue Oct 26 21:35:14 2010] [debug] worker.c(1757): AcceptMutex: sysvsem (default: sysvsem)
[Tue Oct 26 21:35:14 2010] [info] mod_wsgi (pid=3949): Attach interpreter ''.
[Tue Oct 26 21:35:14 2010] [info] mod_wsgi (pid=3950): Attach interpreter ''.
[Tue Oct 26 21:35:50 2010] [notice] child pid 3948 exit signal Segmentation fault (11)
[Tue Oct 26 21:35:50 2010] [info] mod_wsgi (pid=3948): Process 'test' has died, restarting.
[Tue Oct 26 21:35:50 2010] [info] mod_wsgi (pid=4041): Starting process 'test' with uid=33, gid=33 and threads=25.
Vhost 日志(片段)
[Tue Oct 26 21:35:50 2010] [info] mod_wsgi (pid=4041): Attach interpreter ''.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8702): mod_wsgi (pid=4041): Starting 25 threads in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 1 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 2 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 3 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 4 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 5 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 6 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 7 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 8 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 9 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 10 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 11 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 12 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 13 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 14 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 15 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 16 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 17 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 18 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 19 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 20 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 21 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 22 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 23 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 24 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 25 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [info] mod_wsgi (pid=4041): Enable deadlock thread in process 'test'.
[Tue Oct 26 21:35:50 2010] [info] mod_wsgi (pid=4041): Enable monitor thread in process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8542): mod_wsgi (pid=4041): Deadlock timeout is 300.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8545): mod_wsgi (pid=4041): Inactivity timeout is 0.
When running under the in-built development server, all runs fine.
However, when running under mod_wsgi in Apache, the following code (a replacement method for django.shortcuts.render_to_response) causes an internal server error:
# Tidy and render
def render_tidy_response(*args, **kwargs):
httpresponse_kwargs = {
'mimetype': kwargs.pop('mimetype', None)
}
raw_template = loader.render_to_string(*args, **kwargs)
request = kwargs.get('request', None)
output_options = dict(
output_xhtml=1,
tidy_mark=0,
numeric_entities=1,
output_encoding='utf8',
drop_empty_paras=1,
escape_cdata=1,
clean=1,
wrap=0,
merge_divs=0,
output_bom=0
)
if (request != None):
request.session['current_user'] = request.user
tidy_output = str(tidy.parseString(raw_template, **output_options))
return HttpResponse(tidy_output, **httpresponse_kwargs)
The problem appears to be with the call to the tidy library, so the following change works fine:
.. as above
#tidy_output = str(tidy.parseString(raw_template, **output_options))
return HttpResponse(raw_template, **httpresponse_kwargs)
Does anyone have an idea what the problem might be?
[ADDED]
Here are the log entries relating to an erroring request, using debug logging, but I can't seem to find out what is causing the seg fault:
Main log (snippet)
[Tue Oct 26 21:35:14 2010] [info] Shared memory session cache initialised
[Tue Oct 26 21:35:14 2010] [info] Init: Initializing (virtual) servers for SSL
[Tue Oct 26 21:35:14 2010] [info] mod_ssl/2.2.14 compiled against Server: Apache/2.2.14, Library: OpenSSL/0.9.8k
[Tue Oct 26 21:35:14 2010] [info] mod_wsgi (pid=3110): Initializing Python.
[Tue Oct 26 21:35:14 2010] [debug] mod_wsgi.c(8104): mod_wsgi (pid=3110): Socket for 'test' is '/var/run/apache2/wsgi.3110.3.1.sock'.
[Tue Oct 26 21:35:14 2010] [info] mod_wsgi (pid=3948): Starting process 'test' with uid=33, gid=33 and threads=25.
[Tue Oct 26 21:35:14 2010] [notice] Apache/2.2.14 (Ubuntu) mod_ssl/2.2.14 OpenSSL/0.9.8k mod_wsgi/2.8 Python/2.6.5 configured -- resuming normal operations
[Tue Oct 26 21:35:14 2010] [info] Server built: Sep 28 2010 12:54:21
[Tue Oct 26 21:35:14 2010] [debug] worker.c(1757): AcceptMutex: sysvsem (default: sysvsem)
[Tue Oct 26 21:35:14 2010] [info] mod_wsgi (pid=3949): Attach interpreter ''.
[Tue Oct 26 21:35:14 2010] [info] mod_wsgi (pid=3950): Attach interpreter ''.
[Tue Oct 26 21:35:50 2010] [notice] child pid 3948 exit signal Segmentation fault (11)
[Tue Oct 26 21:35:50 2010] [info] mod_wsgi (pid=3948): Process 'test' has died, restarting.
[Tue Oct 26 21:35:50 2010] [info] mod_wsgi (pid=4041): Starting process 'test' with uid=33, gid=33 and threads=25.
Vhost log (snippet)
[Tue Oct 26 21:35:50 2010] [info] mod_wsgi (pid=4041): Attach interpreter ''.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8702): mod_wsgi (pid=4041): Starting 25 threads in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 1 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 2 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 3 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 4 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 5 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 6 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 7 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 8 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 9 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 10 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 11 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 12 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 13 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 14 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 15 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 16 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 17 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 18 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 19 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 20 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 21 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 22 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 23 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 24 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 25 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [info] mod_wsgi (pid=4041): Enable deadlock thread in process 'test'.
[Tue Oct 26 21:35:50 2010] [info] mod_wsgi (pid=4041): Enable monitor thread in process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8542): mod_wsgi (pid=4041): Deadlock timeout is 300.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8545): mod_wsgi (pid=4041): Inactivity timeout is 0.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案似乎是 µTidylib 中未修复的错误: Bug #14691 - 绑定不是 64 位安全
注释中的补丁解决了该问题:
以及对 render_tidy_response 的修改:
The answer it would seem, is an unfixed bug in µTidylib: Bug #14691 - bindings aren't 64bit safe
The patch in the comments solved the issue:
Along with a modification to render_tidy_response: