close session when appropriate