fix variable scope bug when calling `willExitState`