使用 tidy 渲染视图时出现问题(Django + Apache mod_wsgi)

发布于 2024-09-29 13:55:39 字数 6468 浏览 5 评论 0原文

当在内置开发服务器下运行时,一切运行良好。

但是,当在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

留一抹残留的笑 2024-10-06 13:55:39

答案似乎是 µTidylib 中未修复的错误: Bug #14691 - 绑定不是 64 位安全

注释中的补丁解决了该问题:

----------
diff -ruN uTidylib-0.2-orig/tidy/lib.py uTidylib-0.2/tidy/lib.py
--- uTidylib-0.2-orig/tidy/lib.py 2004-02-24 08:12:24.000000000 +0000
+++ uTidylib-0.2/tidy/lib.py 2008-10-16 14:45:56.000000000 +0100
@@ -130,6 +130,8 @@

sinkfactory=SinkFactory()

+_tidy.Create.restype = ctypes.POINTER(ctypes.c_void_p)
+
class _Document(object):
def __init__(self):
self.cdoc = _tidy.Create()
----------

以及对 render_tidy_response 的修改:

---     (revision 409)
+++     (working copy)
@@ -15,6 +15,7 @@
                           output_xhtml=1,
                           tidy_mark=0, 
                           numeric_entities=1,
+             input_encoding='utf8',
                           output_encoding='utf8',
                           drop_empty_paras=1,
                           escape_cdata=1,
@@ -25,8 +26,8 @@
                           )
     if (request != None):
         request.session['current_user'] = request.user    
-    #tidy_output = str(tidy.parseString(raw_template, **output_options))    
-    return HttpResponse(raw_template, **httpresponse_kwargs)
+    tidy_output = str(tidy.parseString(raw_template.encode('utf-8'), **output_options))    
+    return HttpResponse(tidy_output, **httpresponse_kwargs)

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:

----------
diff -ruN uTidylib-0.2-orig/tidy/lib.py uTidylib-0.2/tidy/lib.py
--- uTidylib-0.2-orig/tidy/lib.py 2004-02-24 08:12:24.000000000 +0000
+++ uTidylib-0.2/tidy/lib.py 2008-10-16 14:45:56.000000000 +0100
@@ -130,6 +130,8 @@

sinkfactory=SinkFactory()

+_tidy.Create.restype = ctypes.POINTER(ctypes.c_void_p)
+
class _Document(object):
def __init__(self):
self.cdoc = _tidy.Create()
----------

Along with a modification to render_tidy_response:

---     (revision 409)
+++     (working copy)
@@ -15,6 +15,7 @@
                           output_xhtml=1,
                           tidy_mark=0, 
                           numeric_entities=1,
+             input_encoding='utf8',
                           output_encoding='utf8',
                           drop_empty_paras=1,
                           escape_cdata=1,
@@ -25,8 +26,8 @@
                           )
     if (request != None):
         request.session['current_user'] = request.user    
-    #tidy_output = str(tidy.parseString(raw_template, **output_options))    
-    return HttpResponse(raw_template, **httpresponse_kwargs)
+    tidy_output = str(tidy.parseString(raw_template.encode('utf-8'), **output_options))    
+    return HttpResponse(tidy_output, **httpresponse_kwargs)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文