Manually backported [3978/trunk/trac/web/main.py], so that the thread-specific cleanup is done in any case but CGI.
Authored by: cboos 2006-11-16
Parent: [r4300]
Child: [r4302]