'Clear all contexts in the thread.  Destroy the context if and only
if we are in the main thread.'

def reset(self):

    while self.context_stack:
        ctx = self.context_stack.pop()
        ctx.pop()
    if (threading.current_thread() == self._mainthread):
        self._destroy_all_contexts()
