Skip to content

mimetypes.guess_type() hits recursion limit #50103

Closed

Description

@djc
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']

    Activity

    1. djc commented on Apr 27, 2009

      @djc
      ContributorAuthor

      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
    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      on Apr 27, 2009
    3. djc commented on Apr 27, 2009

      @djc
      ContributorAuthor

      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)
    4. djc commented on Apr 27, 2009

      @djc
      ContributorAuthor

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

    5. pitrou commented on Apr 27, 2009

      @pitrou
      Member

      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)

    6. added
      type-bugAn unexpected behavior, bug, or error
      on Apr 27, 2009
    7. pitrou commented on Apr 27, 2009

      @pitrou
      Member

      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)

    8. pitrou commented on Apr 27, 2009

      @pitrou
      Member

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

    9. pitrou commented on Apr 27, 2009

      @pitrou
      Member

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

    10. pitrou commented on Apr 27, 2009

      @pitrou
      Member

      Committed a fix in r72045. Thanks for the report!

    11. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions