contextmanagers: Put state restore code into finally block