Added a check to ensure we don't close a nil session.