Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mimetypes.guess_type() hits recursion limit #50103

Closed
djc opened this issue Apr 27, 2009 · 8 comments
Closed

mimetypes.guess_type() hits recursion limit #50103

djc opened this issue Apr 27, 2009 · 8 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@djc
Copy link
Member

djc commented Apr 27, 2009

BPO 5853
Nosy @birkenfeld, @pitrou, @djc
Files
  • mimetest.py
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2009-04-27.21:06:55.464>
    created_at = <Date 2009-04-27.08:56:56.665>
    labels = ['type-bug', 'library']
    title = 'mimetypes.guess_type() hits recursion limit'
    updated_at = <Date 2009-04-27.21:06:55.463>
    user = 'https://github.com/djc'

    bugs.python.org fields:

    activity = <Date 2009-04-27.21:06:55.463>
    actor = 'pitrou'
    assignee = 'none'
    closed = True
    closed_date = <Date 2009-04-27.21:06:55.464>
    closer = 'pitrou'
    components = ['Library (Lib)']
    creation = <Date 2009-04-27.08:56:56.665>
    creator = 'djc'
    dependencies = []
    files = ['13798']
    hgrepos = []
    issue_num = 5853
    keywords = []
    message_count = 8.0
    messages = ['86649', '86650', '86664', '86665', '86666', '86667', '86695', '86696']
    nosy_count = 3.0
    nosy_names = ['georg.brandl', 'pitrou', 'djc']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'needs patch'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue5853'
    versions = ['Python 2.6', 'Python 3.0', 'Python 3.1', 'Python 2.7']

    @djc
    Copy link
    Member Author

    djc commented Apr 27, 2009

    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

    @djc djc added the stdlib Python modules in the Lib dir label Apr 27, 2009
    @djc
    Copy link
    Member Author

    djc commented Apr 27, 2009

    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)

    @djc
    Copy link
    Member Author

    djc commented Apr 27, 2009

    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).

    @pitrou
    Copy link
    Member

    pitrou commented Apr 27, 2009

    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)

    @pitrou pitrou added the type-bug An unexpected behavior, bug, or error label Apr 27, 2009
    @pitrou
    Copy link
    Member

    pitrou commented Apr 27, 2009

    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)

    @pitrou
    Copy link
    Member

    pitrou commented Apr 27, 2009

    In the meantime, hgweb could probably call mimetypes.init() at the
    beginning (before spawning threads).

    @pitrou
    Copy link
    Member

    pitrou commented Apr 27, 2009

    For the record, here is a test script showcasing the problem.

    @pitrou
    Copy link
    Member

    pitrou commented Apr 27, 2009

    Committed a fix in r72045. Thanks for the report!

    @pitrou pitrou closed this as completed Apr 27, 2009
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants