Update middleware.py

RuntimeError: dictionary changed size during iteration