[mod_python] Re: python exceptions cause Apache to crash

Graham Dumpleton graham.dumpleton at gmail.com
Wed Aug 6 19:28:21 EDT 2008


Sorry, that should have been:

  http://issues.apache.org/jira/browse/MODPYTHON

Graham

2008/8/7 Graham Dumpleton <graham.dumpleton at gmail.com>:
> 2008/8/7 Jason Carver <ynj0qeh02 at sneakemail.com>:
>> Great, thanks for your help!  Is there a good place for me to file a feature
>> request to have mod_python do something besides crash in this case?
>
> The problem has been discussed before, but can't see that there is an
> issue for it. Issues can be created at:
>
>  http://issues.apache.org/jira/browse/MODPYTHON-202
>
> Yes Python could protect against this so a crash doesn't occur, but
> your code shouldn't have been doing that in the first place.
>
> Graham
>
>> For reference, I have code here to demonstrate the bug and workaround:
>>
>> def testCrash(req):
>>     """
>>     Reliably crash server (on 2nd page access)
>>     Open as: http://server/index.py/testCrash
>>     """
>>     handler = logging.StreamHandler(req)
>>     logging.getLogger('').addHandler( handler )
>>     logging.exception('I crash')
>>
>>     #removing this line will cause an apache crash on the
>>     #    second web page access
>>     logging.getLogger('').removeHandler(handler)
>>
>> I have confirmed that the workaround works reliably for me.
>>
>> Cheers,
>> Jason
>>
>> On Tue, Aug 5, 2008 at 8:36 PM, Graham Dumpleton
>> graham.dumpleton-at-gmail.com |public mailing list|
>> <a9ghk53mp50t at sneakemail.com> wrote:
>>>
>>> Just realised what the problem is, rather obvious really.
>>>
>>> You are supplying a mod_python request object 'req' to:
>>>
>>>  logging.getLogger('').addHandler(logging.StreamHandler(req))
>>>
>>> You can't do that, as the logging handler you are creating will
>>> persist beyond the lifetime of the internals of the 'req' object.
>>> Thus, when logging tries to access it later, it will crash Apache.
>>>
>>> In other words, 'req' is just a wrapper for some internal Apache data
>>> structures which will go away. The mod_python code doesn't currently
>>> try and protect itself from user code making the mistake of retaining
>>> the 'req' object longer than it should and then accessing those
>>> internals.
>>>
>>> Graham
>>>
>>> 2008/8/6 Jason Carver <ynj0qeh02 at sneakemail.com>:
>>> > If I try to install mod_python without python 2.5 installed, mod_python
>>> > refuses, so I assume you mean uninstall all versions of python, then
>>> > install
>>> > python 2.5.2, then mod_python-3.3.1.win32-py2.5-Apache2.2.exe
>>> >
>>> > I did that, with no changes.  I get the same Traceback during mod_python
>>> > install and I can still reproduce the crash reliably.
>>> >
>>> > Bug requirements update: It does not matter if I test the req object, it
>>> > only matters if the req object has something set.  So these are the new
>>> > requirements to reproduce the bug
>>> >
>>> > use built in logging module
>>> > raise Exception
>>> > call page with variables (ie~ http://localhost/index.py?action=boom)
>>> > use the following code:
>>> >
>>> > import logging
>>> >
>>> > def index(req):
>>> >     """Handles all Browser Requests"""
>>> >     try:
>>> >         logging.getLogger('').addHandler( logging.StreamHandler(req) )
>>> >
>>> >         raise Exception('Goodbye Apache')
>>> >
>>> >     except Exception, e:
>>> >         logging.exception('I crash')
>>> >         raise
>>> >
>>> > So there seems to be some kind of three way interaction between the
>>> > logging
>>> > module, the apache req module, and exception handling.  If any one of
>>> > the
>>> > three are not involved, apache does not crash...
>>> >
>>> > For now I can use a home-grown logger to get around the bug, but I hate
>>> > to
>>> > build code that already exists and make future developers have to learn
>>> > my
>>> > logger instead of the built-in one.
>>> >
>>> > Cheers,
>>> > Jason
>>> >
>>> > On Mon, Aug 4, 2008 at 9:44 PM, Graham Dumpleton
>>> > graham.dumpleton-at-gmail.com |public mailing list|
>>> > <a9ghk53mp50t at sneakemail.com> wrote:
>>> >>
>>> >> Please use reply-all and keep discussion on the list, don't reply just
>>> >> to
>>> >> me.
>>> >>
>>> >> Have you tried uninstalling all versions of Python and mod_python
>>> >> first and then install it? Ie., don't install when you have existing
>>> >> versions on the system.
>>> >>
>>> >> When installing packages, are you doing it as a user with
>>> >> administrator privileges?
>>> >>
>>> >> Graham
>>> >>
>>> >> 2008/8/5  <ynj0qeh02 at sneakemail.com>:
>>> >> > Unfortunately, when I tried to install the mod_python 3.3.1 for 2.5 I
>>> >> > got
>>> >> > this traceback:
>>> >> > Traceback (most recent call last):
>>> >> >   File "boot_com_servers.py", line 21, in <module>
>>> >> >   File "C:\Python25\Lib\site-packages\pythoncom.py", line 3, in
>>> >> > <module>
>>> >> >     pywintypes.__import_pywin32_system_module__("pythoncom",
>>> >> > globals())
>>> >> >   File "C:\Python25\Lib\site-packages\win32\Lib\pywintypes.py", line
>>> >> > 98,
>>> >> > in
>>> >> > __import_pywin32_system_module__
>>> >> >     ('.dll', 'rb', imp.C_EXTENSION))
>>> >> > ImportError: DLL load failed: The specified procedure could not be
>>> >> > found.
>>> >> >
>>> >> > and when I try to start apache 2 anyway, it says "The requested
>>> >> > operation
>>> >> > has failed!"
>>> >> >
>>> >> > That's why I was using the version for 2.4.
>>> >> >
>>> >> > On Mon, Aug 4, 2008 at 9:21 PM, Graham Dumpleton
>>> >> > graham.dumpleton-at-......... |public mailing list| <...> wrote:
>>> >> >>
>>> >> >> 2008/8/5  <ynj0qeh02 at sneakemail.com>:
>>> >> >> > Thanks for the quick reply!  Answers inline...
>>> >> >> >
>>> >> >> > On Mon, Aug 4, 2008 at 7:28 PM, Graham Dumpleton
>>> >> >> > graham.dumpleton-at-......... |public mailing list| <...> wrote:
>>> >> >> >>
>>> >> >> >> What operating system? What version of Python? What version of
>>> >> >> >> mod_python?
>>> >> >> >
>>> >> >> > I am on Windows XP SP3 using Python 2.5 in mod_python 3.3.1
>>> >> >> >
>>> >> >> > I hacked the binary to use 2.5 on Windows, but the same problem is
>>> >> >> > reproducible on Ubuntu 7.10 using the latest source from svn
>>> >> >> > compiled
>>> >> >> > with
>>> >> >> > python 2.5.
>>> >> >> >
>>> >> >> >> What startup messages for mod_python are in the Apache error log?
>>> >> >> >
>>> >> >> >
>>> >> >> > [Mon Aug 04 20:03:21 2008] [notice] Server built: Nov  7 2007
>>> >> >> > 11:48:48
>>> >> >> > [Mon Aug 04 20:03:21 2008] [notice] Parent: Created child process
>>> >> >> > 27164
>>> >> >> > [Mon Aug 04 20:03:22 2008] [error] python_init: Python version
>>> >> >> > mismatch,
>>> >> >> > expected '2.4.3', found '2.5'.
>>> >> >>
>>> >> >> This is why it is probably breaking. Install mod_python compiled
>>> >> >> against Python 2.5. The version you are using was compiled against
>>> >> >> older version of Python.
>>> >> >>
>>> >> >> Fix your installation and try again.
>>> >> >>
>>> >> >> Do you get similar warnings on your other platform as well?
>>> >> >>
>>> >> >> Graham
>>> >> >>
>>> >> >> > [Mon Aug 04 20:03:22 2008] [error] python_init: Python executable
>>> >> >> > found
>>> >> >> > 'C:\\xampp\\apache\\bin\\apache.exe'.
>>> >> >> > [Mon Aug 04 20:03:22 2008] [error] python_init: Python path being
>>> >> >> > used
>>> >> >> > 'C:\\Program
>>> >> >> >
>>> >> >> >
>>> >> >> >
>>> >> >> > Files\\OpenLibraries\\python;C:\\WINDOWS\\system32\\python25.zip;C:\\Python25\\Lib;C:\\Python25\\DLLs;C:\\Python25\\Lib\\lib-tk;;C:\\xampp\\apache\\bin'.
>>> >> >> > [Mon Aug 04 20:03:22 2008] [notice] mod_python: Creating 8 session
>>> >> >> > mutexes
>>> >> >> > based on 0 max processes and 250 max threads.
>>> >> >> >
>>> >> >> >>
>>> >> >> >>
>>> >> >> >> What is the mod_python configuration you have in Apache
>>> >> >> >> configuration
>>> >> >> >> files?
>>> >> >> >
>>> >> >> >
>>> >> >> > PythonOption mod_python.mutex_directory "/pytmp"
>>> >> >> > PythonOption mod_python.mutex_locks 8
>>> >> >> >
>>> >> >> > AliasMatch      ^/slique(.*)        'C:/Documents and
>>> >> >> > Settings/Jason/My
>>> >> >> > Documents/Coding/eclipse-workspace/slique-dev/src/$1'
>>> >> >> >
>>> >> >> > <Directory 'C:/Documents and Settings/Jason/My
>>> >> >> > Documents/Coding/eclipse-workspace/slique-dev/src'>
>>> >> >> >     Options Indexes ExecCGI FollowSymLinks MultiViews
>>> >> >> >     AllowOverride All
>>> >> >> >
>>> >> >> >     AddHandler mod_python .py
>>> >> >> >     PythonHandler mod_python.publisher
>>> >> >> >     PythonDebug On
>>> >> >> >     PythonPath "sys.path + ['C:/Documents and Settings/Jason/My
>>> >> >> > Documents/Coding/eclipse-workspace/slique-dev/src']"
>>> >> >> >
>>> >> >> > </Directory>
>>> >> >> >
>>> >> >> >>
>>> >> >> >>
>>> >> >> >> Specifically, are you setting PythonPath directive in really
>>> >> >> >> strange
>>> >> >> >> ways?
>>> >> >> >
>>> >> >> > PythonPath is set this way
>>> >> >> > PythonPath "sys.path + ['C:/Documents and Settings/Jason/My
>>> >> >> > Documents/Coding/eclipse-workspace/slique-dev/src']"
>>> >> >> >
>>> >> >> >>
>>> >> >> >>
>>> >> >> >> Does it happen if you don't use the 'logging' module?
>>> >> >> >
>>> >> >> > If I comment out logging.exception('I crash'), then the traceback
>>> >> >> > prints
>>> >> >> > to
>>> >> >> > screen as expected.
>>> >> >> >
>>> >> >> >>
>>> >> >> >> Graham
>>> >> >> >>
>>> >> >> >> 2008/8/5 Jason Carver <ynj0qeh02 at sneakemail.com>:
>>> >> >> >> > Here is code that consistently crashes Apache for me:
>>> >> >> >> >
>>> >> >> >> > import logging
>>> >> >> >> >
>>> >> >> >> > def index(req):
>>> >> >> >> >     """Handles all Browser Requests"""
>>> >> >> >> >     try:
>>> >> >> >> >         reqHandler = logging.StreamHandler(req)
>>> >> >> >> >         logging.getLogger('').addHandler(reqHandler)
>>> >> >> >> >         if req.form.has_key('action'):
>>> >> >> >> >             raise Exception('Goodbye Apache')
>>> >> >> >> >     except Exception, e:
>>> >> >> >> >         logging.exception('I crash')
>>> >> >> >> >         raise
>>> >> >> >> >
>>> >> >> >> > Now just go to the site with /?action=dosomething and it will
>>> >> >> >> > crash
>>> >> >> >> >
>>> >> >> >> > Removing the if statement, the exception, or the logger are all
>>> >> >> >> > ways
>>> >> >> >> > of
>>> >> >> >> > alleviating the crash, but none of them should be necessary.
>>> >> >> >> >
>>> >> >> >> > Cheers,
>>> >> >> >> > Jason
>>> >> >> >> >
>>> >> >> >> > On Mon, Aug 4, 2008 at 2:51 PM, Jason Carver
>>> >> >> >> > <ynj0qeh02 at sneakemail.com>
>>> >> >> >> > wrote:
>>> >> >> >> >>
>>> >> >> >> >> Hi all,
>>> >> >> >> >>
>>> >> >> >> >> I am having the craziest error, confirmed on both a LAMPython
>>> >> >> >> >> and
>>> >> >> >> >> WAMPython setup: exceptions in mod_python are causing Apache
>>> >> >> >> >> to
>>> >> >> >> >> crash
>>> >> >> >> >> hard.
>>> >> >> >> >> Even stranger is that the exceptions have to meet a few
>>> >> >> >> >> specific
>>> >> >> >> >> conditions
>>> >> >> >> >> to cause Apache to crash.
>>> >> >> >> >>
>>> >> >> >> >> As best I can tell those conditions include:
>>> >> >> >> >>
>>> >> >> >> >> raising an exception (this problem always goes away if the
>>> >> >> >> >> exception
>>> >> >> >> >> is
>>> >> >> >> >> commented out)
>>> >> >> >> >> having the exception be inside an if-block that tests a
>>> >> >> >> >> dictionary
>>> >> >> >> >> (the
>>> >> >> >> >> problem goes away if I do something like """if 'hello' ==
>>> >> >> >> >> 'hello':
>>> >> >> >> >> raise
>>> >> >> >> >> Exception('goodbye')""")
>>> >> >> >> >> catching the exception and logging it using the python
>>> >> >> >> >> built-in
>>> >> >> >> >> logging.exception(e) (the problem goes away if I comment out
>>> >> >> >> >> the
>>> >> >> >> >> logger)
>>> >> >> >> >>
>>> >> >> >> >> Apache does not write anything to its error.log, so it was a
>>> >> >> >> >> bit
>>> >> >> >> >> of
>>> >> >> >> >> an
>>> >> >> >> >> adventure to discover all these elements.
>>> >> >> >> >>
>>> >> >> >> >> Any ideas?
>>> >> >> >> >>
>>> >> >> >> >> Cheers,
>>> >> >> >> >> Jason
>>> >> >> >> >
>>> >> >> >> > _______________________________________________
>>> >> >> >> > Mod_python mailing list
>>> >> >> >> > Mod_python at modpython.org
>>> >> >> >> > http://mailman.modpython.org/mailman/listinfo/mod_python
>>> >> >> >> >
>>> >> >> >> >
>>> >> >> >
>>> >> >> >
>>> >> >
>>> >> >
>>> >
>>> >
>>> > _______________________________________________
>>> > Mod_python mailing list
>>> > Mod_python at modpython.org
>>> > http://mailman.modpython.org/mailman/listinfo/mod_python
>>> >
>>> >
>>> _______________________________________________
>>> Mod_python mailing list
>>> Mod_python at modpython.org
>>> http://mailman.modpython.org/mailman/listinfo/mod_python
>>
>>
>> _______________________________________________
>> Mod_python mailing list
>> Mod_python at modpython.org
>> http://mailman.modpython.org/mailman/listinfo/mod_python
>>
>>
>


More information about the Mod_python mailing list