movido threadlocals para middleware.py