moved threadlocals to middleware.py