This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: mimetypes.guess_type() hits recursion limit
Type: behavior Stage: needs patch
Components: Library (Lib) Versions: Python 3.0, Python 3.1, Python 2.7, Python 2.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: djc, georg.brandl, pitrou
Priority: normal Keywords:

Created on 2009-04-27 08:56 by djc, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
mimetest.py pitrou, 2009-04-27 21:03
Messages (8)
msg86649 - (view) Author: Dirkjan Ochtman (djc) * (Python committer) Date: 2009-04-27 08:56
I've got hgweb (the Mercurial web app) crashing on guess_type() in
2.6.2, but not in 2.5.4. I'm passing in a filename like
'/home/djc/src/hg/crew/templates/static/hglogo.png'. Doesn't happen on
the REPL, but happens in side the hg serve web server.

Traceback (most recent call last):
  File "/home/djc/src/hg/crew/mercurial/hgweb/server.py", line 67, in
do_POST
    self.do_write()
  File "/home/djc/src/hg/crew/mercurial/hgweb/server.py", line 60, in
do_write
    self.do_hgweb()
  File "/home/djc/src/hg/crew/mercurial/hgweb/server.py", line 124, in
do_hgweb
    for chunk in self.server.application(env, self._start_response):
  File "/home/djc/src/hg/crew/mercurial/hgweb/hgwebdir_mod.py", line 91,
in __call__
    return self.run_wsgi(req)
  File "/home/djc/src/hg/crew/mercurial/hgweb/hgwebdir_mod.py", line
132, in run_wsgi
    return (staticfile(static, fname, req),)
  File "/home/djc/src/hg/crew/mercurial/hgweb/common.py", line 73, in
staticfile
    ct = mimetypes.guess_type(path)[0] or "text/plain"
  File "/usr/lib/python2.6/mimetypes.py", line 244, in guess_type
    return guess_type(url, strict)
(... snip ...)
  File "/usr/lib/python2.6/mimetypes.py", line 244, in guess_type
    return guess_type(url, strict)
RuntimeError: maximum recursion depth exceeded
msg86650 - (view) Author: Dirkjan Ochtman (djc) * (Python committer) Date: 2009-04-27 09:05
georg.brandl remarked it might be due to demandimport. That doesn't seem
to be the case:

>>> from mercurial import demandimport
>>> demandimport.enable()
>>> import mimetypes
>>>
mimetypes.guess_type('/home/djc/src/hg/crew/templates/static/hglogo.png')
('image/png', None)
msg86664 - (view) Author: Dirkjan Ochtman (djc) * (Python committer) Date: 2009-04-27 13:57
This could well be due to the SocketServer.ThreadingMixIn that's being
used by the hg serve built-in web server (since it doesn't show on REPL
or, as far as I can see, when used from within Apache + mod_wsgi).
msg86665 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2009-04-27 14:01
The function monkeypatching hack should be replaced with something more
robust.
(what might happen here is that init() is first called from another
thread, sets `inited` to True and then crashes for whatever reason, so
guess_type() isn't actually monkeypatched)
msg86666 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2009-04-27 14:04
The plan is to replace init() with something like:

_db = None

def init():
    global _db
    db = ...   
    # initialize database
    # set it to global only when it's fully ready
    _db = db

and guess_type() with:

def guess_type():
    if _db is None:
       init()
    return _db.guess_type()

(same for other functions)
msg86667 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2009-04-27 14:05
In the meantime, hgweb could probably call mimetypes.init() at the
beginning (before spawning threads).
msg86695 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2009-04-27 21:03
For the record, here is a test script showcasing the problem.
msg86696 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2009-04-27 21:06
Committed a fix in r72045. Thanks for the report!
History
Date User Action Args
2022-04-11 14:56:48adminsetgithub: 50103
2009-04-28 14:58:06pitroulinkissue5868 superseder
2009-04-27 21:06:55pitrousetstatus: open -> closed
resolution: fixed
messages: + msg86696
2009-04-27 21:03:21pitrousetfiles: + mimetest.py

messages: + msg86695
2009-04-27 14:05:55pitrousetmessages: + msg86667
2009-04-27 14:04:57pitrousetmessages: + msg86666
2009-04-27 14:01:31pitrousetpriority: normal

type: behavior
versions: + Python 3.0, Python 3.1, Python 2.7
nosy: + pitrou

messages: + msg86665
stage: needs patch
2009-04-27 13:57:31djcsetmessages: + msg86664
2009-04-27 09:05:44djcsetmessages: + msg86650
2009-04-27 08:57:00djccreate