When being in Genjutsu, the smartest thing always is getting out of it as fast as you can because Genjutsu, especially techniques of higher rank can be very dangerous when used properly and can actually kill you without your opponent even making a direct move on you. After detecting that you're in an illusion, releasing from Genjutsu can be done in three ways. First way of release is through distrubing your physical energy. The person that's in Genjutsu can cause an injury on himself/herself to produce pain in a way that the bigger the pain is, you can release yourself from the higher of rank Genjutsu. People that are extremely durable to pain, such as Kaguya clan members cannot find soulution of their problems in the first way. Seciond way is through disturbing your spiritual energy. The person caught in an illusion can get out of it in two ways; with using a cannon technique Genjutsu: Kai which is the most usual way, or through techniques that produce a high surge of chakra on your chakra flow, such as opnening Eight Inner Gates, and/or releasing a very large amount of chakra all of a sudden. This second technique od countering is not easy to accomplish and thus can be performed only by people with great chakra supplies, such as Uzumaki clan members (as seen by Naruto numerous times). The final way of release is through the production of foreign chakra or energies. This can be done through people who can perform Chakra Transfer Technique transferring their chakra to you, or by entering Sage Mode, being a Jinchuuriki or releasing a Curse Mark. Though, the last technique is ineffective against Mangekyou Sharingan Genjutsu.