Edgewall Software
Modify

Opened 15 years ago

Closed 15 years ago

Last modified 14 years ago

#8629 closed defect (fixed)

Trac should call mimetypes.init() as it is not thread-safe to not do so before python 2.6.3

Reported by: Leonardo Santagada <santagada@…> Owned by: srl@…
Priority: normal Milestone: 0.12
Component: general Version:
Severity: major Keywords: mimetypes racecondition
Cc: srl@… Branch:
Release Notes:
API Changes:
Internal Changes:

Description

The mimetypes library needs that mimetypes.init() be called before threads try to use guess_type and other functions, because not doing so makes the function being called to try to call init in a non thread safe way. This has been fixed in python 2.6.3 (which was still not relased) but not before.

I want this fixed because this triggers a bug on python 2.6 on mac osx that makes it core dump.

I fixed my own copy of trac adding "import mimetypes; mimetypes.init()" on web/wsgi.py but I don't know if there is the best place to do so.

Attachments (2)

8629mimetypes.patch (641 bytes ) - added by Steven R. Loomis <srl@…> 15 years ago.
8629mimetypes.patch
t8629-mimetypes.patch (574 bytes ) - added by Christian Boos 15 years ago.
Fix race condition during mimetypes initialization.

Download all attachments as: .zip

Change History (12)

comment:1 by Christian Boos, 15 years ago

Milestone: 0.12.1

comment:2 by Carsten Klein <carsten.klein@…>, 15 years ago

please provide you patch for a start

comment:3 by Steven R. Loomis <srl@…>, 15 years ago

I hit this also. The python bug is http://bugs.python.org/issue6763 and the stack looks like:

2010-05-20 11:06:08,930 Trac[main] ERROR: Internal Server Error: 
Traceback (most recent call last):
  File "/home/srl/src/trac/trac/web/main.py", line 512, in _dispatch_request
    dispatcher.dispatch(req)
  File "/home/srl/src/trac/trac/web/main.py", line 233, in dispatch
    resp = chosen_handler.process_request(req)
  File "/home/srl/src/trac/trac/web/chrome.py", line 463, in process_request
    req.send_file(path, get_mimetype(path))
  File "/home/srl/src/trac/trac/web/api.py", line 486, in send_file
    mimetype = mimetypes.guess_type(path)[0] or \
  File "/usr/lib64/python2.6/mimetypes.py", line 242, in guess_type
    return guess_type(url, strict)
  File "/usr/lib64/python2.6/mimetypes.py", line 242, in guess_type
    return guess_type(url, strict)

by Steven R. Loomis <srl@…>, 15 years ago

Attachment: 8629mimetypes.patch added

8629mimetypes.patch

comment:4 by Steven R. Loomis <srl@…>, 15 years ago

Cc: srl@… added

comment:5 by Christian Boos, 15 years ago

Milestone: next-minor-0.12.x0.12
Owner: set to srl@…
Severity: normalmajor

Interesting. Note that this got me thinking about #8443 again, as all the TimeoutError we still have regularly here on t.e.o are usually happening right after application startup. I wonder if an Internal Error like this one (or other internal errors at startup) can explain why the db connections are not returned to the pool. OTOH, I've never seen anything like what you report in comment:3 in the logs.

Anyway, we should fix this, thanks for the patch!

First I had some trouble finding confirmation that code executed in an imported module is indeed executed in a thread-safe way, but at last I found http://docs.python.org/library/imp.html#imp.lock_held; so the patch calls mimetypes.init() the correct way.

But I don't think trac/web/__init__.py is the correct place for this, as only some scripts are using from trac.web import ..., see TracModWSGI, TracFastCgi, source:trunk/trac/web/standalone.py, source:trunk/trac/web/mod_python.py and source:trunk/trac/admin/templates/deploy_trac.wsgi, all are directly using from trac.web.main import dispatch_request, so we should do this init() there instead. Would you mind updating the patch?

by Christian Boos, 15 years ago

Attachment: t8629-mimetypes.patch added

Fix race condition during mimetypes initialization.

in reply to:  5 comment:6 by Christian Boos, 15 years ago

… Would you mind updating the patch?

Nevermind… would you mind testing t8629-mimetypes.patch? ;-)

in reply to:  5 ; comment:7 by Remy Blank, 15 years ago

Replying to cboos:

all are directly using from trac.web.main import dispatch_request, so we should do this init() there instead.

That doesn't really matter: if you import trac.web.main, then trac.web is imported first.

in reply to:  7 comment:8 by Christian Boos, 15 years ago

Replying to rblank:

Replying to cboos:

all are directly using from trac.web.main import dispatch_request, so we should do this init() there instead.

That doesn't really matter: if you import trac.web.main, then trac.web is imported first.

Ah, I didn't know that. OK, so it's probably safer to keep it in trac/web/__init__.py.

comment:9 by Christian Boos, 15 years ago

Keywords: racecondition added
Resolution: fixed
Status: newclosed

Slightly modified patch applied in [9740]. Thanks!

comment:10 by Steven R. Loomis <srl@…>, 14 years ago

welcome!

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain srl@….
The resolution will be deleted. Next status will be 'reopened'.
to The owner will be changed from srl@… to the specified user.

Add Comment


E-mail address and name can be saved in the Preferences .
 
Note: See TracTickets for help on using tickets.