In recovering closed guard with an underhook, your “inside” or non-underhooking arm is going to have to do a lot of work. You will be using it to frame at the forearm close to the elbow or to possibly push with your hand to open up your opponent’s knee to make space for your inside knee to come free. You will also need it to keep that knee “clean,” preventing your opponent from pushing that knee back down into half guard by either controlling at their wrist or controlling under their elbow/armpit area and blocking the arm from getting anywhere near your knee.
If your foot gets trapped inside of your opponent’s thigh, you will want to try to simply yank your foot free. Sometimes this will work, but often you will stay jammed up. Assuming you have controlled your opponent’s arm with your non-underhooking arm, you will have an easier time escaping your foot if you place your toes on the ground and use it to press/shrimp your hips out to the side. A small tip here is that when you are shrimping, it helps to focus on moving your head downward as much as you move your hips out. The hips and head balance each other in shrimping in the same way that the arms of a compass balance each other around the pivot point in the middle (in shrimping the pivot point is your shoulders). This will free your foot to recover closed guard more easily.